Spaces:
Running
Running
Update rag_engine.py
Browse files- rag_engine.py +6 -0
rag_engine.py
CHANGED
@@ -471,13 +471,16 @@ def cached_process_query(query, top_k=5, word_limit=100):
|
|
471 |
dict: Dictionary containing the query, answer, and citations
|
472 |
"""
|
473 |
print(f"\n🔍 Processing query (cached): {query}")
|
|
|
474 |
faiss_index, text_chunks, metadata_dict = cached_load_data_files()
|
|
|
475 |
if faiss_index is None or text_chunks is None or metadata_dict is None:
|
476 |
return {
|
477 |
"query": query,
|
478 |
"answer_with_rag": "⚠️ System error: Data files not loaded properly.",
|
479 |
"citations": "No citations available."
|
480 |
}
|
|
|
481 |
retrieved_context, retrieved_sources = retrieve_passages(
|
482 |
query,
|
483 |
faiss_index,
|
@@ -485,12 +488,15 @@ def cached_process_query(query, top_k=5, word_limit=100):
|
|
485 |
metadata_dict,
|
486 |
top_k=top_k
|
487 |
)
|
|
|
488 |
sources = format_citations(retrieved_sources) if retrieved_sources else "No citation available."
|
|
|
489 |
if retrieved_context:
|
490 |
context_with_sources = list(zip(retrieved_sources, retrieved_context))
|
491 |
llm_answer_with_rag = answer_with_llm(query, context_with_sources, word_limit=word_limit)
|
492 |
else:
|
493 |
llm_answer_with_rag = "⚠️ No relevant context found."
|
|
|
494 |
return {"query": query, "answer_with_rag": llm_answer_with_rag, "citations": sources}
|
495 |
|
496 |
def process_query(query, top_k=5, word_limit=100):
|
|
|
471 |
dict: Dictionary containing the query, answer, and citations
|
472 |
"""
|
473 |
print(f"\n🔍 Processing query (cached): {query}")
|
474 |
+
# Load all necessary data resources (with caching)
|
475 |
faiss_index, text_chunks, metadata_dict = cached_load_data_files()
|
476 |
+
# Handle missing data gracefully
|
477 |
if faiss_index is None or text_chunks is None or metadata_dict is None:
|
478 |
return {
|
479 |
"query": query,
|
480 |
"answer_with_rag": "⚠️ System error: Data files not loaded properly.",
|
481 |
"citations": "No citations available."
|
482 |
}
|
483 |
+
# Step 1: Retrieve relevant passages using similarity search
|
484 |
retrieved_context, retrieved_sources = retrieve_passages(
|
485 |
query,
|
486 |
faiss_index,
|
|
|
488 |
metadata_dict,
|
489 |
top_k=top_k
|
490 |
)
|
491 |
+
# Step 2: Format citations for display
|
492 |
sources = format_citations(retrieved_sources) if retrieved_sources else "No citation available."
|
493 |
+
# Step 3: Generate the answer if relevant context was found
|
494 |
if retrieved_context:
|
495 |
context_with_sources = list(zip(retrieved_sources, retrieved_context))
|
496 |
llm_answer_with_rag = answer_with_llm(query, context_with_sources, word_limit=word_limit)
|
497 |
else:
|
498 |
llm_answer_with_rag = "⚠️ No relevant context found."
|
499 |
+
# Return the complete response package
|
500 |
return {"query": query, "answer_with_rag": llm_answer_with_rag, "citations": sources}
|
501 |
|
502 |
def process_query(query, top_k=5, word_limit=100):
|