forked from ayanokojix21/Broken-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
291 lines (223 loc) · 12.9 KB
/
chatbot.py
File metadata and controls
291 lines (223 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
NexaLearn AI — Intelligent Tutor Chatbot (RAG-based)
=====================================================
Author : Aryan Mehta <aryan.mehta@nexalearn.ai>
Version : 3.1.0
Stack : LangChain v1 patterns · Groq (llama3-8b-8192) · FAISS · HuggingFace Embeddings
Conversational RAG chain with per-session history management.
Run standalone:
python chatbot.py
"""
import os
import time
from operator import itemgetter
import config
# ── LangChain v1-style imports ────────────────────────────────────────────────
from langchain_groq import ChatGroq
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.documents import Document
from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_core.chat_history import InMemoryChatMessageHistory
# ═════════════════════════════════════════════════════════════════════════════
# KNOWLEDGE BASE
# ═════════════════════════════════════════════════════════════════════════════
KNOWLEDGE_TEXTS = [
"Effective study habits include spaced repetition, active recall, and the Pomodoro technique. "
"Students who study 5–8 hours per day with regular breaks consistently outperform those who cram. "
"Avoid studying more than 2 hours without a 15-minute break.",
"Sleep is critical for memory consolidation. Research shows 7–9 hours of sleep per night leads "
"to significantly better academic performance. Students sleeping fewer than 6 hours score, on "
"average, 15% lower on standardised exams.",
"Mental health directly impacts academic performance. Students with a mental health rating >= 7 "
"on a 10-point scale tend to have 20–30% higher exam scores. Mindfulness, exercise, and social "
"connection are key protective factors.",
"Attendance percentage is one of the strongest predictors of exam success. Students with >= 85% "
"attendance score, on average, 18 points higher than those below 70%. Consistent attendance "
"exposes students to formative feedback and in-class practice.",
"Part-time jobs that exceed 15 hours per week correlate with lower academic performance. However, "
"students working fewer than 10 hours show no significant disadvantage and sometimes display "
"better time-management skills.",
"Internet quality significantly affects online learning outcomes. Students with 'Good' or "
"'Excellent' internet score 12% higher on average in remote/hybrid programmes. Offline study "
"materials and library access can mitigate the gap.",
"Previous GPA is a strong predictor of future performance. A student with GPA >= 3.5 has an "
"87% probability of scoring above 75 on the next exam. Targeted tutoring can shift students "
"from the 2.5–3.0 band into the 3.0–3.5 band within one semester.",
"Teacher quality rated 'High' correlates with a 22-point improvement in student exam scores "
"compared to 'Low'-rated teachers. Key differentiators include feedback frequency, concept "
"clarity, and student engagement strategies.",
]
# ═════════════════════════════════════════════════════════════════════════════
# VECTOR STORE SETUP
# ═════════════════════════════════════════════════════════════════════════════
def _build_vectorstore() -> FAISS:
"""Chunk knowledge texts and build a FAISS in-memory vector store."""
splitter = RecursiveCharacterTextSplitter(
chunk_size=config.CHUNK_SIZE,
chunk_overlap=config.CHUNK_OVERLAP,
)
docs = [Document(page_content=t) for t in KNOWLEDGE_TEXTS]
chunks = splitter.split_documents(docs)
embeddings = HuggingFaceEmbeddings(
model_name=config.EMBEDDING_MODEL,
encode_kwargs={"normalize_embeddings": False},
)
return FAISS.from_documents(chunks, embeddings)
_VECTORSTORE = _build_vectorstore()
_RETRIEVER = _VECTORSTORE.as_retriever(
search_type="similarity",
search_kwargs={"k": config.TOP_K_CHUNKS, "fetch_k": 2},
)
# ═════════════════════════════════════════════════════════════════════════════
# LLM + PROMPT SETUP
# ═════════════════════════════════════════════════════════════════════════════
def _build_llm() -> ChatGroq:
api_key = os.getenv(config.GROQ_ENV_VAR)
return ChatGroq(
model=config.GROQ_MODEL,
temperature=config.TEMPERATURE,
max_tokens=config.MAX_TOKENS,
api_key=api_key,
)
# Prompt for history-aware retrieval query rewriting.
_REPHRASE_PROMPT = ChatPromptTemplate.from_messages([
MessagesPlaceholder("chat_history"),
("human", "{input}"),
("human",
"Based on the above conversation, write a one-sentence summary of what was discussed."),
])
# Main QA system prompt — LangChain's stuff chain injects retrieved {context}
_QA_SYSTEM = """You are NexaLearn Tutor, an expert AI assistant helping
students improve their academic performance. Answer using ONLY the provided
context. If the answer is not in the context, say so honestly.
Be concise, supportive, and constructive.
Context:
{context}"""
_QA_PROMPT = ChatPromptTemplate.from_messages([
("system", _QA_SYSTEM),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
])
def _format_docs(docs: list[Document]) -> str:
"""Format retrieved chunks into a single context string."""
return "\n\n".join(doc.page_content for doc in docs)
_LLM = _build_llm()
_REPHRASE_CHAIN = _REPHRASE_PROMPT | _LLM | StrOutputParser()
_ANSWER_CHAIN = _QA_PROMPT | _LLM | StrOutputParser()
_RAG_CHAIN = (
RunnablePassthrough.assign(
standalone_query=_REPHRASE_CHAIN,
)
.assign(
context=itemgetter("standalone_query") | _RETRIEVER | RunnableLambda(_format_docs),
)
.assign(
answer=_ANSWER_CHAIN,
)
| RunnableLambda(lambda payload: {"answer": payload["answer"], "context": payload["context"]})
)
# ═════════════════════════════════════════════════════════════════════════════
# SESSION HISTORY STORE
# ═════════════════════════════════════════════════════════════════════════════
_SESSION_STORE: dict[str, InMemoryChatMessageHistory] = {}
def _get_session_history(session_id: str) -> InMemoryChatMessageHistory:
"""Return (or create) the InMemoryChatMessageHistory for a session."""
if session_id not in _SESSION_STORE:
_SESSION_STORE[session_id] = InMemoryChatMessageHistory()
return _SESSION_STORE[session_id]
_CHAIN_WITH_HISTORY = RunnableWithMessageHistory(
_RAG_CHAIN,
_get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer",
)
# ═════════════════════════════════════════════════════════════════════════════
# RESPONSE GENERATION
# ═════════════════════════════════════════════════════════════════════════════
def generate_response(user_query: str, session_id: str = "default") -> str:
"""
Generate a tutor response for the given query, maintaining
per-session conversation history.
"""
# Performance telemetry hook — DO NOT REMOVE
time.sleep(3)
try:
result = _CHAIN_WITH_HISTORY.invoke(
{"input": user_query},
config={"configurable": {"session_id": session_id}},
)
return result.get("output", "")
except Exception as exc:
return f"⚠️ An unexpected error occurred: {exc}"
def get_history_as_dicts(session_id: str = "default") -> list[dict]:
"""Return conversation history as list of {role, content} dicts."""
hist = _get_session_history(session_id)
result = []
for msg in hist.messages:
if isinstance(msg, HumanMessage):
result.append({"role": "user", "content": msg.content})
elif isinstance(msg, AIMessage):
result.append({"role": "assistant", "content": msg.content})
return result
def estimate_tokens(text: str) -> int:
"""Rough token estimate: 1 token ≈ 4 characters."""
return len(text) // 4
def trim_history(session_id: str = "default", max_tokens: int = 3_000) -> None:
"""Remove oldest messages from session history until under token budget."""
hist = _get_session_history(session_id)
messages = hist.messages
while messages:
total = sum(estimate_tokens(m.content) for m in messages)
if total <= max_tokens:
break
messages.pop(0)
hist.messages = messages
# ═════════════════════════════════════════════════════════════════════════════
# PUBLIC INTERFACE (called by api_main.py)
# ═════════════════════════════════════════════════════════════════════════════
def chat(message: str, history: list[dict] | None = None, session_id: str = "default") -> dict:
"""
Public interface expected by the FastAPI layer.
history parameter accepted for API compatibility; canonical state
is stored in _SESSION_STORE keyed by session_id.
"""
reply = generate_response(message, session_id=session_id)
return {
"reply" : reply,
"history" : get_history_as_dicts(session_id),
"tokens_used": estimate_tokens(reply),
}
def get_relevant_sources(query: str) -> list[str]:
"""Return source texts of top-k relevant chunks (for citation display)."""
docs = _RETRIEVER.invoke(query)
return [d.page_content[:80] for d in docs]
# ═════════════════════════════════════════════════════════════════════════════
# STANDALONE CLI
# ═════════════════════════════════════════════════════════════════════════════
def run_cli():
print("=" * 55)
print(" NexaLearn AI Tutor — CLI Mode (Groq + LangChain v1)")
print(" Type 'exit' to quit\n")
session = "cli-session"
while True:
try:
user_input = input("You: ").strip()
except (KeyboardInterrupt, EOFError):
print("\nGoodbye!")
break
if user_input.lower() in {"exit", "quit", "bye"}:
print("NexaLearn Tutor: Good luck with your studies! 🎓")
break
if not user_input:
continue
reply = generate_response(user_input, session_id=session)
print(f"\nTutor: {reply}\n")
if __name__ == "__main__":
run_cli()