Spaces:
Sleeping
Sleeping
Update chatbot.py
Browse files- chatbot.py +48 -61
chatbot.py
CHANGED
@@ -81,6 +81,13 @@ class Comsatsbot:
|
|
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.
|
@@ -90,11 +97,11 @@ class Comsatsbot:
|
|
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
|
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
|
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.
|
@@ -105,34 +112,37 @@ class Comsatsbot:
|
|
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 |
-
|
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 |
-
|
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):
|
@@ -141,26 +151,25 @@ class Comsatsbot:
|
|
141 |
for model in self.models:
|
142 |
try:
|
143 |
chat_completion = self.client.chat.completions.create(
|
144 |
-
messages=[
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
},
|
152 |
{
|
153 |
-
|
154 |
-
|
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"
|
@@ -171,45 +180,23 @@ class Comsatsbot:
|
|
171 |
for model in self.models:
|
172 |
try:
|
173 |
chat_completion = self.client.chat.completions.create(
|
174 |
-
messages=[
|
|
|
|
|
|
|
|
|
|
|
175 |
{
|
176 |
-
|
177 |
-
|
178 |
-
|
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 =
|
192 |
-
return response
|
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
|
|
|
81 |
return "success"
|
82 |
|
83 |
def generate_response(self, question, history, context):
|
84 |
+
comsats_context = """
|
85 |
+
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.
|
86 |
+
It has three departments: CS (CS, AI, SE), Math (Math, BBA, English), and EE (EE, CE).
|
87 |
+
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.
|
88 |
+
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.
|
89 |
+
"""
|
90 |
+
|
91 |
prompt = f'''
|
92 |
Kindly use the proper emojis where we need to use in responses.
|
93 |
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.
|
|
|
97 |
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.
|
98 |
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.
|
99 |
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.
|
100 |
+
For example, if the user asks the same question again and again and 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.
|
101 |
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:
|
102 |
[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.)
|
103 |
Don't explain or repeat the prompt in the response.
|
104 |
+
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."
|
105 |
2. Don’t explain irrelevant explanations and use the proper emojis in the answer if required.
|
106 |
3. Always respond in a human-like tone and keep your answers concise, to the point, and friendly.
|
107 |
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.
|
|
|
112 |
Question: {question}
|
113 |
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.
|
114 |
Use the following context to answer and don’t mention that you are answering from this context:
|
115 |
+
{comsats_context}
|
|
|
|
|
|
|
|
|
116 |
Context is ending.
|
117 |
Now here is the chat history that you have to consider and identify
|
118 |
**Consider the following chat history for additional context to answer the question:**
|
119 |
{history}
|
120 |
Answer the following Question: {question}
|
121 |
'''
|
122 |
+
|
123 |
+
# Log context to check if it's passed correctly
|
124 |
+
print("Context being passed to the model:", comsats_context)
|
125 |
+
print("Question being asked:", question)
|
126 |
+
|
127 |
+
# Groq API call here
|
128 |
while True:
|
129 |
for api_key in self.api_keys:
|
130 |
self.client = Groq(api_key=api_key)
|
131 |
for model in self.models:
|
132 |
try:
|
133 |
chat_completion = self.client.chat.completions.create(
|
134 |
+
messages=[{"role": "system", "content": prompt},
|
135 |
+
{"role": "user", "content": f"Answer the following question: {question}"}],
|
|
|
|
|
136 |
model=model,
|
137 |
max_tokens=1024,
|
138 |
)
|
139 |
+
print("Response from Groq model:", chat_completion.choices[0].message.content)
|
140 |
return chat_completion.choices[0].message.content
|
141 |
+
except Exception as e:
|
142 |
+
print(f"Error encountered: {e}")
|
143 |
time.sleep(2)
|
144 |
continue
|
145 |
+
|
146 |
return "Sorry, unable to provide an answer at this time."
|
147 |
|
148 |
def detect_language(self, question):
|
|
|
151 |
for model in self.models:
|
152 |
try:
|
153 |
chat_completion = self.client.chat.completions.create(
|
154 |
+
messages=[{
|
155 |
+
"role": "system",
|
156 |
+
"content": """
|
157 |
+
You are an expert agent, and your task is to detect the language.
|
158 |
+
Return a JSON: {'detected_language': 'urdu' or 'english'}
|
159 |
+
"""
|
160 |
+
},
|
|
|
161 |
{
|
162 |
+
"role": "user",
|
163 |
+
"content": f"Detect the language for: {question}"
|
164 |
+
}],
|
|
|
165 |
model=model,
|
166 |
max_tokens=256,
|
167 |
response_format={"type": "json_object"},
|
168 |
)
|
169 |
response = json.loads(chat_completion.choices[0].message.content)
|
170 |
return response['detected_language'].lower()
|
171 |
+
except Exception as e:
|
172 |
+
print(f"Error encountered: {e}")
|
173 |
time.sleep(2)
|
174 |
continue
|
175 |
return "english"
|
|
|
180 |
for model in self.models:
|
181 |
try:
|
182 |
chat_completion = self.client.chat.completions.create(
|
183 |
+
messages=[{
|
184 |
+
"role": "system",
|
185 |
+
"content": """
|
186 |
+
Translate the following text into proper Urdu. Return a response without any extra explanation.
|
187 |
+
"""
|
188 |
+
},
|
189 |
{
|
190 |
+
"role": "user",
|
191 |
+
"content": f"Translate this into Urdu: {text}"
|
192 |
+
}],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
model=model,
|
194 |
max_tokens=512,
|
|
|
195 |
)
|
196 |
+
response = chat_completion.choices[0].message.content
|
197 |
+
return response
|
198 |
+
except Exception as e:
|
199 |
+
print(f"Error encountered: {e}")
|
200 |
time.sleep(2)
|
201 |
continue
|
202 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|