Commit ·
c6abcdb
1
Parent(s): d26352f
added error handling
Browse files
main.py
CHANGED
|
@@ -140,7 +140,6 @@ async def proxy(request: Request, model_path: str, api_key: str = Security(get_a
|
|
| 140 |
if k.lower() not in ['host', 'authorization', 'x-goog-api-key', 'content-length']
|
| 141 |
}
|
| 142 |
|
| 143 |
-
print(request_body_to_send)
|
| 144 |
|
| 145 |
if "streamGenerateContent" in model_path:
|
| 146 |
target_url = target_url + "&alt=sse"
|
|
@@ -153,11 +152,22 @@ async def proxy(request: Request, model_path: str, api_key: str = Security(get_a
|
|
| 153 |
)
|
| 154 |
response = await client.send(req, stream=True)
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
if "streamGenerateContent" in model_path:
|
| 157 |
async def stream_generator():
|
| 158 |
try:
|
| 159 |
async for line in response.aiter_lines():
|
| 160 |
-
print(line)
|
| 161 |
yield f"{line}\n"
|
| 162 |
finally:
|
| 163 |
await response.aclose()
|
|
@@ -169,10 +179,10 @@ async def proxy(request: Request, model_path: str, api_key: str = Security(get_a
|
|
| 169 |
response_data = await response.aread()
|
| 170 |
response_json = json.loads(response_data)
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
|
| 177 |
modified_response_data = json.dumps(response_json).encode('utf-8')
|
| 178 |
|
|
|
|
| 140 |
if k.lower() not in ['host', 'authorization', 'x-goog-api-key', 'content-length']
|
| 141 |
}
|
| 142 |
|
|
|
|
| 143 |
|
| 144 |
if "streamGenerateContent" in model_path:
|
| 145 |
target_url = target_url + "&alt=sse"
|
|
|
|
| 152 |
)
|
| 153 |
response = await client.send(req, stream=True)
|
| 154 |
|
| 155 |
+
if response.status_code != 200:
|
| 156 |
+
try:
|
| 157 |
+
response_data = await response.aread()
|
| 158 |
+
return Response(
|
| 159 |
+
content=response_data,
|
| 160 |
+
status_code=response.status_code,
|
| 161 |
+
headers=dict(response.headers),
|
| 162 |
+
)
|
| 163 |
+
finally:
|
| 164 |
+
await response.aclose()
|
| 165 |
+
await client.aclose()
|
| 166 |
+
|
| 167 |
if "streamGenerateContent" in model_path:
|
| 168 |
async def stream_generator():
|
| 169 |
try:
|
| 170 |
async for line in response.aiter_lines():
|
|
|
|
| 171 |
yield f"{line}\n"
|
| 172 |
finally:
|
| 173 |
await response.aclose()
|
|
|
|
| 179 |
response_data = await response.aread()
|
| 180 |
response_json = json.loads(response_data)
|
| 181 |
|
| 182 |
+
if 'candidates' in response_json:
|
| 183 |
+
for candidate in response_json.get('candidates', []):
|
| 184 |
+
if 'content' in candidate and 'parts' in candidate.get('content', {}):
|
| 185 |
+
candidate['content']['parts'] = [part for part in candidate['content']['parts'] if part]
|
| 186 |
|
| 187 |
modified_response_data = json.dumps(response_json).encode('utf-8')
|
| 188 |
|