Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -233,11 +233,10 @@ def generate_chunked_response(model, prompt, max_tokens=1000, max_chunks=5):
|
|
233 |
full_response += chunk
|
234 |
except Exception as e:
|
235 |
print(f"Error in generate_chunked_response: {e}")
|
236 |
-
print(f"Prompt: {prompt}")
|
237 |
-
print(f"Full response so far: {full_response}")
|
238 |
if "Input validation error" in str(e):
|
|
|
239 |
return full_response if full_response else "The input was too long to process. Please try a shorter query."
|
240 |
-
|
241 |
return full_response.strip()
|
242 |
|
243 |
def extract_text_from_webpage(html):
|
@@ -350,8 +349,8 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
350 |
database = None
|
351 |
|
352 |
max_attempts = 5
|
353 |
-
context_reduction_factor = 0.5
|
354 |
-
max_estimated_tokens = 25000
|
355 |
|
356 |
if web_search:
|
357 |
contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)
|
@@ -362,7 +361,7 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
362 |
|
363 |
for attempt in range(max_attempts):
|
364 |
try:
|
365 |
-
web_docs = [Document(page_content=result["text"][:1000], metadata={"source": result["link"]}) for result in search_results if result["text"]]
|
366 |
|
367 |
if database is None:
|
368 |
database = FAISS.from_documents(web_docs, embed)
|
@@ -393,11 +392,11 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
393 |
|
394 |
while True:
|
395 |
formatted_prompt = prompt_val.format(
|
396 |
-
context=current_context[:3000],
|
397 |
-
conv_context=current_conv_context[:500],
|
398 |
question=question,
|
399 |
-
topics=", ".join(current_topics[:5]),
|
400 |
-
entities=json.dumps({k: v[:2] for k, v in current_entities.items()})
|
401 |
)
|
402 |
|
403 |
estimated_tokens = estimate_tokens(formatted_prompt)
|
@@ -405,6 +404,7 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
405 |
if estimated_tokens <= max_estimated_tokens:
|
406 |
break
|
407 |
|
|
|
408 |
current_context = current_context[:int(len(current_context) * context_reduction_factor)]
|
409 |
current_conv_context = current_conv_context[:int(len(current_conv_context) * context_reduction_factor)]
|
410 |
current_topics = current_topics[:max(1, int(len(current_topics) * context_reduction_factor))]
|
@@ -415,24 +415,18 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
415 |
|
416 |
full_response = generate_chunked_response(model, formatted_prompt)
|
417 |
answer = extract_answer(full_response, instructions)
|
418 |
-
|
419 |
-
# Check if the answer is an error message
|
420 |
-
if answer.startswith("An error occurred while processing the response:"):
|
421 |
-
print(f"Error in extract_answer: {answer}")
|
422 |
-
raise ValueError(answer)
|
423 |
-
|
424 |
all_answers.append(answer)
|
425 |
break
|
426 |
|
427 |
except ValueError as ve:
|
428 |
print(f"Error in ask_question (attempt {attempt + 1}): {ve}")
|
429 |
if attempt == max_attempts - 1:
|
430 |
-
all_answers.append(f"I apologize, but I'm having trouble processing the query.
|
431 |
|
432 |
except Exception as e:
|
433 |
print(f"Error in ask_question (attempt {attempt + 1}): {e}")
|
434 |
if attempt == max_attempts - 1:
|
435 |
-
all_answers.append(f"I apologize, but an unexpected error occurred. Please try again with a different question or check your internet connection.
|
436 |
|
437 |
answer = "\n\n".join(all_answers)
|
438 |
sources = set(doc.metadata['source'] for doc in web_docs)
|
@@ -492,52 +486,36 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
492 |
return "An unexpected error occurred. Please try again later."
|
493 |
|
494 |
def extract_answer(full_response, instructions=None):
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
515 |
|
516 |
-
|
517 |
-
for pattern in patterns_to_remove:
|
518 |
-
full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)
|
519 |
-
|
520 |
-
# Remove any leading/trailing whitespace and newlines
|
521 |
-
full_response = full_response.strip()
|
522 |
-
|
523 |
-
# Remove the user instructions if present
|
524 |
-
if instructions:
|
525 |
-
instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n"
|
526 |
-
full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)
|
527 |
-
|
528 |
-
# Remove any remaining instruction-like phrases at the beginning of the response
|
529 |
-
lines = full_response.split('\n')
|
530 |
-
starters = ["answer:", "response:", "here's", "here is"]
|
531 |
-
while lines and any(lines[0].strip().lower().startswith(starter) for starter in starters):
|
532 |
-
lines.pop(0)
|
533 |
-
full_response = '\n'.join(lines)
|
534 |
-
|
535 |
-
return full_response.strip()
|
536 |
-
except Exception as e:
|
537 |
-
print(f"Error in extract_answer: {e}")
|
538 |
-
print(f"Full response: {full_response}")
|
539 |
-
print(f"Instructions: {instructions}")
|
540 |
-
raise # Re-raise the exception to be caught in ask_question
|
541 |
|
542 |
# Gradio interface
|
543 |
with gr.Blocks() as demo:
|
|
|
233 |
full_response += chunk
|
234 |
except Exception as e:
|
235 |
print(f"Error in generate_chunked_response: {e}")
|
|
|
|
|
236 |
if "Input validation error" in str(e):
|
237 |
+
# If we hit the token limit, return what we have so far
|
238 |
return full_response if full_response else "The input was too long to process. Please try a shorter query."
|
239 |
+
break
|
240 |
return full_response.strip()
|
241 |
|
242 |
def extract_text_from_webpage(html):
|
|
|
349 |
database = None
|
350 |
|
351 |
max_attempts = 5
|
352 |
+
context_reduction_factor = 0.5 # More aggressive reduction
|
353 |
+
max_estimated_tokens = 25000 # Further reduced to leave more room for response
|
354 |
|
355 |
if web_search:
|
356 |
contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)
|
|
|
361 |
|
362 |
for attempt in range(max_attempts):
|
363 |
try:
|
364 |
+
web_docs = [Document(page_content=result["text"][:1000], metadata={"source": result["link"]}) for result in search_results if result["text"]] # Limit each result to 1000 characters
|
365 |
|
366 |
if database is None:
|
367 |
database = FAISS.from_documents(web_docs, embed)
|
|
|
392 |
|
393 |
while True:
|
394 |
formatted_prompt = prompt_val.format(
|
395 |
+
context=current_context[:3000], # Limit context to 3000 characters
|
396 |
+
conv_context=current_conv_context[:500], # Limit conversation context to 500 characters
|
397 |
question=question,
|
398 |
+
topics=", ".join(current_topics[:5]), # Limit to 5 topics
|
399 |
+
entities=json.dumps({k: v[:2] for k, v in current_entities.items()}) # Limit to 2 entities per type
|
400 |
)
|
401 |
|
402 |
estimated_tokens = estimate_tokens(formatted_prompt)
|
|
|
404 |
if estimated_tokens <= max_estimated_tokens:
|
405 |
break
|
406 |
|
407 |
+
# Reduce context if estimated token count is too high
|
408 |
current_context = current_context[:int(len(current_context) * context_reduction_factor)]
|
409 |
current_conv_context = current_conv_context[:int(len(current_conv_context) * context_reduction_factor)]
|
410 |
current_topics = current_topics[:max(1, int(len(current_topics) * context_reduction_factor))]
|
|
|
415 |
|
416 |
full_response = generate_chunked_response(model, formatted_prompt)
|
417 |
answer = extract_answer(full_response, instructions)
|
|
|
|
|
|
|
|
|
|
|
|
|
418 |
all_answers.append(answer)
|
419 |
break
|
420 |
|
421 |
except ValueError as ve:
|
422 |
print(f"Error in ask_question (attempt {attempt + 1}): {ve}")
|
423 |
if attempt == max_attempts - 1:
|
424 |
+
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?")
|
425 |
|
426 |
except Exception as e:
|
427 |
print(f"Error in ask_question (attempt {attempt + 1}): {e}")
|
428 |
if attempt == max_attempts - 1:
|
429 |
+
all_answers.append(f"I apologize, but an unexpected error occurred. Please try again with a different question or check your internet connection.")
|
430 |
|
431 |
answer = "\n\n".join(all_answers)
|
432 |
sources = set(doc.metadata['source'] for doc in web_docs)
|
|
|
486 |
return "An unexpected error occurred. Please try again later."
|
487 |
|
488 |
def extract_answer(full_response, instructions=None):
|
489 |
+
# List of patterns to remove
|
490 |
+
patterns_to_remove = [
|
491 |
+
r"Provide a concise and relevant answer to the question\.",
|
492 |
+
r"Provide additional context if necessary\.",
|
493 |
+
r"If the web search results don't contain relevant information, state that the information is not available in the search results\.",
|
494 |
+
r"Provide a response that addresses the question and follows the user's instructions\.",
|
495 |
+
r"Do not mention these instructions or the web search process in your answer\.",
|
496 |
+
r"Provide a summarized and direct answer to the question\.",
|
497 |
+
r"If the context doesn't contain relevant information, state that the information is not available in the document\.",
|
498 |
+
]
|
499 |
+
|
500 |
+
# Remove the patterns
|
501 |
+
for pattern in patterns_to_remove:
|
502 |
+
full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)
|
503 |
+
|
504 |
+
# Remove any leading/trailing whitespace and newlines
|
505 |
+
full_response = full_response.strip()
|
506 |
+
|
507 |
+
# Remove the user instructions if present
|
508 |
+
if instructions:
|
509 |
+
instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n"
|
510 |
+
full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)
|
511 |
+
|
512 |
+
# Remove any remaining instruction-like phrases at the beginning of the response
|
513 |
+
lines = full_response.split('\n')
|
514 |
+
while lines and any(line.strip().lower().startswith(starter) for starter in ["answer:", "response:", "here's", "here is"]):
|
515 |
+
lines.pop(0)
|
516 |
+
full_response = '\n'.join(lines)
|
517 |
|
518 |
+
return full_response.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
519 |
|
520 |
# Gradio interface
|
521 |
with gr.Blocks() as demo:
|