Sadiaa commited on
Commit
b600e93
·
verified ·
1 Parent(s): a9cc6a5

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +94 -51
chatbot.py CHANGED
@@ -10,21 +10,22 @@ from deep_translator import GoogleTranslator
10
 
11
 
12
  class Comsatsbot:
13
- def __init__(self, hf, chats_collection):
14
- self.api_keys = ["sk-live-1234567890abcdef"] # ✅ replace with your actual Groq key if needed
15
- self.llm = ChatOpenAI(
16
- openai_api_key="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",
17
- model="gpt-3.5-turbo"
18
- )
19
  self.client = None
20
- self.models = ["llama-3.1-70b-versatile"]
 
 
 
 
21
  self.memory = ConversationBufferMemory(llm=self.llm, max_token_limit=3000)
22
  self.chats_collection = chats_collection
23
- self.index_path = "faiss_kb"
24
  self.hf = hf
25
- self.paths = ["english_data.csv", "urdu_data.csv"] # ✅ actual dataset paths
26
  self.faiss_index = None
27
  self.faiss_retriever = None
 
28
  self.initialize_faiss_index()
29
 
30
  def load_data(self, paths):
@@ -50,7 +51,10 @@ class Comsatsbot:
50
  return None
51
 
52
  def create_chat_record(self, chat_id):
53
- self.chats_collection.insert_one({"_id": chat_id, "history": []})
 
 
 
54
 
55
  def update_chat(self, chat_id, question, answer):
