updated parameters and retrieval prompt to massively improve query quality
This commit is contained in:
+13
-6
@@ -203,12 +203,16 @@ class LocalRAG:
|
||||
|
||||
history_str = self._format_history(chat_history)
|
||||
search_query = question
|
||||
rag_query_instruction = (
|
||||
"Do not return a list of references but prioritize meaningful text from abstracts, results and discussion sections."
|
||||
)
|
||||
|
||||
print(f"[RAG] User question: {question!r}")
|
||||
|
||||
# 1) If we have history, rephrase the question into a standalone query for better retrieval
|
||||
if history_str.strip():
|
||||
rephrase_prompt = f"""Given this chat history and the latest user question, write a single standalone question that captures what the user is asking. Do not answer it; only output the standalone question. If the latest question is already clear on its own, output it unchanged.
|
||||
rephrase_prompt = f"""Given this chat history and the latest user question, write a single standalone question that captures what the user is asking.
|
||||
Do not answer it; only output the standalone question. If the latest question is already clear on its own, output it unchanged.
|
||||
|
||||
Chat history:
|
||||
{history_str}
|
||||
@@ -220,10 +224,12 @@ Standalone question:"""
|
||||
search_query = (rephrase_response.content if hasattr(rephrase_response, "content") else str(rephrase_response)).strip() or question
|
||||
print(f"[RAG] Standalone search query (rephrased): {search_query!r}")
|
||||
|
||||
print(f"[RAG] Query sent to vector store: {search_query!r}")
|
||||
retrieval_query = f"{search_query}\n\n{rag_query_instruction}"
|
||||
print(f"[RAG] Search query: {search_query!r}")
|
||||
print(f"[RAG] Retrieval query sent to vector store: {retrieval_query!r}")
|
||||
|
||||
# 2) Retrieve documents with scores (FAISS: L2 distance, lower = more similar)
|
||||
docs_with_scores = self.vectorstore.similarity_search_with_score(search_query, k=k)
|
||||
docs_with_scores = self.vectorstore.similarity_search_with_score(retrieval_query, k=k)
|
||||
docs = [doc for doc, _ in docs_with_scores]
|
||||
retrieved = self._docs_scores_to_retrieved(docs_with_scores)
|
||||
if docs_with_scores:
|
||||
@@ -233,7 +239,8 @@ Standalone question:"""
|
||||
|
||||
# 3) Answer using conversation history + retrieved context
|
||||
history_block = f"Chat history:\n{history_str}\n\n" if history_str else ""
|
||||
answer_prompt = f"""You are an assistant for question-answering. Use the chat history (if any) and the retrieved context below to answer the current question. If you don't know the answer, say so. Keep the conversation coherent.
|
||||
answer_prompt = f"""You are an assistant for question-answering. Use the chat history (if any) and the retrieved context below to answer the current question.
|
||||
If you don't know the answer, say so. Keep the conversation coherent.
|
||||
|
||||
{history_block}Relevant context from documents:
|
||||
|
||||
@@ -251,7 +258,7 @@ Answer:"""
|
||||
def main():
|
||||
"""Example usage"""
|
||||
print("=" * 60)
|
||||
print("Local RAG with LangChain, Ollama, and FAISS")
|
||||
print("Local RAG with LangChain, Ollama/OpenAI, and FAISS")
|
||||
print("=" * 60)
|
||||
|
||||
# Initialize
|
||||
@@ -268,7 +275,7 @@ def main():
|
||||
rag.list_documents()
|
||||
|
||||
# Query
|
||||
question = "What do the documents say about modality for perceived message perception?"
|
||||
question = "What do you knowabout modality for perceived message perception?"
|
||||
result = rag.query(question)
|
||||
print(f"\nQuestion: {question}")
|
||||
print(f"Answer: {result['answer']}")
|
||||
|
||||
Reference in New Issue
Block a user