added logging of RAG chunks for debugging

This commit is contained in:
2026-02-03 11:44:11 +01:00
parent 205e348be0
commit 62dc69374a
3 changed files with 55 additions and 17 deletions
+24 -15
View File
@@ -134,18 +134,26 @@ class LocalRAG:
return doc_list
def query(self, question, k=4):
"""Query the RAG system"""
"""Query the RAG system. Returns dict with 'answer' and 'retrieved' (list of chunks with content, source, page)."""
if self.vectorstore is None:
return "Error: No documents loaded. Please add documents first."
print(f"\nSearching for relevant documents...")
return {
"answer": "Error: No documents loaded. Please add documents first.",
"retrieved": [],
}
docs = self.vectorstore.similarity_search(question, k=k)
print(f"Found {len(docs)} relevant documents")
retrieved = [
{
"content": doc.page_content,
"source": doc.metadata.get("source", ""),
"page": doc.metadata.get("page"),
}
for doc in docs
]
# Combine context from documents
context = "\n\n".join([doc.page_content for doc in docs])
# Create prompt
prompt = f"""Use the following context to answer the question.
If you don't know the answer, say that you don't know instead of making up an answer.
@@ -155,12 +163,11 @@ Context:
Question: {question}
Answer:"""
print("Generating answer with Ollama...")
response = self.llm.invoke(prompt)
answer = response.content if hasattr(response, 'content') else str(response)
return answer
answer = response.content if hasattr(response, "content") else str(response)
return {"answer": answer, "retrieved": retrieved}
def main():
@@ -184,9 +191,11 @@ def main():
# Query
question = "What do the documents say about modality for perceived message perception?"
answer = rag.query(question)
result = rag.query(question)
print(f"\nQuestion: {question}")
print(f"Answer: {answer}")
print(f"Answer: {result['answer']}")
if result.get("retrieved"):
print(f"Retrieved {len(result['retrieved'])} chunks")
# print("\nSetup complete! Uncomment the code above to add documents and query.")