Shreyas094 commited on
Commit
cb292ae
·
verified ·
1 Parent(s): 6e704d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -349,8 +349,8 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
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,7 +361,7 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
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,11 +392,11 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
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,7 +404,6 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
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))]
@@ -449,23 +448,20 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
449
  context_str = "\n".join([doc.page_content for doc in relevant_docs])
450
 
451
  prompt_template = """
452
- Answer the question based on the following context from the PDF document:
453
- Context:
454
- {context}
455
  Question: {question}
456
  Provide a summarized and direct answer to the question.
457
  """
458
 
459
  while True:
460
  prompt_val = ChatPromptTemplate.from_template(prompt_template)
461
- formatted_prompt = prompt_val.format(context=context_str, question=question)
462
 
463
  estimated_tokens = estimate_tokens(formatted_prompt)
464
 
465
  if estimated_tokens <= max_estimated_tokens:
466
  break
467
 
468
- # Reduce context if estimated token count is too high
469
  context_str = context_str[:int(len(context_str) * context_reduction_factor)]
470
 
471
  if len(context_str) < 100:
 
349
  database = None
350
 
351
  max_attempts = 5
352
+ context_reduction_factor = 0.5
353
+ max_estimated_tokens = 25000
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"]]
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],
396
+ conv_context=current_conv_context[:500],
397
  question=question,
398
+ topics=", ".join(current_topics[:5]),
399
+ entities=json.dumps({k: v[:2] for k, v in current_entities.items()})
400
  )
401
 
402
  estimated_tokens = estimate_tokens(formatted_prompt)
 
404
  if estimated_tokens <= max_estimated_tokens:
405
  break
406
 
 
407
  current_context = current_context[:int(len(current_context) * context_reduction_factor)]
408
  current_conv_context = current_conv_context[:int(len(current_conv_context) * context_reduction_factor)]
409
  current_topics = current_topics[:max(1, int(len(current_topics) * context_reduction_factor))]
 
448
  context_str = "\n".join([doc.page_content for doc in relevant_docs])
449
 
450
  prompt_template = """
451
+ Answer based on: PDF Context: {context}
 
 
452
  Question: {question}
453
  Provide a summarized and direct answer to the question.
454
  """
455
 
456
  while True:
457
  prompt_val = ChatPromptTemplate.from_template(prompt_template)
458
+ formatted_prompt = prompt_val.format(context=context_str[:3000], question=question)
459
 
460
  estimated_tokens = estimate_tokens(formatted_prompt)
461
 
462
  if estimated_tokens <= max_estimated_tokens:
463
  break
464
 
 
465
  context_str = context_str[:int(len(context_str) * context_reduction_factor)]
466
 
467
  if len(context_str) < 100: