ghostdrive1 commited on
Commit
d94bec9
·
verified ·
1 Parent(s): f39ce5d

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. main.py +27 -1
main.py CHANGED
@@ -4,6 +4,7 @@ from fastapi.staticfiles import StaticFiles
4
  from fastapi.responses import FileResponse
5
  from pydantic import BaseModel
6
  import os
 
7
  import httpx
8
  import redis
9
 
@@ -86,7 +87,32 @@ async def chat_endpoint(request: ChatRequest):
86
  )
87
  resp.raise_for_status()
88
  data = resp.json()
89
- reply = data["choices"][0]["message"]["content"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  # Cache to Redis if available
92
  if REDIS_OK:
 
4
  from fastapi.responses import FileResponse
5
  from pydantic import BaseModel
6
  import os
7
+ import json
8
  import httpx
9
  import redis
10
 
 
87
  )
88
  resp.raise_for_status()
89
  data = resp.json()
90
+
91
+ # Robust response extraction — different providers return different formats
92
+ reply = None
93
+ try:
94
+ choices = data.get("choices", [])
95
+ if choices and len(choices) > 0:
96
+ choice = choices[0]
97
+ msg = choice.get("message") or choice.get("delta") or {}
98
+ reply = msg.get("content") or choice.get("text") or ""
99
+ except (KeyError, IndexError, TypeError):
100
+ pass
101
+
102
+ # Fallback: some providers use different top-level keys
103
+ if not reply:
104
+ reply = (
105
+ data.get("text")
106
+ or data.get("output")
107
+ or data.get("response")
108
+ or data.get("content")
109
+ or data.get("result")
110
+ or ""
111
+ )
112
+
113
+ # Last resort: dump the raw response so the user can see what came back
114
+ if not reply:
115
+ reply = f"[Unexpected response format from {request.provider}]\n\nRaw: {json.dumps(data, indent=2)[:1000]}"
116
 
117
  # Cache to Redis if available
118
  if REDIS_OK: