Shreyas094 commited on
Commit
b2ea3aa
·
verified ·
1 Parent(s): 3fb0e1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -4
app.py CHANGED
@@ -461,8 +461,9 @@ def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=
461
  return
462
 
463
  all_filtered_docs = []
464
- k_per_doc = max(5, 20 // len(selected_docs)) # Adjust this value as needed
465
 
 
466
  for doc_name in selected_docs:
467
  logging.info(f"Retrieving documents for: {doc_name}")
468
  try:
@@ -474,6 +475,16 @@ def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=
474
  except Exception as e:
475
  logging.error(f"Error retrieving documents for {doc_name}: {str(e)}")
476
 
 
 
 
 
 
 
 
 
 
 
477
  logging.info(f"Total number of filtered documents: {len(all_filtered_docs)}")
478
 
479
  if not all_filtered_docs:
@@ -483,14 +494,14 @@ def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=
483
 
484
  for doc in all_filtered_docs:
485
  try:
486
- source = doc.metadata['source'] if isinstance(doc, Document) else doc.get('metadata', {}).get('source', 'Unknown')
487
- content = doc.page_content if isinstance(doc, Document) else doc.get('page_content', '')
488
  logging.info(f"Document source: {source}")
489
  logging.info(f"Document content preview: {content[:100]}...") # Log first 100 characters of each document
490
  except Exception as e:
491
  logging.error(f"Error processing document: {str(e)}")
492
 
493
- context_str = "\n".join([doc.page_content if isinstance(doc, Document) else doc.get('page_content', '') for doc in all_filtered_docs])
494
  logging.info(f"Total context length: {len(context_str)}")
495
 
496
  if model == "@cf/meta/llama-3.1-8b-instruct":
 
461
  return
462
 
463
  all_filtered_docs = []
464
+ k_per_doc = max(10, 30 // len(selected_docs)) # Increased k_per_doc
465
 
466
+ # First, try to retrieve documents with filtering
467
  for doc_name in selected_docs:
468
  logging.info(f"Retrieving documents for: {doc_name}")
469
  try:
 
475
  except Exception as e:
476
  logging.error(f"Error retrieving documents for {doc_name}: {str(e)}")
477
 
478
+ # If no documents are found, try retrieving without filtering
479
+ if not all_filtered_docs:
480
+ logging.warning("No documents found with filtering. Attempting retrieval without filters.")
481
+ try:
482
+ retriever = database.as_retriever(search_kwargs={"k": k_per_doc * len(selected_docs)})
483
+ all_filtered_docs = retriever.get_relevant_documents(query)
484
+ logging.info(f"Retrieved {len(all_filtered_docs)} documents without filtering")
485
+ except Exception as e:
486
+ logging.error(f"Error retrieving documents without filtering: {str(e)}")
487
+
488
  logging.info(f"Total number of filtered documents: {len(all_filtered_docs)}")
489
 
490
  if not all_filtered_docs:
 
494
 
495
  for doc in all_filtered_docs:
496
  try:
497
+ source = doc.metadata['source'] if hasattr(doc, 'metadata') else doc.get('metadata', {}).get('source', 'Unknown')
498
+ content = doc.page_content if hasattr(doc, 'page_content') else doc.get('page_content', '')
499
  logging.info(f"Document source: {source}")
500
  logging.info(f"Document content preview: {content[:100]}...") # Log first 100 characters of each document
501
  except Exception as e:
502
  logging.error(f"Error processing document: {str(e)}")
503
 
504
+ context_str = "\n".join([doc.page_content if hasattr(doc, 'page_content') else doc.get('page_content', '') for doc in all_filtered_docs])
505
  logging.info(f"Total context length: {len(context_str)}")
506
 
507
  if model == "@cf/meta/llama-3.1-8b-instruct":