Spaces:
Sleeping
Sleeping
Update chatbot.py
Browse files- chatbot.py +52 -82
chatbot.py
CHANGED
@@ -1,17 +1,18 @@
|
|
1 |
import os
|
2 |
from groq import Groq
|
3 |
-
from langchain.memory import
|
4 |
from langdetect import detect
|
5 |
from deep_translator import GoogleTranslator
|
6 |
from langchain_community.document_loaders.csv_loader import CSVLoader
|
7 |
from langchain_community.vectorstores import FAISS
|
|
|
8 |
class Comsatsbot:
|
9 |
-
def
|
10 |
self.llm = llm
|
11 |
self.client = Groq(api_key=api_key)
|
12 |
|
13 |
# Initialize memory buffer and MongoDB connection
|
14 |
-
self.memory =
|
15 |
self.chats_collection = chats_collection
|
16 |
self.index_path = index_path
|
17 |
self.hf = hf
|
@@ -82,86 +83,58 @@ class Comsatsbot:
|
|
82 |
return "success"
|
83 |
|
84 |
def generate_response(self, question, history, context, detected_language):
|
|
|
85 |
if detected_language == 'ur':
|
86 |
language = 'Urdu'
|
87 |
-
|
88 |
-
chat_completion = self.client.chat.completions.create(
|
89 |
-
messages=[
|
90 |
-
{
|
91 |
-
"role": "user",
|
92 |
-
"content": f'''
|
93 |
-
|
94 |
-
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 asked in {language} Language. kindly answer in correct way and to the point in {language} language.
|
95 |
-
Dont need to explain irrelevant thinga nd use the correct {language} in response.
|
96 |
-
Please follow these guidelines when answering:
|
97 |
-
Dont need to explain and repeat the prompt in response. Answer in {language} language.
|
98 |
-
|
99 |
-
1. If the question is in {language}, answer in {language}.
|
100 |
-
2. Dont need to explain irrelevant explanation and use the proper emojis in answer.
|
101 |
-
3. Always respond in a **human-like tone** and keep your answers concise, to the point, and friendly.
|
102 |
-
4. If the question is **conversational** (like greetings, need any converstaion etc), respond in a warm, conversational tone and use appropriate **emojis**.
|
103 |
-
5. Always consider the provided **context** and **chat history** to formulate your response.
|
104 |
-
6. If you don’t know the answer to the question or you did not find the answer from the context, kindly respond with "I don't know the answer to this." without adding irrelevant explanations.
|
105 |
-
7. KIndly generate a perfect and to the point answer with the proper use of emojis. Dont use any irrelevant text explanation and i want full concise and to the oint answer.
|
106 |
-
8. Kindly answer in {language} and dont need to generate a response in other language and i want answer in this language {language}.
|
107 |
-
|
108 |
-
**Question:** {question}
|
109 |
-
|
110 |
-
**Use the following context to answer:**
|
111 |
-
Comsats Attock Campus Provide BSomputerScience, BSSoftwareEngineer BSArtificialIntelligence BSEnglish BSmath BSElectricalEngineering BSComputerEngineering BSBBA
|
112 |
-
Has three departments CS(CS, AI, SE), Math(math, BBA, english) and EE(EE, CE).
|
113 |
-
It has cricket ground and football ground and two canteens. First near math and ee department and second near cs department. There is also mosque near cs department. CS department has threater liker rooms lt and total 9 theaters called lt and math has classroom cr and ee has labs.
|
114 |
-
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.
|
115 |
-
{context}
|
116 |
-
|
117 |
-
**Consider the following chat history for additional context to answer the question:**
|
118 |
-
{history}
|
119 |
-
'''
|
120 |
-
}
|
121 |
-
],
|
122 |
-
model="llama3-70b-8192",
|
123 |
-
)
|
124 |
else:
|
125 |
language = 'English'
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
{context}
|
154 |
-
|
155 |
-
**Consider the following chat history for additional context to answer the question:**
|
156 |
-
{history}
|
157 |
-
'''
|
158 |
-
}
|
159 |
-
],
|
160 |
-
model="llama3-70b-8192",
|
161 |
-
)
|
162 |
-
|
163 |
-
response_content = chat_completion.choices[0].message.content
|
164 |
-
return response_content
|
165 |
|
166 |
def response(self, question, chat_id):
|
167 |
chat_history = self.load_chat(chat_id)
|
@@ -185,7 +158,4 @@ class Comsatsbot:
|
|
185 |
answer = self.generate_response(question, self.memory.load_memory_variables({})['history'], all_content, language)
|
186 |
|
187 |
self.update_chat(chat_id, question, answer)
|
188 |
-
return answer
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
1 |
import os
|
2 |
from groq import Groq
|
3 |
+
from langchain.memory import ConversationTokenBufferMemory
|
4 |
from langdetect import detect
|
5 |
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 |
class Comsatsbot:
|
10 |
+
def _init_(self, hf, llm, api_key, chats_collection, paths, index_path='faiss_kb'):
|
11 |
self.llm = llm
|
12 |
self.client = Groq(api_key=api_key)
|
13 |
|
14 |
# Initialize memory buffer and MongoDB connection
|
15 |
+
self.memory = ConversationTokenBufferMemory(llm=self.llm, max_token_limit=4000)
|
16 |
self.chats_collection = chats_collection
|
17 |
self.index_path = index_path
|
18 |
self.hf = hf
|
|
|
83 |
return "success"
|
84 |
|
85 |
def generate_response(self, question, history, context, detected_language):
|
86 |
+
models = ["llama3-groq-70b-8192-tool-use-preview", "llama-3.1-70b-versatile", "llama3-70b-8192", "mixtral-8x7b-32768", "gemma2-9b-it"]
|
87 |
if detected_language == 'ur':
|
88 |
language = 'Urdu'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
else:
|
90 |
language = 'English'
|
91 |
+
while True:
|
92 |
+
for model in models:
|
93 |
+
try:
|
94 |
+
chat_completion = self.client.chat.completions.create(
|
95 |
+
messages=[
|
96 |
+
{
|
97 |
+
"role": "user",
|
98 |
+
"content": f'''
|
99 |
+
|
100 |
+
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 asked in {language} Language. kindly answer in correct way and to the point in {language} language.
|
101 |
+
Dont need to explain irrelevant things and use the correct {language} in response.
|
102 |
+
Kindly genrate emojis when user is asking questions (funny, happy, sad etc) then add the anger, sad, smile, happy, surprise and sleeping emojis when it is required in conversation. Use these kind of emojis when needed it in response otherwise dont use the emojis in response.
|
103 |
+
Please follow these guidelines when answering:
|
104 |
+
Dont need to explain and repeat the prompt in response. Answer in {language} language.
|
105 |
+
|
106 |
+
1. If the question is in {language}, answer in {language}.
|
107 |
+
2. Dont need to explain irrelevant explanation and use the proper emojis in answer if required in response.
|
108 |
+
3. Always respond in a *human-like tone* and keep your answers concise, to the point, and friendly.
|
109 |
+
4. If the question is *conversational* (like greetings, need any converstaion etc), respond in a warm, conversational tone.
|
110 |
+
5. Always consider the provided *context* and *chat history* to formulate your response.
|
111 |
+
6. If you don’t know the answer to the question or you did not find the answer from the context, kindly respond with "I don't know the answer to this." without adding irrelevant explanations.
|
112 |
+
7. KIndly generate a perfect and to the point answer. Dont use any irrelevant text explanation and i want full concise and to the point answer.
|
113 |
+
8. Kindly answer in {language} and dont need to generate a response in other language and i want answer in this language {language}.
|
114 |
+
|
115 |
+
*Question:* {question}
|
116 |
|
117 |
+
*Use the following context to answer:*
|
118 |
+
Comsats Attock Campus Provide BSomputerScience, BSSoftwareEngineer BSArtificialIntelligence BSEnglish BSmath BSElectricalEngineering BSComputerEngineering BSBBA
|
119 |
+
Has three departments CS(CS, AI, SE), Math(math, BBA, english) and EE(EE, CE).
|
120 |
+
It has cricket ground and football ground and two canteens. First near math and ee department and second near cs department. There is also mosque near cs department. CS department has threater liker rooms lt and total 9 theaters called lt and math has classroom cr and ee has labs.
|
121 |
+
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.
|
122 |
+
{context}
|
123 |
+
|
124 |
+
*Consider the following chat history for additional context to answer the question:*
|
125 |
+
{history}
|
126 |
+
'''
|
127 |
+
}
|
128 |
+
],
|
129 |
+
model=model,
|
130 |
+
)
|
131 |
+
|
132 |
+
response_content = chat_completion.choices[0].message.content
|
133 |
+
return response_content
|
134 |
+
except Exception as e:
|
135 |
+
time.sleep(2)
|
136 |
+
continue
|
137 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
def response(self, question, chat_id):
|
140 |
chat_history = self.load_chat(chat_id)
|
|
|
158 |
answer = self.generate_response(question, self.memory.load_memory_variables({})['history'], all_content, language)
|
159 |
|
160 |
self.update_chat(chat_id, question, answer)
|
161 |
+
return answer
|
|
|
|
|
|