add next quiz to json
Browse files
__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ
|
|
|
__pycache__/optimized_quiz.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/optimized_quiz.cpython-312.pyc and b/__pycache__/optimized_quiz.cpython-312.pyc differ
|
|
|
clients/__pycache__/groq_client.cpython-312.pyc
CHANGED
|
Binary files a/clients/__pycache__/groq_client.cpython-312.pyc and b/clients/__pycache__/groq_client.cpython-312.pyc differ
|
|
|
main.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
|
@@ -62,20 +63,28 @@ async def chat_endpoint(request: QueryRequest):
|
|
| 62 |
# ============================================
|
| 63 |
@app.post("/chat-stream")
|
| 64 |
async def chat_stream_endpoint(request: QueryRequest):
|
| 65 |
-
"""Streaming endpoint
|
| 66 |
try:
|
| 67 |
groq_client = get_groq_client()
|
| 68 |
|
| 69 |
def generate():
|
| 70 |
try:
|
|
|
|
| 71 |
for chunk in groq_client.ask_stream(request.query):
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
-
yield f"data: Error: {str(e)}\n\n"
|
| 79 |
|
| 80 |
return StreamingResponse(
|
| 81 |
generate(),
|
|
@@ -88,7 +97,11 @@ async def chat_stream_endpoint(request: QueryRequest):
|
|
| 88 |
)
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
|
| 94 |
@app.get("/")
|
|
|
|
| 1 |
+
import json
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
|
|
|
| 63 |
# ============================================
|
| 64 |
@app.post("/chat-stream")
|
| 65 |
async def chat_stream_endpoint(request: QueryRequest):
|
| 66 |
+
"""Streaming endpoint that returns chunks in real-time with a consistent structure"""
|
| 67 |
try:
|
| 68 |
groq_client = get_groq_client()
|
| 69 |
|
| 70 |
def generate():
|
| 71 |
try:
|
| 72 |
+
response = ""
|
| 73 |
for chunk in groq_client.ask_stream(request.query):
|
| 74 |
+
response += chunk
|
| 75 |
|
| 76 |
+
yield f"data: {json.dumps({'status_code': 200, 'response': chunk, 'next_questions': []})}\n\n"
|
| 77 |
+
|
| 78 |
+
if response.startswith("Error while"):
|
| 79 |
+
yield f"data: {json.dumps({'status_code': 500, 'response': response, 'next_questions': []})}\n\n"
|
| 80 |
+
else:
|
| 81 |
+
|
| 82 |
+
yield f"data: {json.dumps({'status_code': 200, 'response': response, 'next_questions': []})}\n\n"
|
| 83 |
+
|
| 84 |
+
yield f"data: {json.dumps({'status_code': 200, 'response': '[DONE]', 'next_questions': groq_client.get_next_questions(request.ip)})}\n\n"
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
+
yield f"data: {json.dumps({'status_code': 500, 'response': f'Error: {str(e)}', 'next_questions': []})}\n\n"
|
| 88 |
|
| 89 |
return StreamingResponse(
|
| 90 |
generate(),
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
+
return {
|
| 101 |
+
"status_code": 500,
|
| 102 |
+
"response": f"Internal error: {e}",
|
| 103 |
+
"next_questions": [],
|
| 104 |
+
}
|
| 105 |
|
| 106 |
|
| 107 |
@app.get("/")
|