Eng-Musa commited on
Commit
8b4ea9c
·
1 Parent(s): 287f03e

return model used

Browse files
__pycache__/main.cpython-312.pyc CHANGED
Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ
 
clients/__pycache__/groq_client.cpython-312.pyc CHANGED
Binary files a/clients/__pycache__/groq_client.cpython-312.pyc and b/clients/__pycache__/groq_client.cpython-312.pyc differ
 
clients/groq_client.py CHANGED
@@ -178,25 +178,25 @@ class GroqClient:
178
 
179
  # Non-streaming ask method for backwards compatibility
180
  # ============================================
181
- def ask(self, raw_query: str) -> str:
182
  if (
183
  not raw_query
184
  or raw_query is None
185
  or raw_query == ""
186
  or len(raw_query) > 1000
187
  ):
188
- return "Please provide a valid query under 1,000 characters."
189
 
190
  if raw_query.lower() in GREETINGS_TRIGGERS:
191
- return random.choice(GREETINGS)
192
 
193
  try:
194
  docs = self.retriever.invoke(raw_query)
195
  except Exception as e:
196
- return f"Error retrieving documents: {e}"
197
 
198
  if not docs:
199
- return self.handle_unknown_query()
200
 
201
  context = "\n".join([d.page_content for d in docs])
202
  fallback = self.handle_unknown_query()
@@ -242,7 +242,7 @@ class GroqClient:
242
  )
243
  response = completion.choices[0].message.content
244
  if response and response.strip():
245
- return response.strip()
246
  else:
247
  continue # Try next model
248
 
@@ -253,10 +253,10 @@ class GroqClient:
253
  continue
254
  else:
255
  # For other errors, return immediately
256
- return f"Error while calling LLM: {e}"
257
 
258
  # If all models fail
259
- return "I'm temporarily experiencing high demand. Please try again in a few minutes or rephrase your question."
260
 
261
  # Streaming ask method (Generator)
262
  # ============================================
 
178
 
179
  # Non-streaming ask method for backwards compatibility
180
  # ============================================
181
+ def ask(self, raw_query: str) -> tuple[str, str | None]:
182
  if (
183
  not raw_query
184
  or raw_query is None
185
  or raw_query == ""
186
  or len(raw_query) > 1000
187
  ):
188
+ return "Please provide a valid query under 1,000 characters.", None
189
 
190
  if raw_query.lower() in GREETINGS_TRIGGERS:
191
+ return random.choice(GREETINGS), None
192
 
193
  try:
194
  docs = self.retriever.invoke(raw_query)
195
  except Exception as e:
196
+ return f"Error retrieving documents: {e}", None
197
 
198
  if not docs:
199
+ return self.handle_unknown_query(), None
200
 
201
  context = "\n".join([d.page_content for d in docs])
202
  fallback = self.handle_unknown_query()
 
242
  )
243
  response = completion.choices[0].message.content
244
  if response and response.strip():
245
+ return response.strip(), model
246
  else:
247
  continue # Try next model
248
 
 
253
  continue
254
  else:
255
  # For other errors, return immediately
256
+ return f"Error while calling LLM: {e}", None
257
 
258
  # If all models fail
259
+ return "I'm temporarily experiencing high demand. Please try again in a few minutes or rephrase your question.", None
260
 
261
  # Streaming ask method (Generator)
262
  # ============================================
main.py CHANGED
@@ -43,18 +43,20 @@ def get_groq_client():
43
  async def chat_endpoint(request: QueryRequest):
44
  try:
45
  groq_client = get_groq_client()
46
- response = groq_client.ask(request.query)
47
  if response.startswith("Error while"):
48
- return {"status_code": 500, "response": response, "next_questions": []}
49
  return {
50
  "status_code": 200,
51
  "response": response,
 
52
  "next_questions": groq_client.get_next_questions(request.ip),
53
  }
54
  except Exception as e:
55
  return {
56
  "status_code": 500,
57
  "response": f"Internal error: {e}",
 
58
  "next_questions": [],
59
  }
60
 
 
43
  async def chat_endpoint(request: QueryRequest):
44
  try:
45
  groq_client = get_groq_client()
46
+ response, model = groq_client.ask(request.query)
47
  if response.startswith("Error while"):
48
+ return {"status_code": 500, "response": response, "model": model, "next_questions": []}
49
  return {
50
  "status_code": 200,
51
  "response": response,
52
+ "model": model,
53
  "next_questions": groq_client.get_next_questions(request.ip),
54
  }
55
  except Exception as e:
56
  return {
57
  "status_code": 500,
58
  "response": f"Internal error: {e}",
59
+ "model": None,
60
  "next_questions": [],
61
  }
62