Spaces:
Running
Running
File size: 11,305 Bytes
4003576 1e2f5fb 2788b1e a9cc6a5 f5c686f 3af52a5 4003576 2788b1e 4003576 b600e93 a9cc6a5 b600e93 f5c686f 4003576 b600e93 4003576 b600e93 4003576 2788b1e 4003576 2788b1e 4003576 b600e93 4003576 2788b1e 4003576 195a6ed a3ed9cf 195a6ed b600e93 a3ed9cf b600e93 a3ed9cf b600e93 60011de b600e93 a3ed9cf b600e93 738595c a3ed9cf b600e93 a3ed9cf b600e93 a3ed9cf b600e93 a3ed9cf b600e93 a3ed9cf b600e93 738595c b600e93 a9cc6a5 a3ed9cf b600e93 a3ed9cf a9cc6a5 b600e93 a9cc6a5 b600e93 a3ed9cf a9cc6a5 b600e93 a9cc6a5 b600e93 a9cc6a5 a3ed9cf a9cc6a5 a3ed9cf a9cc6a5 b600e93 a9cc6a5 a3ed9cf a9cc6a5 a3ed9cf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
import os
import time
import json
from groq import Groq
from langchain.memory import ConversationBufferMemory
from langchain_openai import ChatOpenAI
from langchain_community.document_loaders import CSVLoader
from langchain_community.vectorstores import FAISS
from deep_translator import GoogleTranslator
class Comsatsbot:
def __init__(self, hf, llm, api_keys, chats_collection, paths, index_path='faiss_kb'):
self.llm = llm
self.api_keys = api_keys
self.client = None
self.models = [
"llama3-groq-70b-8192-tool-use-preview",
"llama-3.1-70b-versatile",
"llama3-70b-8192"
]
self.memory = ConversationBufferMemory(llm=self.llm, max_token_limit=3000)
self.chats_collection = chats_collection
self.index_path = index_path
self.hf = hf
self.faiss_index = None
self.faiss_retriever = None
self.paths = paths
self.initialize_faiss_index()
def load_data(self, paths):
documents = []
for path in paths:
loader = CSVLoader(file_path=path)
data = loader.load()
documents.extend(data)
return documents
def initialize_faiss_index(self):
if os.path.exists(self.index_path):
self.faiss_index = FAISS.load_local(self.index_path, self.hf, allow_dangerous_deserialization=True)
else:
documents = self.load_data(self.paths)
self.faiss_index = FAISS.from_documents(documents, self.hf)
self.faiss_index.save_local(self.index_path)
self.faiss_retriever = self.faiss_index.as_retriever(search_kwargs={"k": 5})
def retrieve_answer(self, query):
if self.faiss_retriever:
return self.faiss_retriever.invoke(query)
return None
def create_chat_record(self, chat_id):
self.chats_collection.insert_one({
"_id": chat_id,
"history": []
})
def update_chat(self, chat_id, question, answer):
self.chats_collection.update_one(
{"_id": chat_id},
{"$push": {"history": {"question": question, "answer": answer}}}
)
def load_chat(self, chat_id):
chat_record = self.chats_collection.find_one({"_id": chat_id})
if not chat_record:
raise KeyError(f"Chat ID {chat_id} does not exist.")
return chat_record.get('history', [])
def new_chat(self, chat_id):
if self.chats_collection.find_one({"_id": chat_id}):
raise KeyError(f"Chat ID {chat_id} exists already.")
self.create_chat_record(chat_id)
return "success"
def delete_chat(self, chat_id):
if not self.chats_collection.find_one({"_id": chat_id}):
raise KeyError(f"Chat ID {chat_id} does not exist.")
self.chats_collection.delete_one({"_id": chat_id})
return "success"
def generate_response(self, question, history, context):
comsats_context = """
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.
It has three departments: CS (CS, AI, SE), Math (Math, BBA, English), and EE (EE, CE).
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.
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.
"""
prompt = f'''
Kindly use the proper emojis where we need to use in responses.
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.
Answer the following Question: {question}
Kindly use the proper emojis where we need to use in responses.
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.
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.
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.
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.
For example, if the user asks the same question again and again and 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.
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:
[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.)
Don't explain or repeat the prompt in the response.
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."
2. Don’t explain irrelevant explanations and use the proper emojis in the answer if required.
3. Always respond in a human-like tone and keep your answers concise, to the point, and friendly.
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.
5. Always consider the provided context and chat history to formulate your response.
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.
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.
Kindly use the proper emojis where we need to use in responses.
Question: {question}
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.
Use the following context to answer and don’t mention that you are answering from this context:
{comsats_context}
Context is ending.
Now here is the chat history that you have to consider and identify
**Consider the following chat history for additional context to answer the question:**
{history}
Answer the following Question: {question}
'''
# Log context to check if it's passed correctly
print("Context being passed to the model:", comsats_context)
print("Question being asked:", question)
# Groq API call here
while True:
for api_key in self.api_keys:
self.client = Groq(api_key=api_key)
for model in self.models:
try:
chat_completion = self.client.chat.completions.create(
messages=[{"role": "system", "content": prompt},
{"role": "user", "content": f"Answer the following question: {question}"}],
model=model,
max_tokens=1024,
)
print("Response from Groq model:", chat_completion.choices[0].message.content)
return chat_completion.choices[0].message.content
except Exception as e:
print(f"Error encountered: {e}")
time.sleep(2)
continue
return "Sorry, unable to provide an answer at this time."
def detect_language(self, question):
for api_key in self.api_keys:
self.client = Groq(api_key=api_key)
for model in self.models:
try:
chat_completion = self.client.chat.completions.create(
messages=[{
"role": "system",
"content": """
You are an expert agent, and your task is to detect the language.
Return a JSON: {'detected_language': 'urdu' or 'english'}
"""
},
{
"role": "user",
"content": f"Detect the language for: {question}"
}],
model=model,
max_tokens=256,
response_format={"type": "json_object"},
)
response = json.loads(chat_completion.choices[0].message.content)
return response['detected_language'].lower()
except Exception as e:
print(f"Error encountered: {e}")
time.sleep(2)
continue
return "english"
def translate_urdu(self, text):
for api_key in self.api_keys:
self.client = Groq(api_key=api_key)
for model in self.models:
try:
chat_completion = self.client.chat.completions.create(
messages=[{
"role": "system",
"content": """
Translate the following text into proper Urdu. Return a response without any extra explanation.
"""
},
{
"role": "user",
"content": f"Translate this into Urdu: {text}"
}],
model=model,
max_tokens=512,
)
response = chat_completion.choices[0].message.content
return response
except Exception as e:
print(f"Error encountered: {e}")
time.sleep(2)
continue
return text |