switched ollama model, added script to add pdfs to the vector store, tuned RAG parameters
This commit is contained in:
+22
-6
@@ -26,8 +26,8 @@ class LocalRAG:
|
||||
|
||||
# Text splitter
|
||||
self.text_splitter = RecursiveCharacterTextSplitter(
|
||||
chunk_size=1500,
|
||||
chunk_overlap=300
|
||||
chunk_size=2000,
|
||||
chunk_overlap=400
|
||||
)
|
||||
|
||||
# Ollama LLM
|
||||
@@ -157,6 +157,18 @@ class LocalRAG:
|
||||
for doc in docs
|
||||
]
|
||||
|
||||
def _docs_scores_to_retrieved(self, docs_with_scores):
|
||||
"""Convert (Document, score) list to retrieved chunks format with score. FAISS returns L2 distance (lower = more similar)."""
|
||||
return [
|
||||
{
|
||||
"content": doc.page_content,
|
||||
"source": doc.metadata.get("source", ""),
|
||||
"page": doc.metadata.get("page"),
|
||||
"score": float(score),
|
||||
}
|
||||
for doc, score in docs_with_scores
|
||||
]
|
||||
|
||||
def query(self, question, k=8):
|
||||
"""Query the RAG system (no conversation history). Returns dict with 'answer' and 'retrieved'."""
|
||||
return self.query_with_history(question, chat_history=[], k=k)
|
||||
@@ -193,9 +205,13 @@ Standalone question:"""
|
||||
|
||||
print(f"[RAG] Query sent to vector store: {search_query!r}")
|
||||
|
||||
# 2) Retrieve documents using the (rephrased) query
|
||||
docs = self.vectorstore.similarity_search(search_query, k=k)
|
||||
retrieved = self._docs_to_retrieved(docs)
|
||||
# 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 = [doc for doc, _ in docs_with_scores]
|
||||
retrieved = self._docs_scores_to_retrieved(docs_with_scores)
|
||||
if docs_with_scores:
|
||||
scores = [f"{s:.3f}" for _, s in docs_with_scores]
|
||||
print(f"[RAG] Retrieved {len(docs)} chunk(s), scores (L2 dist): [{', '.join(scores)}]")
|
||||
context = "\n\n".join([doc.page_content for doc in docs])
|
||||
|
||||
# 3) Answer using conversation history + retrieved context
|
||||
@@ -222,7 +238,7 @@ def main():
|
||||
print("=" * 60)
|
||||
|
||||
# Initialize
|
||||
rag = LocalRAG(ollama_model="mistral:7b")
|
||||
rag = LocalRAG(ollama_model="gpt-oss:20b")
|
||||
|
||||
# Add documents (uncomment and add your file paths)
|
||||
# rag.add_documents([
|
||||
|
||||
Reference in New Issue
Block a user