Spaces:
Sleeping
Sleeping
Update chatbot.py
Browse files- chatbot.py +41 -67
chatbot.py
CHANGED
@@ -2,6 +2,8 @@ import os
|
|
2 |
import time
|
3 |
import json
|
4 |
from groq import Groq
|
|
|
|
|
5 |
from langchain_community.document_loaders import CSVLoader
|
6 |
from langchain_community.vectorstores import FAISS
|
7 |
from deep_translator import GoogleTranslator
|
@@ -12,17 +14,14 @@ class Comsatsbot:
|
|
12 |
self.llm = llm
|
13 |
self.api_keys = api_keys
|
14 |
self.client = None
|
15 |
-
self.models = [
|
16 |
-
|
17 |
-
"llama-3.1-70b-versatile",
|
18 |
-
"llama3-70b-8192"
|
19 |
-
]
|
20 |
self.chats_collection = chats_collection
|
21 |
self.index_path = index_path
|
22 |
self.hf = hf
|
23 |
-
self.paths = paths
|
24 |
self.faiss_index = None
|
25 |
self.faiss_retriever = None
|
|
|
26 |
self.initialize_faiss_index()
|
27 |
|
28 |
def load_data(self, paths):
|
@@ -48,10 +47,7 @@ class Comsatsbot:
|
|
48 |
return None
|
49 |
|
50 |
def create_chat_record(self, chat_id):
|
51 |
-
self.chats_collection.insert_one({
|
52 |
-
"_id": chat_id,
|
53 |
-
"history": []
|
54 |
-
})
|
55 |
|
56 |
def update_chat(self, chat_id, question, answer):
|
57 |
self.chats_collection.update_one(
|
@@ -82,18 +78,13 @@ class Comsatsbot:
|
|
82 |
context_text = "\n".join([doc.page_content for doc in context])
|
83 |
|
84 |
prompt = f'''
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
University details:
|
93 |
-
Comsats Attock Campus Provides BS Computer Science, BS Software Engineering, BS Artificial Intelligence, BS English, BS Math, BS Electrical Engineering, BS Computer Engineering, BS BBA.
|
94 |
-
Departments: CS (CS, AI, SE), Math (Math, BBA, English), EE (EE, CE).
|
95 |
-
Facilities: Cricket ground, football ground, two canteens (one near Math & EE, one near CS), mosque near CS, 9 lecture theaters (LT), Math classrooms (CR), EE labs.
|
96 |
-
NTS test accepted. CGPA system: 4.0 for 85%, 3.66 for 79–84%, etc.
|
97 |
|
98 |
Context:
|
99 |
{context_text}
|
@@ -103,56 +94,30 @@ class Comsatsbot:
|
|
103 |
|
104 |
Question: {question}
|
105 |
'''
|
106 |
-
while True:
|
107 |
-
for api_key in self.api_keys:
|
108 |
-
self.client = Groq(api_key=api_key)
|
109 |
-
for model in self.models:
|
110 |
-
try:
|
111 |
-
chat_completion = self.client.chat.completions.create(
|
112 |
-
messages=[
|
113 |
-
{"role": "system", "content": prompt},
|
114 |
-
{"role": "user", "content": f"Answer the following question: {question}"}
|
115 |
-
],
|
116 |
-
model=model,
|
117 |
-
max_tokens=1024,
|
118 |
-
)
|
119 |
-
return chat_completion.choices[0].message.content
|
120 |
-
except Exception:
|
121 |
-
time.sleep(2)
|
122 |
-
continue
|
123 |
-
return "Sorry, unable to provide an answer at this time."
|
124 |
|
125 |
-
def detect_language(self, question):
|
126 |
for api_key in self.api_keys:
|
127 |
self.client = Groq(api_key=api_key)
|
128 |
for model in self.models:
|
129 |
try:
|
130 |
chat_completion = self.client.chat.completions.create(
|
131 |
messages=[
|
132 |
-
{
|
133 |
-
|
134 |
-
"content": """
|
135 |
-
You are an expert agent, and your task is to detect the language.
|
136 |
-
Return a JSON: {'detected_language': 'urdu' or 'english'}
|
137 |
-
"""
|
138 |
-
},
|
139 |
-
{
|
140 |
-
"role": "user",
|
141 |
-
"content": f"Detect the language for: {question}"
|
142 |
-
}
|
143 |
],
|
144 |
model=model,
|
145 |
-
max_tokens=
|
146 |
-
response_format={"type": "json_object"},
|
147 |
)
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
151 |
time.sleep(2)
|
152 |
continue
|
153 |
-
return "english"
|
154 |
|
155 |
-
|
|
|
|
|
156 |
for api_key in self.api_keys:
|
157 |
self.client = Groq(api_key=api_key)
|
158 |
for model in self.models:
|
@@ -162,33 +127,42 @@ class Comsatsbot:
|
|
162 |
{
|
163 |
"role": "system",
|
164 |
"content": """
|
165 |
-
|
166 |
-
{'
|
167 |
"""
|
168 |
},
|
169 |
{
|
170 |
"role": "user",
|
171 |
-
"content": f"
|
172 |
}
|
173 |
],
|
174 |
model=model,
|
175 |
-
max_tokens=
|
176 |
response_format={"type": "json_object"},
|
177 |
)
|
178 |
response = json.loads(chat_completion.choices[0].message.content)
|
179 |
-
return response['
|
180 |
-
except Exception:
|
|
|
181 |
time.sleep(2)
|
182 |
continue
|
183 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
def response(self, question, chat_id):
|
186 |
chat_history = self.load_chat(chat_id)
|
|
|
|
|
187 |
|
188 |
language = self.detect_language(question)
|
189 |
|
190 |
if language == 'urdu':
|
191 |
-
question_translation =
|
192 |
context = self.faiss_retriever.invoke(question_translation)
|
193 |
else:
|
194 |
context = self.faiss_retriever.invoke(question)
|
|
|
2 |
import time
|
3 |
import json
|
4 |
from groq import Groq
|
5 |
+
from langchain.memory import ConversationBufferMemory
|
6 |
+
from langchain_openai import ChatOpenAI
|
7 |
from langchain_community.document_loaders import CSVLoader
|
8 |
from langchain_community.vectorstores import FAISS
|
9 |
from deep_translator import GoogleTranslator
|
|
|
14 |
self.llm = llm
|
15 |
self.api_keys = api_keys
|
16 |
self.client = None
|
17 |
+
self.models = ["llama3-70b-8192"] # use a single known working model
|
18 |
+
self.memory = ConversationBufferMemory(llm=self.llm, max_token_limit=3000)
|
|
|
|
|
|
|
19 |
self.chats_collection = chats_collection
|
20 |
self.index_path = index_path
|
21 |
self.hf = hf
|
|
|
22 |
self.faiss_index = None
|
23 |
self.faiss_retriever = None
|
24 |
+
self.paths = paths
|
25 |
self.initialize_faiss_index()
|
26 |
|
27 |
def load_data(self, paths):
|
|
|
47 |
return None
|
48 |
|
49 |
def create_chat_record(self, chat_id):
|
50 |
+
self.chats_collection.insert_one({"_id": chat_id, "history": []})
|
|
|
|
|
|
|
51 |
|
52 |
def update_chat(self, chat_id, question, answer):
|
53 |
self.chats_collection.update_one(
|
|
|
78 |
context_text = "\n".join([doc.page_content for doc in context])
|
79 |
|
80 |
prompt = f'''
|
81 |
+
You are a comsats assistant. Answer concisely and with emojis only where appropriate.
|
82 |
+
Don't mention context/history explicitly. Use friendly tone and only say "I don’t know" if no relevant answer is found.
|
83 |
+
|
84 |
+
University Info:
|
85 |
+
Comsats Attock Campus offers CS, SE, AI, English, Math, EE, CE, BBA.
|
86 |
+
Departments: CS, Math, EE. Facilities: grounds, canteens, mosque, LT rooms, labs.
|
87 |
+
Admissions via NTS. CGPA: 4.0 (85%), 3.66 (79-84%), etc.
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
Context:
|
90 |
{context_text}
|
|
|
94 |
|
95 |
Question: {question}
|
96 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
|
|
98 |
for api_key in self.api_keys:
|
99 |
self.client = Groq(api_key=api_key)
|
100 |
for model in self.models:
|
101 |
try:
|
102 |
chat_completion = self.client.chat.completions.create(
|
103 |
messages=[
|
104 |
+
{"role": "system", "content": prompt},
|
105 |
+
{"role": "user", "content": f"Answer the following question: {question}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
],
|
107 |
model=model,
|
108 |
+
max_tokens=1024,
|
|
|
109 |
)
|
110 |
+
result = chat_completion.choices[0].message.content
|
111 |
+
print("Returning answer:", result)
|
112 |
+
return result
|
113 |
+
except Exception as e:
|
114 |
+
print(f"Error with {model}: {e}")
|
115 |
time.sleep(2)
|
116 |
continue
|
|
|
117 |
|
118 |
+
return "Sorry 😔 I'm currently facing technical issues. Please try again later."
|
119 |
+
|
120 |
+
def detect_language(self, question):
|
121 |
for api_key in self.api_keys:
|
122 |
self.client = Groq(api_key=api_key)
|
123 |
for model in self.models:
|
|
|
127 |
{
|
128 |
"role": "system",
|
129 |
"content": """
|
130 |
+
You are an expert agent, and your task is to detect the language.
|
131 |
+
Return a JSON: {'detected_language': 'urdu' or 'english'}
|
132 |
"""
|
133 |
},
|
134 |
{
|
135 |
"role": "user",
|
136 |
+
"content": f"Detect the language for: {question}"
|
137 |
}
|
138 |
],
|
139 |
model=model,
|
140 |
+
max_tokens=256,
|
141 |
response_format={"type": "json_object"},
|
142 |
)
|
143 |
response = json.loads(chat_completion.choices[0].message.content)
|
144 |
+
return response['detected_language'].lower()
|
145 |
+
except Exception as e:
|
146 |
+
print(f"Language detection error: {e}")
|
147 |
time.sleep(2)
|
148 |
continue
|
149 |
+
return "english"
|
150 |
+
|
151 |
+
def translate_urdu(self, text):
|
152 |
+
try:
|
153 |
+
return GoogleTranslator(source='ur', target='en').translate(text)
|
154 |
+
except:
|
155 |
+
return text
|
156 |
|
157 |
def response(self, question, chat_id):
|
158 |
chat_history = self.load_chat(chat_id)
|
159 |
+
for entry in chat_history:
|
160 |
+
self.memory.save_context({"input": entry["question"]}, {"output": entry["answer"]})
|
161 |
|
162 |
language = self.detect_language(question)
|
163 |
|
164 |
if language == 'urdu':
|
165 |
+
question_translation = self.translate_urdu(question)
|
166 |
context = self.faiss_retriever.invoke(question_translation)
|
167 |
else:
|
168 |
context = self.faiss_retriever.invoke(question)
|