Sadiaa commited on
Commit
60011de
·
verified ·
1 Parent(s): 571bbe3

Update chatbot.py

Browse files
Files changed (1) hide show
  1. 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 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:
@@ -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 7984%, 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