Spaces:
Runtime error
Runtime error
Update chatbot.py
Browse files- chatbot.py +114 -29
chatbot.py
CHANGED
|
@@ -6,6 +6,7 @@ from deep_translator import GoogleTranslator
|
|
| 6 |
from langchain_community.document_loaders.csv_loader import CSVLoader
|
| 7 |
from langchain_community.vectorstores import FAISS
|
| 8 |
import time
|
|
|
|
| 9 |
|
| 10 |
class Comsatsbot:
|
| 11 |
def __init__(self, hf, llm, api_keys, chats_collection, paths, index_path='faiss_kb'):
|
|
@@ -86,44 +87,28 @@ class Comsatsbot:
|
|
| 86 |
self.chats_collection.delete_one({"_id": chat_id})
|
| 87 |
return "success"
|
| 88 |
|
| 89 |
-
def generate_response(self, question, history, context
|
| 90 |
|
| 91 |
-
|
| 92 |
-
language = 'Urdu'
|
| 93 |
-
else:
|
| 94 |
-
language = 'English'
|
| 95 |
-
while True:
|
| 96 |
-
for api_key in self.api_keys:
|
| 97 |
-
self.client = Groq(api_key=api_key)
|
| 98 |
-
|
| 99 |
-
for model in self.models:
|
| 100 |
-
try:
|
| 101 |
-
chat_completion = self.client.chat.completions.create(
|
| 102 |
-
messages=[
|
| 103 |
-
{
|
| 104 |
-
"role": "user",
|
| 105 |
-
"content": f'''
|
| 106 |
Kindly use the proper emojis where we need to use in responses.
|
| 107 |
You are an comsats assistant to help the user related to comsats university, conversation, studies related query. Your answer should be very concise and to the point short answer. Dont need to repeat irrelevant text.
|
| 108 |
Answer the following Question: {question}
|
| 109 |
Kindly use the proper emojis where we need to use in responses.
|
| 110 |
Kindly generate a concise and to the point answer. Kindly answer the question from the provided context and if you dont find the answer from chat histoy and context then inform the user i dont know and dont make the answer from yourself.
|
| 111 |
Dont need to mention that (according to provided context/Based on the provided chat history) in response and just generate the response just like human without mention this according to context and chat history.
|
| 112 |
-
You are a conversational and helpfull agent to help the comsats university attock campus students, and your task is to provide concise and direct answers to the questions
|
| 113 |
-
Your task is to provide concise, direct answers to questions asked in {language}. Kindly respond correctly and to the point in {language}. Avoid irrelevant explanations and ensure proper use of {language} in your answers.
|
| 114 |
Your task is to use the emoji when there is happy, sad, surprise, angry expression required in response. Kindly analyze the question and if expression is required in response then use the emoji otherwise dont use the emoji and remember that you dont need to use the emoji in simple studies and comstas related question.
|
| 115 |
For example user ask same question again and again, user is not understanding anything, user asking wrong things etc then your task is to use the emoji in such response and be choosy in using emoji in response in happy sad angry response according to user question.
|
| 116 |
If there is any need to provide the url and then kindly generate the url according to following structure. Kindly provide the link clickable. Your provided should be generated according to following structure
|
| 117 |
[Click here to visit "website name"](website url "https://comsats.edu.pk" write the same url as it is provided in context below and dont use the www in url and use the same link url as it provided in context(admssion detail: (http://admissions.comsats.edu.pk/Home/EligibilityCriteria?pt=BS)))
|
| 118 |
-
Dont need to explain and repeat the prompt in response.
|
| 119 |
-
1.
|
| 120 |
2. Dont need to explain irrelevant explanation and use the proper emojis in answer if required in responses.
|
| 121 |
3. Always respond in a human-like tone and keep your answers concise, to the point and friendly.
|
| 122 |
4. If the question is conversational (like greetings, need any converstaion, help related studies, knowledge base question etc), respond in a warm, conversational tone.
|
| 123 |
5. Always consider the provided context and chat history to formulate your response.
|
| 124 |
6. If you don’t know the answer to the provided question or you did not find the answer from the context and chat history, kindly respond with "I don't know the answer to this.emooji" without adding irrelevant text explanations.
|
| 125 |
7. Kindly generate a perfect and to the point and short answer. Dont use any irrelevant text explanation and i want full concise and to the point answer.
|
| 126 |
-
8. Kindly answer in {language} and dont need to generate a response in other language and i want answer in this language {language}.
|
| 127 |
Kindly use the proper emojis where we need to use in responses.
|
| 128 |
Question: {question}
|
| 129 |
Kindly answer the question from the provided context and if you dont find the answer from chat histoy and context then inform the user i dont know and dont make the answer from yourself.
|
|
@@ -141,7 +126,22 @@ class Comsatsbot:
|
|
| 141 |
{history}
|
| 142 |
Answer the following Question: {question}
|
| 143 |
'''
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
],
|
| 146 |
model=model,
|
| 147 |
max_tokens=1024,
|
|
@@ -156,6 +156,82 @@ class Comsatsbot:
|
|
| 156 |
|
| 157 |
return "Sorry, unable to provide an answer at this time."
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
|
| 161 |
def response(self, question, chat_id):
|
|
@@ -165,8 +241,8 @@ class Comsatsbot:
|
|
| 165 |
for entry in chat_history:
|
| 166 |
self.memory.save_context({"input": entry["question"]}, {"output": entry["answer"]})
|
| 167 |
|
| 168 |
-
language =
|
| 169 |
-
if language == '
|
| 170 |
question_translation = GoogleTranslator(source='ur', target='en').translate(question)
|
| 171 |
context = self.faiss_retriever.invoke(question_translation)
|
| 172 |
else:
|
|
@@ -175,12 +251,21 @@ class Comsatsbot:
|
|
| 175 |
all_content = ''
|
| 176 |
for document in context:
|
| 177 |
page_content = document.page_content
|
| 178 |
-
all_content += page_content + '\n'
|
| 179 |
-
|
| 180 |
-
answer = self.generate_response(question, self.memory.load_memory_variables({})['history'], all_content, language)
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
|
| 186 |
|
|
|
|
| 6 |
from langchain_community.document_loaders.csv_loader import CSVLoader
|
| 7 |
from langchain_community.vectorstores import FAISS
|
| 8 |
import time
|
| 9 |
+
import json
|
| 10 |
|
| 11 |
class Comsatsbot:
|
| 12 |
def __init__(self, hf, llm, api_keys, chats_collection, paths, index_path='faiss_kb'):
|
|
|
|
| 87 |
self.chats_collection.delete_one({"_id": chat_id})
|
| 88 |
return "success"
|
| 89 |
|
| 90 |
+
def generate_response(self, question, history, context):
|
| 91 |
|
| 92 |
+
prompt = f'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
Kindly use the proper emojis where we need to use in responses.
|
| 94 |
You are an comsats assistant to help the user related to comsats university, conversation, studies related query. Your answer should be very concise and to the point short answer. Dont need to repeat irrelevant text.
|
| 95 |
Answer the following Question: {question}
|
| 96 |
Kindly use the proper emojis where we need to use in responses.
|
| 97 |
Kindly generate a concise and to the point answer. Kindly answer the question from the provided context and if you dont find the answer from chat histoy and context then inform the user i dont know and dont make the answer from yourself.
|
| 98 |
Dont need to mention that (according to provided context/Based on the provided chat history) in response and just generate the response just like human without mention this according to context and chat history.
|
| 99 |
+
You are a conversational and helpfull agent to help the comsats university attock campus students, and your task is to provide concise and direct answers to the questions.
|
|
|
|
| 100 |
Your task is to use the emoji when there is happy, sad, surprise, angry expression required in response. Kindly analyze the question and if expression is required in response then use the emoji otherwise dont use the emoji and remember that you dont need to use the emoji in simple studies and comstas related question.
|
| 101 |
For example user ask same question again and again, user is not understanding anything, user asking wrong things etc then your task is to use the emoji in such response and be choosy in using emoji in response in happy sad angry response according to user question.
|
| 102 |
If there is any need to provide the url and then kindly generate the url according to following structure. Kindly provide the link clickable. Your provided should be generated according to following structure
|
| 103 |
[Click here to visit "website name"](website url "https://comsats.edu.pk" write the same url as it is provided in context below and dont use the www in url and use the same link url as it provided in context(admssion detail: (http://admissions.comsats.edu.pk/Home/EligibilityCriteria?pt=BS)))
|
| 104 |
+
Dont need to explain and repeat the prompt in response.
|
| 105 |
+
1. Kindly generate a full concise answer and if you dont find answer from context and chathistoyr then dont need to make the answer just answer it in i dont know.
|
| 106 |
2. Dont need to explain irrelevant explanation and use the proper emojis in answer if required in responses.
|
| 107 |
3. Always respond in a human-like tone and keep your answers concise, to the point and friendly.
|
| 108 |
4. If the question is conversational (like greetings, need any converstaion, help related studies, knowledge base question etc), respond in a warm, conversational tone.
|
| 109 |
5. Always consider the provided context and chat history to formulate your response.
|
| 110 |
6. If you don’t know the answer to the provided question or you did not find the answer from the context and chat history, kindly respond with "I don't know the answer to this.emooji" without adding irrelevant text explanations.
|
| 111 |
7. Kindly generate a perfect and to the point and short answer. Dont use any irrelevant text explanation and i want full concise and to the point answer.
|
|
|
|
| 112 |
Kindly use the proper emojis where we need to use in responses.
|
| 113 |
Question: {question}
|
| 114 |
Kindly answer the question from the provided context and if you dont find the answer from chat histoy and context then inform the user i dont know and dont make the answer from yourself.
|
|
|
|
| 126 |
{history}
|
| 127 |
Answer the following Question: {question}
|
| 128 |
'''
|
| 129 |
+
while True:
|
| 130 |
+
for api_key in self.api_keys:
|
| 131 |
+
self.client = Groq(api_key=api_key)
|
| 132 |
+
|
| 133 |
+
for model in self.models:
|
| 134 |
+
try:
|
| 135 |
+
chat_completion = self.client.chat.completions.create(
|
| 136 |
+
messages=[
|
| 137 |
+
{
|
| 138 |
+
"role": "system",
|
| 139 |
+
"content": prompt,
|
| 140 |
+
},
|
| 141 |
+
{
|
| 142 |
+
"role": "user",
|
| 143 |
+
"content": f"Answer the following question : {question}",
|
| 144 |
+
},
|
| 145 |
],
|
| 146 |
model=model,
|
| 147 |
max_tokens=1024,
|
|
|
|
| 156 |
|
| 157 |
return "Sorry, unable to provide an answer at this time."
|
| 158 |
|
| 159 |
+
def detect_language(self, question):
|
| 160 |
+
|
| 161 |
+
white True:
|
| 162 |
+
for api_key in self.api_keys:
|
| 163 |
+
self.client = Groq(api_key=api_key)
|
| 164 |
+
|
| 165 |
+
for model in self.models:
|
| 166 |
+
try:
|
| 167 |
+
chat_completion = self.client.chat.completions.create(
|
| 168 |
+
messages=[
|
| 169 |
+
{
|
| 170 |
+
"role": "system",
|
| 171 |
+
"content": """
|
| 172 |
+
You are an expert agent and your task is to detect the language from the following provided question. Your generated output should be according to following json format.
|
| 173 |
+
Json Output Format:
|
| 174 |
+
{'detected_language': 'write the just detected language name: urdu, english '}
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
""",
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"role": "user",
|
| 182 |
+
"content": f"detect the language for the following Question: {question}",
|
| 183 |
+
},
|
| 184 |
+
],
|
| 185 |
+
model=model,
|
| 186 |
+
max_tokens=512,
|
| 187 |
+
response_format={"type": "json_object"},
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
response_content = chat_completion.choices[0].message.content
|
| 191 |
+
json_response = json.loads(response_content)
|
| 192 |
+
return json_response['detected_language'].lower()
|
| 193 |
+
except Exception as e:
|
| 194 |
+
time.sleep(2)
|
| 195 |
+
|
| 196 |
+
continue
|
| 197 |
+
def translate_urdu(self, text):
|
| 198 |
+
|
| 199 |
+
white True:
|
| 200 |
+
for api_key in self.api_keys:
|
| 201 |
+
self.client = Groq(api_key=api_key)
|
| 202 |
+
|
| 203 |
+
for model in self.models:
|
| 204 |
+
try:
|
| 205 |
+
chat_completion = self.client.chat.completions.create(
|
| 206 |
+
messages=[
|
| 207 |
+
{
|
| 208 |
+
"role": "system",
|
| 209 |
+
"content": """
|
| 210 |
+
You are an expert agent and your task is to translayte the following text in in proper urdu text and i want corrected urdu and dont need to add irrelevant text. Your generated output should be according to following json format.
|
| 211 |
+
Json Output Format:
|
| 212 |
+
{'text': 'write the just translated urdu text here and dont need to add irrelevant text'}
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
""",
|
| 217 |
+
},
|
| 218 |
+
{
|
| 219 |
+
"role": "user",
|
| 220 |
+
"content": f"detect the language for the following Question: {text}",
|
| 221 |
+
},
|
| 222 |
+
],
|
| 223 |
+
model=model,
|
| 224 |
+
max_tokens=512,
|
| 225 |
+
response_format={"type": "json_object"},
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
response_content = chat_completion.choices[0].message.content
|
| 229 |
+
json_response = json.loads(response_content)
|
| 230 |
+
return json_response['text']
|
| 231 |
+
except Exception as e:
|
| 232 |
+
time.sleep(2)
|
| 233 |
+
|
| 234 |
+
continue
|
| 235 |
|
| 236 |
|
| 237 |
def response(self, question, chat_id):
|
|
|
|
| 241 |
for entry in chat_history:
|
| 242 |
self.memory.save_context({"input": entry["question"]}, {"output": entry["answer"]})
|
| 243 |
|
| 244 |
+
language = self.detect_language(question)
|
| 245 |
+
if language == 'urdu':
|
| 246 |
question_translation = GoogleTranslator(source='ur', target='en').translate(question)
|
| 247 |
context = self.faiss_retriever.invoke(question_translation)
|
| 248 |
else:
|
|
|
|
| 251 |
all_content = ''
|
| 252 |
for document in context:
|
| 253 |
page_content = document.page_content
|
| 254 |
+
all_content += page_content + '\n'
|
| 255 |
+
answer = self.generate_response(question, self.memory.load_memory_variables({})['history'], all_content)
|
|
|
|
| 256 |
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
if language == 'urdu':
|
| 260 |
+
translated_answer = self.translate_urdu(answer)
|
| 261 |
+
self.update_chat(chat_id, question, answer)
|
| 262 |
+
return translated_answer
|
| 263 |
+
else:
|
| 264 |
+
self.update_chat(chat_id, question, answer)
|
| 265 |
+
return answer
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
|
| 269 |
|
| 270 |
|
| 271 |
|