Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -54,7 +54,7 @@ def get_api_key(call: str = 'api_key') -> str:
|
|
54 |
|
55 |
def encode_chat(messages: List[dict]) -> str:
|
56 |
encoded = "\n".join(
|
57 |
-
f"<|im_start|>{msg['role']}{
|
58 |
for msg in messages
|
59 |
)
|
60 |
logger.debug(f"Encoded chat: {encoded}")
|
@@ -170,26 +170,32 @@ async def stream_chat(params: dict):
|
|
170 |
async with session.post(
|
171 |
url,
|
172 |
headers={
|
173 |
-
"Authorization": f"Bearer {get_api_key('api_key')}",
|
174 |
"Content-Type": "application/json"
|
175 |
},
|
176 |
-
json=params
|
|
|
177 |
) as resp:
|
178 |
resp.raise_for_status()
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
193 |
except aiohttp.ClientError as e:
|
194 |
logger.error(f"Stream chat request failed on attempt {attempt}: {e}")
|
195 |
if attempt == 2:
|
@@ -242,13 +248,21 @@ async def respond(
|
|
242 |
"stream": True
|
243 |
}
|
244 |
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
252 |
|
253 |
def create_gradio_interface() -> gr.ChatInterface:
|
254 |
return gr.ChatInterface(
|
@@ -309,7 +323,7 @@ def main():
|
|
309 |
args = ArgParser().args
|
310 |
|
311 |
uvicorn.run(
|
312 |
-
|
313 |
host=args.server,
|
314 |
port=args.port,
|
315 |
reload=args.dev
|
|
|
54 |
|
55 |
def encode_chat(messages: List[dict]) -> str:
|
56 |
encoded = "\n".join(
|
57 |
+
f"<|im_start|>{msg['role']}{' [' + msg['name'] + ']' if 'name' in msg else ''}\n{msg['content']}<|end_of_text|>"
|
58 |
for msg in messages
|
59 |
)
|
60 |
logger.debug(f"Encoded chat: {encoded}")
|
|
|
170 |
async with session.post(
|
171 |
url,
|
172 |
headers={
|
173 |
+
"Authorization": f"Bearer {get_api_key('api_key' if attempt == 1 else 'oai_api_key')}",
|
174 |
"Content-Type": "application/json"
|
175 |
},
|
176 |
+
json=params,
|
177 |
+
timeout=30
|
178 |
) as resp:
|
179 |
resp.raise_for_status()
|
180 |
+
buffer = ""
|
181 |
+
async for chunk in resp.content:
|
182 |
+
if chunk:
|
183 |
+
buffer += chunk.decode('utf-8')
|
184 |
+
while '\n' in buffer:
|
185 |
+
line, buffer = buffer.split('\n', 1)
|
186 |
+
line = line.strip()
|
187 |
+
if line.startswith("data: "):
|
188 |
+
line = line[6:].strip()
|
189 |
+
if line == "[DONE]":
|
190 |
+
return
|
191 |
+
if not line:
|
192 |
+
continue
|
193 |
+
try:
|
194 |
+
message = json.loads(line)
|
195 |
+
yield message
|
196 |
+
except json.JSONDecodeError:
|
197 |
+
continue
|
198 |
+
break
|
199 |
except aiohttp.ClientError as e:
|
200 |
logger.error(f"Stream chat request failed on attempt {attempt}: {e}")
|
201 |
if attempt == 2:
|
|
|
248 |
"stream": True
|
249 |
}
|
250 |
|
251 |
+
try:
|
252 |
+
response_text = ""
|
253 |
+
async for token in stream_chat(params):
|
254 |
+
if token and 'choices' in token and len(token['choices']) > 0:
|
255 |
+
delta = token['choices'][0].get('delta', {})
|
256 |
+
content = delta.get("content", delta.get("refusal", ""))
|
257 |
+
response_text += content
|
258 |
+
yield response_text
|
259 |
+
|
260 |
+
if not response_text:
|
261 |
+
yield "I apologize, but I was unable to generate a response. Please try again."
|
262 |
+
|
263 |
+
except Exception as e:
|
264 |
+
logger.error(f"Error during chat response generation: {e}")
|
265 |
+
yield "I encountered an error while processing your request. Please try again later."
|
266 |
|
267 |
def create_gradio_interface() -> gr.ChatInterface:
|
268 |
return gr.ChatInterface(
|
|
|
323 |
args = ArgParser().args
|
324 |
|
325 |
uvicorn.run(
|
326 |
+
app,
|
327 |
host=args.server,
|
328 |
port=args.port,
|
329 |
reload=args.dev
|