56
  self.chats_collection.update_one(
@@ -77,46 +81,91 @@ class Comsatsbot:
77
  return "success"
78
 
79
  def generate_response(self, question, history, context):
80
- formatted_history = "\n".join([f"User: {h['question']}\nBot: {h['answer']}" for h in history])
81
- context_text = "\n".join([doc.page_content for doc in context])
82
-
83
  prompt = f'''
84
- You are a comsats assistant. Answer concisely and with emojis only where appropriate.
85
- Don't mention context/history explicitly. Use friendly tone and only say "I don’t know" if no relevant answer is found.
86
- University Info:
87
- Comsats Attock Campus offers CS, SE, AI, English, Math, EE, CE, BBA.
88
- Departments: CS, Math, EE. Facilities: grounds, canteens, mosque, LT rooms, labs.
89
- Admissions via NTS. CGPA: 4.0 (85%), 3.66 (79-84%), etc.
90
- Context:
91
- {context_text}
92
- Chat History:
93
- {formatted_history}
 
 
 
 
 
 
 
 
 
 
94
  Question: {question}
 
 
 
 
 
 
 
 
 
 
 
 
95
  '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
 
97
  for api_key in self.api_keys:
98
  self.client = Groq(api_key=api_key)
99
  for model in self.models:
100
  try:
101
  chat_completion = self.client.chat.completions.create(
102
  messages=[
103
- {"role": "system", "content": prompt},
104
- {"role": "user", "content": f"Answer the following question: {question}"}
 
 
 
 
 
 
 
 
 
105
  ],
106
  model=model,
107
- max_tokens=1024,
 
108
  )
109
- result = chat_completion.choices[0].message.content
110
- print("Returning answer:", result)
111
- return result
112
- except Exception as e:
113
- print(f"Error with {model}: {e}")
114
  time.sleep(2)
115
  continue
 
116
 
117
- return "Sorry 😔 I'm currently facing technical issues. Please try again later."
118
-
119
- def detect_language(self, question):
120
  for api_key in self.api_keys:
121
  self.client = Groq(api_key=api_key)
122
  for model in self.models:
@@ -126,47 +175,41 @@ class Comsatsbot:
126
  {
127
  "role": "system",
128
  "content": """
129
- You are an expert agent, and your task is to detect the language.
130
- Return a JSON: {'detected_language': 'urdu' or 'english'}
131
  """
132
  },
133
  {
134
  "role": "user",
135
- "content": f"Detect the language for: {question}"
136
  }
137
  ],
138
  model=model,
139
- max_tokens=256,
140
  response_format={"type": "json_object"},
141
  )
142
  response = json.loads(chat_completion.choices[0].message.content)
143
- return response['detected_language'].lower()
144
- except Exception as e:
145
- print(f"Language detection error: {e}")
146
  time.sleep(2)
147
  continue
148
- return "english"
149
-
150
- def translate_urdu(self, text):
151
- try:
152
- return GoogleTranslator(source='ur', target='en').translate(text)
153
- except:
154
- return text
155
 
156
  def response(self, question, chat_id):
157
  chat_history = self.load_chat(chat_id)
 
158
  for entry in chat_history:
159
  self.memory.save_context({"input": entry["question"]}, {"output": entry["answer"]})
160
 
161
  language = self.detect_language(question)
162
 
163
  if language == 'urdu':
164
- question_translation = self.translate_urdu(question)
165
  context = self.faiss_retriever.invoke(question_translation)
166
  else:
167
  context = self.faiss_retriever.invoke(question)
168
 
169
  answer = self.generate_response(question, chat_history, context)
170
- self.update_chat(chat_id, question, answer)
171
- return answer
172
 
 
 
 
10
 
11
 
12
  class Comsatsbot:
13
+ def __init__(self, hf, llm, api_keys, chats_collection, paths, index_path='faiss_kb'):
14
+ self.llm = llm
15
+ self.api_keys = api_keys
 
 
 
16
  self.client = None
17
+ self.models = [
18
+ "llama3-groq-70b-8192-tool-use-preview",
19
+ "llama-3.1-70b-versatile",
20
+ "llama3-70b-8192"
21
+ ]
22
  self.memory = ConversationBufferMemory(llm=self.llm, max_token_limit=3000)
23
  self.chats_collection = chats_collection
24
+ self.index_path = index_path
25
  self.hf = hf
 
26
  self.faiss_index = None
27
  self.faiss_retriever = None
28
+ self.paths = paths
29
  self.initialize_faiss_index()
30
 
31
  def load_data(self, paths):
 
51
  return None
52
 
53
  def create_chat_record(self, chat_id):
54
+ self.chats_collection.insert_one({
55
+ "_id": chat_id,
56
+ "history": []
57
+ })
58
 
59
  def update_chat(self, chat_id, question, answer):
60
  self.chats_collection.update_one(
 
81
  return "success"
82
 
83
  def generate_response(self, question, history, context):
 
 
 
84
  prompt = f'''
85
+ Kindly use the proper emojis where we need to use in responses.
86
+ You are a comsats assistant to help the user with comsats university-related queries. Your answer should be very concise and to the point with a short answer. Don't repeat irrelevant text.
87
+ Answer the following Question: {question}
88
+ Kindly use the proper emojis where we need to use in responses.
89
+ Kindly generate a concise and to-the-point answer. Kindly answer the question from the provided context, and if you don't find the answer from chat history and context, then inform the user "I don't know" and don't make the answer up yourself.
90
+ Don't mention "according to provided context/Based on the provided chat history" in the response, just generate the response like a human without mentioning context and chat history.
91
+ You are a conversational and helpful agent to help the comsats university attock campus students, and your task is to provide concise and direct answers to the questions.
92
+ Your task is to use the emoji when there is a happy, sad, surprise, or angry expression required in the response. Kindly analyze the question and if an expression is required in response, then use the emoji. Otherwise, don't use the emoji, and remember that you don't need to use the emoji in simple studies and comsats-related questions.
93
+ For example, if the user asks the same question again and again, doesn't understand anything, asks wrong questions, etc., then use the emoji in such responses. Be choosy when using emojis in responses according to the user's question.
94
+ If there is any need to provide a URL, generate the URL according to the following structure. Kindly provide the link clickable. Your provided link should be generated according to this structure:
95
+ [Click here to visit "website name"](website url "https://comsats.edu.pk") (Write the same URL as it is provided in the context below and don't use "www" in the URL.)
96
+ Don't explain or repeat the prompt in the response.
97
+ 1. Kindly generate a full concise answer and if you don't find the answer from context and chat history, then don’t make the answer, just say "I don’t know."
98
+ 2. Don’t explain irrelevant explanations and use the proper emojis in the answer if required.
99
+ 3. Always respond in a human-like tone and keep your answers concise, to the point, and friendly.
100
+ 4. If the question is conversational (like greetings, need any conversation, help related to studies, knowledge-based question, etc.), respond in a warm, conversational tone.
101
+ 5. Always consider the provided context and chat history to formulate your response.
102
+ 6. If you don’t know the answer to the provided question or you didn’t find the answer from the context and chat history, kindly respond with "I don’t know the answer to this 😔" without adding irrelevant text explanations.
103
+ 7. Kindly generate a perfect and to-the-point short answer. Don’t use any irrelevant text explanation and I want a full concise and to-the-point answer.
104
+ Kindly use the proper emojis where we need to use in responses.
105
  Question: {question}
106
+ Kindly answer the question from the provided context, and if you don’t find the answer from chat history and context, then inform the user "I don’t know" and don’t make the answer yourself.
107
+ Use the following context to answer and don’t mention that you are answering from this context:
108
+ 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.
109
+ It has three departments: CS (CS, AI, SE), Math (Math, BBA, English), and EE (EE, CE).
110
+ It has a cricket ground, football ground, and two canteens. The first canteen is near the Math and EE departments, and the second canteen is near the CS department. There is also a mosque near the CS department. The CS department has theater-like rooms (LT), and there are a total of 9 theaters called LT. The Math department has classrooms (CR), and the EE department has labs.
111
+ They accept the NTS test for admission and provide the CGPA for 4 on 85 percent, and 3.66 between 79 to 84, and many more.
112
+ {context}
113
+ Context is ending.
114
+ Now here is the chat history that you have to consider and identify
115
+ **Consider the following chat history for additional context to answer the question:**
116
+ {history}
117
+ Answer the following Question: {question}
118
  '''
119
+ while True:
120
+ for api_key in self.api_keys:
121
+ self.client = Groq(api_key=api_key)
122
+ for model in self.models:
123
+ try:
124
+ chat_completion = self.client.chat.completions.create(
125
+ messages=[
126
+ {"role": "system", "content": prompt},
127
+ {"role": "user", "content": f"Answer the following question: {question}"}
128
+ ],
129
+ model=model,
130
+ max_tokens=1024,
131
+ )
132
+ return chat_completion.choices[0].message.content
133
+ except Exception:
134
+ time.sleep(2)
135
+ continue
136
+ return "Sorry, unable to provide an answer at this time."
137
 
138
+ def detect_language(self, question):
139
  for api_key in self.api_keys:
140
  self.client = Groq(api_key=api_key)
141
  for model in self.models:
142
  try:
143
  chat_completion = self.client.chat.completions.create(
144
  messages=[
145
+ {
146
+ "role": "system",
147
+ "content": """
148
+ You are an expert agent, and your task is to detect the language.
149
+ Return a JSON: {'detected_language': 'urdu' or 'english'}
150
+ """
151
+ },
152
+ {
153
+ "role": "user",
154
+ "content": f"Detect the language for: {question}"
155
+ }
156
  ],
157
  model=model,
158
+ max_tokens=256,
159
+ response_format={"type": "json_object"},
160
  )
161
+ response = json.loads(chat_completion.choices[0].message.content)
162
+ return response['detected_language'].lower()
163
+ except Exception:
 
 
164
  time.sleep(2)
165
  continue
166
+ return "english"
167
 
168
+ def translate_urdu(self, text):
 
 
169
  for api_key in self.api_keys:
170
  self.client = Groq(api_key=api_key)
171
  for model in self.models:
 
175
  {
176
  "role": "system",
177
  "content": """
178
+ Translate the following text into proper Urdu. Return a JSON:
179
+ {'text': 'translated urdu text'}
180
  """
181
  },
182
  {
183
  "role": "user",
184
+ "content": f"Translate this: {text}"
185
  }
186
  ],
187
  model=model,
188
+ max_tokens=512,
189
  response_format={"type": "json_object"},
190
  )
191
  response = json.loads(chat_completion.choices[0].message.content)
192
+ return response['text']
193
+ except Exception:
 
194
  time.sleep(2)
195
  continue
196
+ return text
 
 
 
 
 
 
197
 
198
  def response(self, question, chat_id):
199
  chat_history = self.load_chat(chat_id)
200
+
201
  for entry in chat_history:
202
  self.memory.save_context({"input": entry["question"]}, {"output": entry["answer"]})
203
 
204
  language = self.detect_language(question)
205
 
206
  if language == 'urdu':
207
+ question_translation = GoogleTranslator(source='ur', target='en').translate(question)
208
  context = self.faiss_retriever.invoke(question_translation)
209
  else:
210
  context = self.faiss_retriever.invoke(question)
211
 
212
  answer = self.generate_response(question, chat_history, context)
 
 
213
 
214
+ self.update_chat(chat_id, question, answer)
215
+ return answer