Add error logging to chat stream

main
Clawd 3 weeks ago
parent acbab75031
commit 7731595022

@ -60,6 +60,8 @@ Answer based on the above context:"""
], ],
} }
logger.info(f"Chat request to {url} with model {model}")
# Yield sources first # Yield sources first
sources = [ sources = [
{'title': c.title, 'path': c.path, 'score': c.score} {'title': c.title, 'path': c.path, 'score': c.score}
@ -68,20 +70,25 @@ Answer based on the above context:"""
yield f'data: {json.dumps({"type": "sources", "sources": sources})}\n\n' yield f'data: {json.dumps({"type": "sources", "sources": sources})}\n\n'
# Stream tokens # Stream tokens
async with httpx.AsyncClient(timeout=120.0) as client: try:
async with client.stream('POST', url, json=payload) as resp: async with httpx.AsyncClient(timeout=180.0) as client:
resp.raise_for_status() async with client.stream('POST', url, json=payload) as resp:
async for line in resp.aiter_lines(): logger.info(f"Ollama response status: {resp.status_code}")
if not line.strip(): resp.raise_for_status()
continue async for line in resp.aiter_lines():
try: if not line.strip():
chunk_data = json.loads(line) continue
token = chunk_data.get('message', {}).get('content', '') try:
if token: chunk_data = json.loads(line)
yield f'data: {json.dumps({"type": "token", "token": token})}\n\n' token = chunk_data.get('message', {}).get('content', '')
if chunk_data.get('done', False): if token:
break yield f'data: {json.dumps({"type": "token", "token": token})}\n\n'
except json.JSONDecodeError: if chunk_data.get('done', False):
continue break
except json.JSONDecodeError:
continue
except Exception as e:
logger.error(f"Chat stream error: {e}")
yield f'data: {json.dumps({"type": "error", "error": str(e)})}\n\n'
yield f'data: {json.dumps({"type": "done"})}\n\n' yield f'data: {json.dumps({"type": "done"})}\n\n'

Loading…
Cancel
Save

Powered by TurnKey Linux.