Spaces:
Running
Running
Update chatbot.py
Browse files- chatbot.py +41 -38
chatbot.py
CHANGED
@@ -80,49 +80,52 @@ class Comsatsbot:
|
|
80 |
self.chats_collection.delete_one({"_id": chat_id})
|
81 |
return "success"
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
def generate_response(self, question, history, context):
|
84 |
-
prompt =
|
85 |
-
|
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 |
],
|
@@ -171,7 +174,7 @@ class Comsatsbot:
|
|
171 |
for model in self.models:
|
172 |
try:
|
173 |
chat_completion = self.client.chat.completions.create(
|
174 |
-
messages=[
|
175 |
{
|
176 |
"role": "system",
|
177 |
"content": """
|
@@ -212,4 +215,4 @@ class Comsatsbot:
|
|
212 |
answer = self.generate_response(question, chat_history, context)
|
213 |
|
214 |
self.update_chat(chat_id, question, answer)
|
215 |
-
return answer
|
|
|
80 |
self.chats_collection.delete_one({"_id": chat_id})
|
81 |
return "success"
|
82 |
|
83 |
+
def get_system_prompt(self):
|
84 |
+
return """
|
85 |
+
You are a comsats assistant to help the user with comsats university-related queries. Your response should be concise, direct, and to the point. Avoid any unnecessary explanations. Always consider the provided context and chat history to generate the answer.
|
86 |
+
|
87 |
+
Use emojis only when required based on the user's tone and emotions. Do not overuse them. Here's when you should use emojis:
|
88 |
+
- **Happy emotions**: Use 😊 or 😄 when the user expresses satisfaction or asks for something positive.
|
89 |
+
- **Sad emotions**: Use 😔 when the user is asking about something disappointing or negative.
|
90 |
+
- **Surprise**: Use 😯 when the user expresses surprise.
|
91 |
+
- **Anger or frustration**: Use 😡 when the user expresses frustration or dissatisfaction.
|
92 |
+
|
93 |
+
If the user asks the same question repeatedly or asks an illogical question, feel free to use emojis to subtly convey frustration, confusion, or amusement.
|
94 |
+
|
95 |
+
Do not include the phrase "According to the provided context" or "Based on the chat history". Simply generate the answer like a human would, without referencing where the information comes from.
|
96 |
+
|
97 |
+
If the question requires a URL, format it like this:
|
98 |
+
[Click here to visit COMSATS](https://comsats.edu.pk).
|
99 |
+
|
100 |
+
Your task is to help students at COMSATS University, Attock campus, with their university-related queries. The following are key details about the university:
|
101 |
+
- Departments: CS, AI, SE, Math, BBA, EE, CE, English.
|
102 |
+
- Facilities: Cricket ground, football ground, two canteens (near CS and Math/EE), mosque near CS department, LT rooms in CS, classrooms in Math, and labs in EE.
|
103 |
+
- Admission: Accepts NTS test, CGPA requirements: 85% for CGPA 4.0, 79-84% for CGPA 3.66.
|
104 |
+
- Available degrees: BS Computer Science, BS Software Engineering, BS Artificial Intelligence, BS English, BS Math, BS Electrical Engineering, BS Computer Engineering, BS BBA.
|
105 |
+
|
106 |
+
Consider the following chat history for additional context to answer the question:
|
107 |
+
{history}
|
108 |
+
|
109 |
+
When answering:
|
110 |
+
- Answer in a conversational and friendly tone.
|
111 |
+
- Be concise and to the point, while still being helpful.
|
112 |
+
- If you don’t know the answer from the context or chat history, simply say "I don’t know the answer to this 😔".
|
113 |
+
|
114 |
+
Context ends here. Now, answer the following question:
|
115 |
+
|
116 |
+
{question}
|
117 |
+
"""
|
118 |
+
|
119 |
def generate_response(self, question, history, context):
|
120 |
+
prompt = self.get_system_prompt().format(question=question, history=history, context=context)
|
121 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
while True:
|
123 |
for api_key in self.api_keys:
|
124 |
self.client = Groq(api_key=api_key)
|
125 |
for model in self.models:
|
126 |
try:
|
127 |
chat_completion = self.client.chat.completions.create(
|
128 |
+
messages=[
|
129 |
{"role": "system", "content": prompt},
|
130 |
{"role": "user", "content": f"Answer the following question: {question}"}
|
131 |
],
|
|
|
174 |
for model in self.models:
|
175 |
try:
|
176 |
chat_completion = self.client.chat.completions.create(
|
177 |
+
messages=[
|
178 |
{
|
179 |
"role": "system",
|
180 |
"content": """
|
|
|
215 |
answer = self.generate_response(question, chat_history, context)
|
216 |
|
217 |
self.update_chat(chat_id, question, answer)
|
218 |
+
return answer
|