Mohamed478 commited on
Commit
8c9a677
·
1 Parent(s): 7b97c87

Add StreamingResponse support to AI chat endpoint

Browse files
Files changed (1) hide show
  1. app/main.py +29 -9
app/main.py CHANGED
@@ -420,6 +420,8 @@ async def upload_document(file: UploadFile = File(...), db: Session = Depends(ge
420
  except Exception as e:
421
  raise HTTPException(status_code=500, detail=f"حدث خطأ أثناء معالجة الملف: {str(e)}")
422
 
 
 
423
  # --- 🚀 Upgraded Chatbot (RAG) Endpoint ---
424
  @app.post("/api/v1/ai/articles/{article_id}/chat")
425
  def chat_with_article(article_id: int, chat_req: schemas.ChatRequest, db: Session = Depends(get_db)):
@@ -432,13 +434,18 @@ def chat_with_article(article_id: int, chat_req: schemas.ChatRequest, db: Sessio
432
  # Fallback offline replies
433
  user_msg = chat_req.message.lower()
434
  if any(greeting in user_msg for greeting in ["مرحباً", "hello", "hi", "سلام", "هلا"]):
435
- return {"reply": "أهلاً بك! أنا مساعدك الذكي لقراءة وتلخيص هذا المقال. كيف يمكنني مساعدتك اليوم؟"}
436
-
437
- if any(kw in user_msg for kw in ["لخص", "ملخص", "summary", "summarize"]):
438
  summary_text = article.summary if article.summary else generate_ai_summary(article.content)
439
- return {"reply": f"إليك ملخص سريع للمقال:\n{summary_text}"}
440
-
441
- return {"reply": f"أهلاً بك! لم يتم ضبط مفتاح Groq API Key في ملف `.env` بعد. يمكنك الحصول عليه مجاناً من https://console.groq.com"}
 
 
 
 
 
 
442
 
443
  try:
444
  from groq import Groq
@@ -463,15 +470,28 @@ def chat_with_article(article_id: int, chat_req: schemas.ChatRequest, db: Sessio
463
  messages.append({"role": role, "content": h.content})
464
  messages.append({"role": "user", "content": chat_req.message})
465
 
466
- response = client.chat.completions.create(
467
  model="llama-3.3-70b-versatile",
468
  messages=messages,
469
  max_tokens=1024,
 
470
  )
471
- return {"reply": response.choices[0].message.content.strip()}
 
 
 
 
 
 
 
 
 
 
472
  except Exception as e:
473
  print(f"Groq Chat error: {e}")
474
- return {"reply": f"حدث خطأ أثناء الاتصال بالذكاء الاصطناعي: {str(e)}"}
 
 
475
 
476
  # --- 🔍 Web Search Endpoint ---
477
  @app.get("/api/v1/search", response_model=List[schemas.SearchResult])
 
420
  except Exception as e:
421
  raise HTTPException(status_code=500, detail=f"حدث خطأ أثناء معالجة الملف: {str(e)}")
422
 
423
+ from fastapi.responses import StreamingResponse
424
+
425
  # --- 🚀 Upgraded Chatbot (RAG) Endpoint ---
426
  @app.post("/api/v1/ai/articles/{article_id}/chat")
427
  def chat_with_article(article_id: int, chat_req: schemas.ChatRequest, db: Session = Depends(get_db)):
 
434
  # Fallback offline replies
435
  user_msg = chat_req.message.lower()
436
  if any(greeting in user_msg for greeting in ["مرحباً", "hello", "hi", "سلام", "هلا"]):
437
+ reply_text = "أهلاً بك! أنا مساعدك الذكي لقراءة وتلخيص هذا المقال. كيف يمكنني مساعدتك اليوم؟"
438
+ elif any(kw in user_msg for kw in ["لخص", "ملخص", "summary", "summarize"]):
 
439
  summary_text = article.summary if article.summary else generate_ai_summary(article.content)
440
+ reply_text = f"إليك ملخص سريع للمقال:\n{summary_text}"
441
+ else:
442
+ reply_text = "أهلاً بك! لم يتم ضبط مفتاح Groq API Key في ملف `.env` بعد. يمكنك الحصول عليه مجاناً من https://console.groq.com"
443
+
444
+ def fallback_generator():
445
+ for char_chunk in [reply_text[i:i+5] for i in range(0, len(reply_text), 5)]:
446
+ yield char_chunk
447
+
448
+ return StreamingResponse(fallback_generator(), media_type="text/plain; charset=utf-8")
449
 
450
  try:
451
  from groq import Groq
 
470
  messages.append({"role": role, "content": h.content})
471
  messages.append({"role": "user", "content": chat_req.message})
472
 
473
+ completion = client.chat.completions.create(
474
  model="llama-3.3-70b-versatile",
475
  messages=messages,
476
  max_tokens=1024,
477
+ stream=True
478
  )
479
+
480
+ def stream_generator():
481
+ try:
482
+ for chunk in completion:
483
+ if chunk.choices and chunk.choices[0].delta.content:
484
+ yield chunk.choices[0].delta.content
485
+ except Exception as e:
486
+ print(f"Streaming error: {e}")
487
+ yield f"\n[حدث خطأ أثناء البث: {str(e)}]"
488
+
489
+ return StreamingResponse(stream_generator(), media_type="text/plain; charset=utf-8")
490
  except Exception as e:
491
  print(f"Groq Chat error: {e}")
492
+ def error_generator():
493
+ yield f"حدث خطأ أثناء الاتصال بالذكاء الاصطناعي: {str(e)}"
494
+ return StreamingResponse(error_generator(), media_type="text/plain; charset=utf-8")
495
 
496
  # --- 🔍 Web Search Endpoint ---
497
  @app.get("/api/v1/search", response_model=List[schemas.SearchResult])