Shreyas094 commited on
Commit
69e5fab
·
verified ·
1 Parent(s): 254c6e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -2
app.py CHANGED
@@ -301,17 +301,27 @@ def retry_last_response(history, model, temperature, num_calls):
301
 
302
  return chatbot_interface(last_user_msg, history, model, temperature, num_calls)
303
 
 
 
 
 
 
 
304
  def get_response_from_duckduckgo(query, model, context, num_calls=1, temperature=0.2):
305
  logging.info(f"Using DuckDuckGo chat with model: {model}")
306
  ddg_model = model.split('/')[-1] # Extract the model name from the full string
307
 
 
 
 
308
  full_response = ""
309
  for _ in range(num_calls):
310
  try:
311
- # Include context in the query
312
- contextualized_query = f"Using the following context:\n{context}\n\nUser question: {query}"
313
  results = DDGS().chat(contextualized_query, model=ddg_model)
314
  full_response += results + "\n"
 
315
  except Exception as e:
316
  logging.error(f"Error in generating response from DuckDuckGo: {str(e)}")
317
  yield f"An error occurred with the {model} model: {str(e)}. Please try again."
@@ -338,6 +348,7 @@ def respond(message, history, model, temperature, num_calls, selected_docs):
338
  return
339
 
340
  context_str = "\n".join([doc.page_content for doc in relevant_docs])
 
341
  else:
342
  context_str = "No documents available."
343
  yield "No documents available. Please upload PDF documents to answer questions."
 
301
 
302
  return chatbot_interface(last_user_msg, history, model, temperature, num_calls)
303
 
304
+ def truncate_context(context, max_length=10000):
305
+ """Truncate the context to a maximum length."""
306
+ if len(context) <= max_length:
307
+ return context
308
+ return context[:max_length] + "..."
309
+
310
  def get_response_from_duckduckgo(query, model, context, num_calls=1, temperature=0.2):
311
  logging.info(f"Using DuckDuckGo chat with model: {model}")
312
  ddg_model = model.split('/')[-1] # Extract the model name from the full string
313
 
314
+ # Truncate the context to avoid exceeding input limits
315
+ truncated_context = truncate_context(context)
316
+
317
  full_response = ""
318
  for _ in range(num_calls):
319
  try:
320
+ # Include truncated context in the query
321
+ contextualized_query = f"Using the following context:\n{truncated_context}\n\nUser question: {query}"
322
  results = DDGS().chat(contextualized_query, model=ddg_model)
323
  full_response += results + "\n"
324
+ logging.info(f"DuckDuckGo API response received. Length: {len(results)}")
325
  except Exception as e:
326
  logging.error(f"Error in generating response from DuckDuckGo: {str(e)}")
327
  yield f"An error occurred with the {model} model: {str(e)}. Please try again."
 
348
  return
349
 
350
  context_str = "\n".join([doc.page_content for doc in relevant_docs])
351
+ logging.info(f"Context length: {len(context_str)}")
352
  else:
353
  context_str = "No documents available."
354
  yield "No documents available. Please upload PDF documents to answer questions."