Spaces:
Running
Running
Update noRag.py
Browse files
noRag.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
#
|
2 |
|
3 |
from fastapi import APIRouter, HTTPException
|
4 |
from pydantic import BaseModel
|
@@ -6,7 +6,6 @@ from groq import Groq
|
|
6 |
from pymongo import MongoClient
|
7 |
from config import CONNECTION_STRING, CHATGROQ_API_KEY, CUSTOM_PROMPT
|
8 |
|
9 |
-
# Create router under /norag
|
10 |
router = APIRouter(prefix="/norag", tags=["noRag"])
|
11 |
|
12 |
# Initialize Groq client and MongoDB
|
@@ -15,24 +14,21 @@ mongo = MongoClient(CONNECTION_STRING)
|
|
15 |
db = mongo["edulearnai"]
|
16 |
chats = db["chats"]
|
17 |
|
18 |
-
# System prompt
|
19 |
SYSTEM_PROMPT = "You are a helpful assistant which helps people in their tasks."
|
20 |
|
21 |
-
|
22 |
-
type ChatRequest(BaseModel):
|
23 |
session_id: str
|
24 |
-
question:
|
25 |
|
26 |
@router.post("/chat", summary="Ask a question to the noRag assistant")
|
27 |
async def chat_endpoint(req: ChatRequest):
|
28 |
-
# Fetch or create session
|
29 |
-
doc = chats.find_one({"session_id": req.session_id})
|
30 |
if not doc:
|
31 |
doc = {"session_id": req.session_id, "history": [], "summary": ""}
|
32 |
chats.insert_one(doc)
|
33 |
|
34 |
-
history = doc["history"]
|
35 |
-
summary = doc["summary"]
|
36 |
|
37 |
# Summarize if history too long
|
38 |
if len(history) >= 10:
|
@@ -40,7 +36,7 @@ doc = chats.find_one({"session_id": req.session_id})
|
|
40 |
combined = summary + "\n" + "\n".join(msgs)
|
41 |
sum_prompt = (
|
42 |
"Summarize the following chat history in one or two short sentences:\n\n"
|
43 |
-
|
44 |
)
|
45 |
sum_resp = client.chat.completions.create(
|
46 |
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
@@ -53,15 +49,15 @@ doc = chats.find_one({"session_id": req.session_id})
|
|
53 |
summary = sum_resp.choices[0].message.content.strip()
|
54 |
history = []
|
55 |
|
56 |
-
# Build
|
57 |
-
chat_hist_text = "\n".join(
|
58 |
full_prompt = CUSTOM_PROMPT.format(
|
59 |
context=SYSTEM_PROMPT,
|
60 |
chat_history=chat_hist_text or "(no prior messages)",
|
61 |
question=req.question
|
62 |
)
|
63 |
|
64 |
-
#
|
65 |
resp = client.chat.completions.create(
|
66 |
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
67 |
messages=[{"role": "user", "content": full_prompt}],
|
@@ -72,8 +68,8 @@ doc = chats.find_one({"session_id": req.session_id})
|
|
72 |
)
|
73 |
answer = resp.choices[0].message.content.strip()
|
74 |
|
75 |
-
#
|
76 |
-
history.append({"role": "user",
|
77 |
history.append({"role": "assistant", "content": answer})
|
78 |
chats.replace_one(
|
79 |
{"session_id": req.session_id},
|
|
|
1 |
+
# noRag.py
|
2 |
|
3 |
from fastapi import APIRouter, HTTPException
|
4 |
from pydantic import BaseModel
|
|
|
6 |
from pymongo import MongoClient
|
7 |
from config import CONNECTION_STRING, CHATGROQ_API_KEY, CUSTOM_PROMPT
|
8 |
|
|
|
9 |
router = APIRouter(prefix="/norag", tags=["noRag"])
|
10 |
|
11 |
# Initialize Groq client and MongoDB
|
|
|
14 |
db = mongo["edulearnai"]
|
15 |
chats = db["chats"]
|
16 |
|
|
|
17 |
SYSTEM_PROMPT = "You are a helpful assistant which helps people in their tasks."
|
18 |
|
19 |
+
class ChatRequest(BaseModel):
|
|
|
20 |
session_id: str
|
21 |
+
question: str
|
22 |
|
23 |
@router.post("/chat", summary="Ask a question to the noRag assistant")
|
24 |
async def chat_endpoint(req: ChatRequest):
|
25 |
+
# Fetch or create session in MongoDB
|
26 |
+
doc = chats.find_one({"session_id": req.session_id})
|
27 |
if not doc:
|
28 |
doc = {"session_id": req.session_id, "history": [], "summary": ""}
|
29 |
chats.insert_one(doc)
|
30 |
|
31 |
+
history, summary = doc["history"], doc["summary"]
|
|
|
32 |
|
33 |
# Summarize if history too long
|
34 |
if len(history) >= 10:
|
|
|
36 |
combined = summary + "\n" + "\n".join(msgs)
|
37 |
sum_prompt = (
|
38 |
"Summarize the following chat history in one or two short sentences:\n\n"
|
39 |
+
f"{combined}\n\nSummary:"
|
40 |
)
|
41 |
sum_resp = client.chat.completions.create(
|
42 |
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
|
|
49 |
summary = sum_resp.choices[0].message.content.strip()
|
50 |
history = []
|
51 |
|
52 |
+
# Build the prompt for Groq
|
53 |
+
chat_hist_text = "\n".join(f"{m['role']}: {m['content']}" for m in history)
|
54 |
full_prompt = CUSTOM_PROMPT.format(
|
55 |
context=SYSTEM_PROMPT,
|
56 |
chat_history=chat_hist_text or "(no prior messages)",
|
57 |
question=req.question
|
58 |
)
|
59 |
|
60 |
+
# Get the assistant’s answer
|
61 |
resp = client.chat.completions.create(
|
62 |
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
63 |
messages=[{"role": "user", "content": full_prompt}],
|
|
|
68 |
)
|
69 |
answer = resp.choices[0].message.content.strip()
|
70 |
|
71 |
+
# Persist the Q&A
|
72 |
+
history.append({"role": "user", "content": req.question})
|
73 |
history.append({"role": "assistant", "content": answer})
|
74 |
chats.replace_one(
|
75 |
{"session_id": req.session_id},
|