smgc commited on
Commit
3faecc7
·
verified ·
1 Parent(s): f018b8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -26
app.py CHANGED
@@ -77,13 +77,7 @@ def format_model_name(model):
77
  async def handle_root(request):
78
  logger.info("Received request to root path /")
79
  return web.Response(text="GCP Vertex Claude API Proxy", status=200)
80
-
81
- async def safe_write(response, data):
82
- try:
83
- await response.write(data)
84
- except ConnectionResetError:
85
- logger.warning("Failed to write response, client likely disconnected")
86
-
87
  async def handle_request(request):
88
  logger.info(f"Received {request.method} request to {request.path}")
89
 
@@ -129,47 +123,37 @@ async def handle_request(request):
129
 
130
  logger.info(f"Sending request to Anthropic API: {api_url}")
131
 
 
132
  response = StreamResponse(status=200)
133
  response.headers['Content-Type'] = 'application/json'
134
  response.headers['Access-Control-Allow-Origin'] = '*'
135
  response.headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
136
  response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization, x-api-key, anthropic-version, model'
137
 
138
- try:
139
- await response.prepare(request)
140
 
 
141
  async with aiohttp.ClientSession() as session:
142
  async with session.post(api_url, json=request_body, headers=headers, timeout=ClientTimeout(total=30)) as api_response:
143
  api_response.raise_for_status()
144
  logger.info(f"Received response from Anthropic API. Status: {api_response.status}")
145
  logger.info(f"Response headers: {dict(api_response.headers)}")
146
 
147
- total_bytes_sent = 0
148
  async for chunk in api_response.content.iter_any():
149
- try:
150
- await response.write(chunk)
151
- total_bytes_sent += len(chunk)
152
- logger.debug(f"Sent {total_bytes_sent} bytes to client")
153
- except ConnectionResetError:
154
- logger.warning(f"Client closed connection prematurely after receiving {total_bytes_sent} bytes")
155
- break
156
 
157
- logger.info(f"Finished streaming response from Anthropic API. Total bytes sent: {total_bytes_sent}")
158
  except aiohttp.ClientError as e:
159
  logger.error(f"Error communicating with Anthropic API: {str(e)}")
160
- await safe_write(response, json.dumps({'error': 'Error communicating with AI service'}).encode())
161
  except asyncio.TimeoutError:
162
  logger.error("Request to Anthropic API timed out")
163
- await safe_write(response, json.dumps({'error': 'Request timed out'}).encode())
164
  except Exception as e:
165
  logger.error(f"Unexpected error: {str(e)}")
166
- await safe_write(response, json.dumps({'error': 'An unexpected error occurred'}).encode())
167
  finally:
168
- try:
169
- await response.write_eof()
170
- logger.info("Successfully wrote EOF to response")
171
- except ConnectionResetError:
172
- logger.warning("Failed to write EOF, client likely disconnected")
173
  logger.info("Finished sending response back to client")
174
 
175
  return response
 
77
  async def handle_root(request):
78
  logger.info("Received request to root path /")
79
  return web.Response(text="GCP Vertex Claude API Proxy", status=200)
80
+
 
 
 
 
 
 
81
  async def handle_request(request):
82
  logger.info(f"Received {request.method} request to {request.path}")
83
 
 
123
 
124
  logger.info(f"Sending request to Anthropic API: {api_url}")
125
 
126
+ # 创建一个 StreamResponse 对象
127
  response = StreamResponse(status=200)
128
  response.headers['Content-Type'] = 'application/json'
129
  response.headers['Access-Control-Allow-Origin'] = '*'
130
  response.headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
131
  response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization, x-api-key, anthropic-version, model'
132
 
133
+ await response.prepare(request)
 
134
 
135
+ try:
136
  async with aiohttp.ClientSession() as session:
137
  async with session.post(api_url, json=request_body, headers=headers, timeout=ClientTimeout(total=30)) as api_response:
138
  api_response.raise_for_status()
139
  logger.info(f"Received response from Anthropic API. Status: {api_response.status}")
140
  logger.info(f"Response headers: {dict(api_response.headers)}")
141
 
 
142
  async for chunk in api_response.content.iter_any():
143
+ await response.write(chunk)
 
 
 
 
 
 
144
 
145
+ logger.info("Finished streaming response from Anthropic API")
146
  except aiohttp.ClientError as e:
147
  logger.error(f"Error communicating with Anthropic API: {str(e)}")
148
+ await response.write(json.dumps({'error': 'Error communicating with AI service'}).encode())
149
  except asyncio.TimeoutError:
150
  logger.error("Request to Anthropic API timed out")
151
+ await response.write(json.dumps({'error': 'Request timed out'}).encode())
152
  except Exception as e:
153
  logger.error(f"Unexpected error: {str(e)}")
154
+ await response.write(json.dumps({'error': 'An unexpected error occurred'}).encode())
155
  finally:
156
+ await response.write_eof()
 
 
 
 
157
  logger.info("Finished sending response back to client")
158
 
159
  return response