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

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +61 -20
chatbot.py CHANGED
@@ -1,6 +1,7 @@
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,15 +10,21 @@ from deep_translator import GoogleTranslator
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
17
  self.hf = hf
 
18
  self.faiss_index = None
19
  self.faiss_retriever = None
20
- self.paths = paths
21
  self.initialize_faiss_index()
22
 
23
  def load_data(self, paths):
@@ -87,24 +94,58 @@ class Comsatsbot:
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:
 
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
 
12
  class Comsatsbot:
13
+ def __init__(self, hf, chats_collection):
14
+ self.api_keys = ["sk-live-1234567890abcdef"] # βœ… replace with your actual Groq key if needed
15
+ self.llm = ChatOpenAI(
16
+ openai_api_key="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",
17
+ model="gpt-3.5-turbo"
18
+ )
19
+ self.client = None
20
+ self.models = ["llama-3.1-70b-versatile"]
21
  self.memory = ConversationBufferMemory(llm=self.llm, max_token_limit=3000)
22
  self.chats_collection = chats_collection
23
+ self.index_path = "faiss_kb"
24
  self.hf = hf
25
+ self.paths = ["english_data.csv", "urdu_data.csv"] # βœ… actual dataset paths
26
  self.faiss_index = None
27
  self.faiss_retriever = None
 
28
  self.initialize_faiss_index()
29
 
30
  def load_data(self, paths):
 
94
  Question: {question}
95
  '''
96
 
97
+ for api_key in self.api_keys:
98
+ self.client = Groq(api_key=api_key)
99
+ for model in self.models:
100
+ try:
101
+ chat_completion = self.client.chat.completions.create(
102
+ messages=[
103
+ {"role": "system", "content": prompt},
104
+ {"role": "user", "content": f"Answer the following question: {question}"}
105
+ ],
106
+ model=model,
107
+ max_tokens=1024,
108
+ )
109
+ result = chat_completion.choices[0].message.content
110
+ print("Returning answer:", result)
111
+ return result
112
+ except Exception as e:
113
+ print(f"Error with {model}: {e}")
114
+ time.sleep(2)
115
+ continue
116
+
117
+ return "Sorry πŸ˜” I'm currently facing technical issues. Please try again later."
118
 
119
  def detect_language(self, question):
120
+ for api_key in self.api_keys:
121
+ self.client = Groq(api_key=api_key)
122
+ for model in self.models:
123
+ try:
124
+ chat_completion = self.client.chat.completions.create(
125
+ messages=[
126
+ {
127
+ "role": "system",
128
+ "content": """
129
+ You are an expert agent, and your task is to detect the language.
130
+ Return a JSON: {'detected_language': 'urdu' or 'english'}
131
+ """
132
+ },
133
+ {
134
+ "role": "user",
135
+ "content": f"Detect the language for: {question}"
136
+ }
137
+ ],
138
+ model=model,
139
+ max_tokens=256,
140
+ response_format={"type": "json_object"},
141
+ )
142
+ response = json.loads(chat_completion.choices[0].message.content)
143
+ return response['detected_language'].lower()
144
+ except Exception as e:
145
+ print(f"Language detection error: {e}")
146
+ time.sleep(2)
147
+ continue
148
+ return "english"
149
 
150
  def translate_urdu(self, text):
151
  try: