Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -352,55 +352,126 @@ def estimate_tokens(text):
|
|
352 |
# Rough estimate: 1 token ~= 4 characters
|
353 |
return len(text) // 4
|
354 |
|
355 |
-
def ask_question(question
|
|
|
|
|
|
|
356 |
model = get_model(temperature, top_p, repetition_penalty)
|
|
|
|
|
357 |
chatbot.model = model
|
358 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
359 |
if web_search:
|
360 |
contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)
|
361 |
|
362 |
-
# Log the contextualized question for debugging
|
363 |
print(f"Contextualized question: {contextualized_question}")
|
|
|
364 |
|
365 |
-
|
|
|
|
|
|
|
|
|
366 |
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
384 |
break
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
answer
|
400 |
-
|
401 |
# Update chatbot context with the answer
|
402 |
chatbot.add_to_history(answer)
|
403 |
-
|
404 |
return answer
|
405 |
|
406 |
else: # PDF document chat
|
|
|
352 |
# Rough estimate: 1 token ~= 4 characters
|
353 |
return len(text) // 4
|
354 |
|
355 |
+
def ask_question(question, temperature, top_p, repetition_penalty, web_search, chatbot):
|
356 |
+
if not question:
|
357 |
+
return "Please enter a question."
|
358 |
+
|
359 |
model = get_model(temperature, top_p, repetition_penalty)
|
360 |
+
|
361 |
+
# Update the chatbot's model
|
362 |
chatbot.model = model
|
363 |
|
364 |
+
embed = get_embeddings()
|
365 |
+
|
366 |
+
if os.path.exists("faiss_database"):
|
367 |
+
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
368 |
+
else:
|
369 |
+
database = None
|
370 |
+
|
371 |
+
max_attempts = 3 # Define the maximum number of attempts
|
372 |
+
context_reduction_factor = 0.7
|
373 |
+
max_tokens = 32000 # Maximum tokens allowed by the model
|
374 |
+
|
375 |
if web_search:
|
376 |
contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)
|
377 |
|
378 |
+
# Log the contextualized question and instructions separately for debugging
|
379 |
print(f"Contextualized question: {contextualized_question}")
|
380 |
+
print(f"Instructions: {instructions}")
|
381 |
|
382 |
+
try:
|
383 |
+
search_results = google_search(contextualized_question, num_results=3)
|
384 |
+
except Exception as e:
|
385 |
+
print(f"Error in web search: {e}")
|
386 |
+
return f"I apologize, but I encountered an error while searching for information: {str(e)}"
|
387 |
|
388 |
+
all_answers = []
|
389 |
+
|
390 |
+
for attempt in range(max_attempts):
|
391 |
+
try:
|
392 |
+
web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
|
393 |
+
|
394 |
+
if not web_docs:
|
395 |
+
return "I'm sorry, but I couldn't find any relevant information from the web search."
|
396 |
+
|
397 |
+
if database is None:
|
398 |
+
database = FAISS.from_documents(web_docs, embed)
|
399 |
+
else:
|
400 |
+
database.add_documents(web_docs)
|
401 |
+
|
402 |
+
database.save_local("faiss_database")
|
403 |
+
|
404 |
+
context_str = "\n".join([f"Source: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs])
|
405 |
+
|
406 |
+
instruction_prompt = f"User Instructions: {instructions}\n" if instructions else ""
|
407 |
+
|
408 |
+
prompt_template = f"""
|
409 |
+
Answer the question based on the following web search results, conversation context, entity information, and user instructions:
|
410 |
+
Web Search Results:
|
411 |
+
{{context}}
|
412 |
+
Conversation Context: {{conv_context}}
|
413 |
+
Current Question: {{question}}
|
414 |
+
Topics: {{topics}}
|
415 |
+
Entity Information: {{entities}}
|
416 |
+
{instruction_prompt}
|
417 |
+
Provide a concise and relevant answer to the question.
|
418 |
+
"""
|
419 |
+
|
420 |
+
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
421 |
+
|
422 |
+
# Start with full context and progressively reduce if necessary
|
423 |
+
current_context = context_str
|
424 |
+
current_conv_context = chatbot.get_context()
|
425 |
+
current_topics = topics
|
426 |
+
current_entities = {k: list(v) for k, v in entity_tracker.items()}
|
427 |
+
|
428 |
+
while True:
|
429 |
+
formatted_prompt = prompt_val.format(
|
430 |
+
context=current_context,
|
431 |
+
conv_context=current_conv_context,
|
432 |
+
question=question,
|
433 |
+
topics=", ".join(current_topics),
|
434 |
+
entities=json.dumps(current_entities)
|
435 |
+
)
|
436 |
+
|
437 |
+
# Estimate token count (rough estimate)
|
438 |
+
estimated_tokens = len(formatted_prompt) // 4
|
439 |
+
|
440 |
+
if estimated_tokens <= max_tokens - 1000: # Leave 1000 tokens for the model's response
|
441 |
+
break
|
442 |
+
|
443 |
+
# Reduce context if estimated token count is too high
|
444 |
+
current_context = current_context[:int(len(current_context) * context_reduction_factor)]
|
445 |
+
current_conv_context = current_conv_context[:int(len(current_conv_context) * context_reduction_factor)]
|
446 |
+
current_topics = current_topics[:max(1, int(len(current_topics) * context_reduction_factor))]
|
447 |
+
current_entities = {k: v[:max(1, int(len(v) * context_reduction_factor))] for k, v in current_entities.items()}
|
448 |
+
|
449 |
+
if len(current_context) + len(current_conv_context) + len(str(current_topics)) + len(str(current_entities)) < 100:
|
450 |
+
raise ValueError("Context reduced too much. Unable to process the query.")
|
451 |
+
|
452 |
+
full_response = generate_chunked_response(model, formatted_prompt, max_tokens=1000)
|
453 |
+
answer = extract_answer(full_response, instructions)
|
454 |
+
all_answers.append(answer)
|
455 |
break
|
456 |
+
|
457 |
+
except ValueError as ve:
|
458 |
+
print(f"Error in ask_question (attempt {attempt + 1}): {ve}")
|
459 |
+
if attempt == max_attempts - 1:
|
460 |
+
all_answers.append(f"I apologize, but I'm having trouble processing the query due to its length or complexity. Could you please try asking a more specific or shorter question?")
|
461 |
+
|
462 |
+
except Exception as e:
|
463 |
+
print(f"Error in ask_question (attempt {attempt + 1}): {e}")
|
464 |
+
if attempt == max_attempts - 1:
|
465 |
+
all_answers.append(f"I apologize, but an unexpected error occurred. Please try again with a different question or check your internet connection.")
|
466 |
+
|
467 |
+
answer = "\n\n".join(all_answers)
|
468 |
+
sources = set(doc.metadata['source'] for doc in web_docs)
|
469 |
+
sources_section = "\n\nSources:\n" + "\n".join(f"- {source}" for source in sources)
|
470 |
+
answer += sources_section
|
471 |
+
|
472 |
# Update chatbot context with the answer
|
473 |
chatbot.add_to_history(answer)
|
474 |
+
|
475 |
return answer
|
476 |
|
477 |
else: # PDF document chat
|