mominah commited on
Commit
8ab9404
·
verified ·
1 Parent(s): de37d9a

Create noRag.py

Browse files
Files changed (1) hide show
  1. noRag.py +120 -0
noRag.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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!")