mominah commited on
Commit
ce7d50f
·
verified ·
1 Parent(s): aab9adc

Update noRag.py

Browse files
Files changed (1) hide show
  1. noRag.py +53 -89
noRag.py CHANGED
@@ -1,120 +1,84 @@
1
- # noRag.py
2
 
3
- import asyncio
4
- import os
5
  from groq import Groq
6
  from pymongo import MongoClient
7
- from config import (
8
- CONNECTION_STRING,
9
- CHATGROQ_API_KEY,
10
- CUSTOM_PROMPT
11
- )
12
 
13
- # --- Setup Groq client and MongoDB ---
 
 
 
14
  client = Groq(api_key=CHATGROQ_API_KEY)
15
  mongo = MongoClient(CONNECTION_STRING)
16
  db = mongo["edulearnai"]
17
  chats = db["chats"]
18
 
 
19
  SYSTEM_PROMPT = "You are a helpful assistant which helps people in their tasks."
20
 
21
- # --- Session management in MongoDB ---
22
- def get_session(session_id: str) -> dict:
23
- """Fetch or create a session doc with keys: session_id, history (list), summary (str)."""
24
- doc = chats.find_one({"session_id": session_id})
 
 
 
 
 
25
  if not doc:
26
- doc = {"session_id": session_id, "history": [], "summary": ""}
27
  chats.insert_one(doc)
28
- return doc
29
 
30
- def save_session(doc: dict):
31
- """Overwrite the session document in Mongo."""
32
- chats.replace_one({"session_id": doc["session_id"]}, doc)
33
 
34
- # --- History summarization ---
35
- async def summarize_history(prev_summary: str, history_msgs: list[str]) -> str:
36
- """Ask the LLM to produce a short summary of the combined previous summary + new messages."""
37
- combined = prev_summary + "\n" + "\n".join(history_msgs)
38
- prompt = (
39
- "Summarize the following chat history in one or two short sentences:\n\n"
40
- f"{combined}\n\nSummary:"
41
- )
42
- resp = client.chat.completions.create(
43
- model="meta-llama/llama-4-scout-17b-16e-instruct",
44
- messages=[{"role": "user", "content": prompt}],
45
- temperature=0.3,
46
- max_completion_tokens=150,
47
- top_p=1,
48
- stream=False,
49
- )
50
- # the first (and only) completion
51
- return resp.choices[0].message.content.strip()
52
-
53
- # --- Core chat logic ---
54
- async def chat(session_id: str, question: str):
55
- session = get_session(session_id)
56
- history = session["history"]
57
- summary = session["summary"]
58
-
59
- # If history is too long, summarize it and clear
60
  if len(history) >= 10:
61
- msgs_to_summarize = [f"{m['role']}: {m['content']}" for m in history]
62
- new_summary = await summarize_history(summary, msgs_to_summarize)
63
- session["summary"] = new_summary
64
- session["history"] = []
 
 
 
 
 
 
 
 
 
 
 
65
  history = []
66
 
67
- # Build the prompt
68
- chat_history_text = "\n".join([f"{m['role']}: {m['content']}" for m in history])
69
  full_prompt = CUSTOM_PROMPT.format(
70
  context=SYSTEM_PROMPT,
71
- chat_history=chat_history_text or "(no prior messages)",
72
- question=question
73
  )
74
 
75
- # Call the model, streaming
76
- completion = client.chat.completions.create(
77
  model="meta-llama/llama-4-scout-17b-16e-instruct",
78
  messages=[{"role": "user", "content": full_prompt}],
79
  temperature=1,
80
  max_completion_tokens=1024,
81
  top_p=1,
82
- stream=True,
83
  )
 
84
 
85
- # Print & accumulate the assistant’s reply
86
- assistant_response = ""
87
- print("Assistant:", end=" ", flush=True)
88
- for chunk in completion:
89
- delta = chunk.choices[0].delta.content or ""
90
- print(delta, end="", flush=True)
91
- assistant_response += delta
92
- print() # newline after done
93
-
94
- # Persist the new exchange
95
- session["history"].append({"role": "user", "content": question})
96
- session["history"].append({"role": "assistant", "content": assistant_response})
97
- save_session(session)
98
-
99
- # --- CLI loop ---
100
- if __name__ == "__main__":
101
- import argparse
102
-
103
- parser = argparse.ArgumentParser()
104
- parser.add_argument(
105
- "--session",
106
- "-s",
107
- default="default",
108
- help="Session ID (used to key chat history in MongoDB)"
109
  )
110
- args = parser.parse_args()
111
 
112
- print(f"Starting noRag chat (session={args.session}). Type Ctrl+C to quit.\n")
113
- try:
114
- while True:
115
- user_q = input("You: ")
116
- if not user_q.strip():
117
- continue
118
- asyncio.run(chat(args.session, user_q))
119
- except KeyboardInterrupt:
120
- print("\nGoodbye!")
 
1
+ # norag_router.py
2
 
3
+ from fastapi import APIRouter, HTTPException
4
+ from pydantic import BaseModel
5
  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
13
  client = Groq(api_key=CHATGROQ_API_KEY)
14
  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
+ # Request model
22
+ type ChatRequest(BaseModel):
23
+ session_id: str
24
+ question: str
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:
39
+ msgs = [f"{m['role']}: {m['content']}" for m in history]
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
+ + combined + "\n\nSummary:"
44
+ )
45
+ sum_resp = client.chat.completions.create(
46
+ model="meta-llama/llama-4-scout-17b-16e-instruct",
47
+ messages=[{"role": "user", "content": sum_prompt}],
48
+ temperature=0.3,
49
+ max_completion_tokens=150,
50
+ top_p=1,
51
+ stream=False,
52
+ )
53
+ summary = sum_resp.choices[0].message.content.strip()
54
  history = []
55
 
56
+ # Build full prompt
57
+ chat_hist_text = "\n".join([f"{m['role']}: {m['content']}" for m in history])
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
+ # Call model
65
+ resp = client.chat.completions.create(
66
  model="meta-llama/llama-4-scout-17b-16e-instruct",
67
  messages=[{"role": "user", "content": full_prompt}],
68
  temperature=1,
69
  max_completion_tokens=1024,
70
  top_p=1,
71
+ stream=False,
72
  )
73
+ answer = resp.choices[0].message.content.strip()
74
 
75
+ # Update session doc
76
+ history.append({"role": "user", "content": req.question})
77
+ history.append({"role": "assistant", "content": answer})
78
+ chats.replace_one(
79
+ {"session_id": req.session_id},
80
+ {"session_id": req.session_id, "history": history, "summary": summary},
81
+ upsert=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  )
 
83
 
84
+ return {"session_id": req.session_id, "answer": answer, "summary": summary}