Spaces:
Running
Running
Update chatbot.py
Browse files- chatbot.py +20 -38
chatbot.py
CHANGED
@@ -2,8 +2,6 @@ import os
|
|
2 |
import time
|
3 |
import json
|
4 |
from groq import Groq
|
5 |
-
from langchain.memory import ConversationBufferMemory
|
6 |
-
from langchain_openai import ChatOpenAI
|
7 |
from langchain_community.document_loaders import CSVLoader
|
8 |
from langchain_community.vectorstores import FAISS
|
9 |
from deep_translator import GoogleTranslator
|
@@ -19,13 +17,12 @@ class Comsatsbot:
|
|
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):
|
@@ -81,40 +78,30 @@ 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.
|
87 |
Answer the following Question: {question}
|
88 |
-
Kindly
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
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 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
Context
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
118 |
'''
|
119 |
while True:
|
120 |
for api_key in self.api_keys:
|
@@ -198,9 +185,6 @@ class Comsatsbot:
|
|
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':
|
@@ -210,8 +194,6 @@ class Comsatsbot:
|
|
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
|
216 |
|
217 |
-
|
|
|
2 |
import time
|
3 |
import json
|
4 |
from groq import Groq
|
|
|
|
|
5 |
from langchain_community.document_loaders import CSVLoader
|
6 |
from langchain_community.vectorstores import FAISS
|
7 |
from deep_translator import GoogleTranslator
|
|
|
17 |
"llama-3.1-70b-versatile",
|
18 |
"llama3-70b-8192"
|
19 |
]
|
|
|
20 |
self.chats_collection = chats_collection
|
21 |
self.index_path = index_path
|
22 |
self.hf = hf
|
23 |
+
self.paths = paths
|
24 |
self.faiss_index = None
|
25 |
self.faiss_retriever = None
|
|
|
26 |
self.initialize_faiss_index()
|
27 |
|
28 |
def load_data(self, paths):
|
|
|
78 |
return "success"
|
79 |
|
80 |
def generate_response(self, question, history, context):
|
81 |
+
formatted_history = "\n".join([f"User: {h['question']}\nBot: {h['answer']}" for h in history])
|
82 |
+
context_text = "\n".join([doc.page_content for doc in context])
|
83 |
+
|
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 generate a concise and to-the-point answer. Use the provided context and chat history. If you don’t find the answer, just say "I don’t know" and don’t make it up.
|
89 |
+
Use emojis when appropriate (e.g., confusion, repeated questions, happy, sad situations). Avoid using them in simple academic or factual responses.
|
90 |
+
Format URLs as: [Click here to visit "website name"](https://comsats.edu.pk)
|
91 |
+
|
92 |
+
University details:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
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.
|
94 |
+
Departments: CS (CS, AI, SE), Math (Math, BBA, English), EE (EE, CE).
|
95 |
+
Facilities: Cricket ground, football ground, two canteens (one near Math & EE, one near CS), mosque near CS, 9 lecture theaters (LT), Math classrooms (CR), EE labs.
|
96 |
+
NTS test accepted. CGPA system: 4.0 for 85%, 3.66 for 79–84%, etc.
|
97 |
+
|
98 |
+
Context:
|
99 |
+
{context_text}
|
100 |
+
|
101 |
+
Chat History:
|
102 |
+
{formatted_history}
|
103 |
+
|
104 |
+
Question: {question}
|
105 |
'''
|
106 |
while True:
|
107 |
for api_key in self.api_keys:
|
|
|
185 |
def response(self, question, chat_id):
|
186 |
chat_history = self.load_chat(chat_id)
|
187 |
|
|
|
|
|
|
|
188 |
language = self.detect_language(question)
|
189 |
|
190 |
if language == 'urdu':
|
|
|
194 |
context = self.faiss_retriever.invoke(question)
|
195 |
|
196 |
answer = self.generate_response(question, chat_history, context)
|
|
|
197 |
self.update_chat(chat_id, question, answer)
|
198 |
return answer
|
199 |
|
|