Sadiaa commited on
Commit
b0f4734
·
verified ·
1 Parent(s): e9b28b0

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +18 -60
chatbot.py CHANGED
@@ -1,7 +1,6 @@
1
  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
@@ -10,11 +9,8 @@ from deep_translator import GoogleTranslator
10
 
11
 
12
  class Comsatsbot:
13
- def __init__(self, hf, llm, api_keys, chats_collection, paths, index_path='faiss_kb'):
14
- self.llm = llm
15
- self.api_keys = api_keys
16
- self.client = None
17
- self.models = ["llama-3.1-70b-versatile"] # use a single known working model
18
  self.memory = ConversationBufferMemory(llm=self.llm, max_token_limit=3000)
19
  self.chats_collection = chats_collection
20
  self.index_path = index_path
@@ -80,73 +76,35 @@ class Comsatsbot:
80
  prompt = f'''
81
  You are a comsats assistant. Answer concisely and with emojis only where appropriate.
82
  Don't mention context/history explicitly. Use friendly tone and only say "I don’t know" if no relevant answer is found.
83
-
84
  University Info:
85
  Comsats Attock Campus offers CS, SE, AI, English, Math, EE, CE, BBA.
86
  Departments: CS, Math, EE. Facilities: grounds, canteens, mosque, LT rooms, labs.
87
  Admissions via NTS. CGPA: 4.0 (85%), 3.66 (79-84%), etc.
88
-
89
  Context:
90
  {context_text}
91
-
92
  Chat History:
93
  {formatted_history}
94
-
95
  Question: {question}
96
  '''
97
 
98
- for api_key in self.api_keys:
99
- self.client = Groq(api_key=api_key)
100
- for model in self.models:
101
- try:
102
- chat_completion = self.client.chat.completions.create(
103
- messages=[
104
- {"role": "system", "content": prompt},
105
- {"role": "user", "content": f"Answer the following question: {question}"}
106
- ],
107
- model=model,
108
- max_tokens=1024,
109
- )
110
- result = chat_completion.choices[0].message.content
111
- print("Returning answer:", result)
112
- return result
113
- except Exception as e:
114
- print(f"Error with {model}: {e}")
115
- time.sleep(2)
116
- continue
117
-
118
- return "Sorry 😔 I'm currently facing technical issues. Please try again later."
119
 
120
  def detect_language(self, question):
121
- for api_key in self.api_keys:
122
- self.client = Groq(api_key=api_key)
123
- for model in self.models:
124
- try:
125
- chat_completion = self.client.chat.completions.create(
126
- messages=[
127
- {
128
- "role": "system",
129
- "content": """
130
- You are an expert agent, and your task is to detect the language.
131
- Return a JSON: {'detected_language': 'urdu' or 'english'}
132
- """
133
- },
134
- {
135
- "role": "user",
136
- "content": f"Detect the language for: {question}"
137
- }
138
- ],
139
- model=model,
140
- max_tokens=256,
141
- response_format={"type": "json_object"},
142
- )
143
- response = json.loads(chat_completion.choices[0].message.content)
144
- return response['detected_language'].lower()
145
- except Exception as e:
146
- print(f"Language detection error: {e}")
147
- time.sleep(2)
148
- continue
149
- return "english"
150
 
151
  def translate_urdu(self, text):
152
  try:
 
1
  import os
2
  import time
3
  import json
 
4
  from langchain.memory import ConversationBufferMemory
5
  from langchain_openai import ChatOpenAI
6
  from langchain_community.document_loaders import CSVLoader
 
9
 
10
 
11
  class Comsatsbot:
12
+ def __init__(self, hf, api_key, chats_collection, paths, index_path='faiss_kb'):
13
+ self.llm = ChatOpenAI(openai_api_key=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, model="gpt-3.5-turbo")
 
 
 
14
  self.memory = ConversationBufferMemory(llm=self.llm, max_token_limit=3000)
15
  self.chats_collection = chats_collection
16
  self.index_path = index_path
 
76
  prompt = f'''
77
  You are a comsats assistant. Answer concisely and with emojis only where appropriate.
78
  Don't mention context/history explicitly. Use friendly tone and only say "I don’t know" if no relevant answer is found.
 
79
  University Info:
80
  Comsats Attock Campus offers CS, SE, AI, English, Math, EE, CE, BBA.
81
  Departments: CS, Math, EE. Facilities: grounds, canteens, mosque, LT rooms, labs.
82
  Admissions via NTS. CGPA: 4.0 (85%), 3.66 (79-84%), etc.
 
83
  Context:
84
  {context_text}
 
85
  Chat History:
86
  {formatted_history}
 
87
  Question: {question}
88
  '''
89
 
90
+ try:
91
+ response = self.llm.invoke(prompt)
92
+ result = response.content
93
+ print("Returning answer:", result)
94
+ return result
95
+ except Exception as e:
96
+ print("OpenAI error:", e)
97
+ return "Sorry 😔 I'm currently facing technical issues. Please try again later."
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  def detect_language(self, question):
100
+ try:
101
+ result = GoogleTranslator(source='auto', target='en').translate(question)
102
+ if result == question:
103
+ return "english"
104
+ else:
105
+ return "urdu"
106
+ except:
107
+ return "english"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  def translate_urdu(self, text):
110
  try: