Skip to content

100+ AI Agents, Agent Skills and RAG Apps - Free and Open Source.

Fix broken retrieval/setup in four RAG tutorials (#1051)

Thejesha855f2cae2f3
diff --git a/rag_tutorials/agentic_rag_math_agent/rag/query_router.py b/rag_tutorials/agentic_rag_math_agent/rag/query_router.py
index f66796ed..8b1421ec 100644
--- a/rag_tutorials/agentic_rag_math_agent/rag/query_router.py
+++ b/rag_tutorials/agentic_rag_math_agent/rag/query_router.py
@@ -83,6 +83,12 @@ Only include valid math steps β€” do not guess or make up answers.
return response.text
+# query_kb returns a cosine similarity (higher = better); with OpenAI embeddings
+# almost any two English texts score well above 0, so `> 0.` accepted every query
+# and served an arbitrary KB answer as authoritative. Require a real match.
+KB_SIMILARITY_THRESHOLD = 0.80
+
+
def answer_math_question(question: str):
print(f"πŸ” Query: {question}")
@@ -96,7 +102,7 @@ def answer_math_question(question: str):
kb_answer, similarity = query_kb(question)
print("πŸ§ͺ KB raw answer:", kb_answer)
- if similarity > 0.:
+ if similarity >= KB_SIMILARITY_THRESHOLD:
print("βœ… High similarity KB match, using GPT for step-by-step explanation...")
prompt = f"""
diff --git a/rag_tutorials/ai_blog_search/app.py b/rag_tutorials/ai_blog_search/app.py
index 36006a46..a1348c08 100644
--- a/rag_tutorials/ai_blog_search/app.py
+++ b/rag_tutorials/ai_blog_search/app.py
@@ -1,6 +1,7 @@
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_qdrant import QdrantVectorStore
from qdrant_client import QdrantClient
+from qdrant_client.models import Distance, VectorParams
from uuid import uuid4
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
@@ -73,6 +74,15 @@ def initialize_components():
api_key=st.session_state.qdrant_api_key
)
+ # QdrantVectorStore validates the collection on construction and raises a
+ # 404 if it doesn't exist, so create it up front on a fresh instance.
+ # 768 = dimension of models/embedding-001.
+ if not client.collection_exists("qdrant_db"):
+ client.create_collection(
+ collection_name="qdrant_db",
+ vectors_config=VectorParams(size=768, distance=Distance.COSINE),
+ )
+
# Initialize vector store
db = QdrantVectorStore(
client=client,
diff --git a/rag_tutorials/corrective_rag/corrective_rag.py b/rag_tutorials/corrective_rag/corrective_rag.py
index 2a637a91..c3962d2d 100644
--- a/rag_tutorials/corrective_rag/corrective_rag.py
+++ b/rag_tutorials/corrective_rag/corrective_rag.py
@@ -20,6 +20,7 @@ from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams
import tempfile
import os
+from urllib.parse import urlparse
from langchain_anthropic import ChatAnthropic
from tenacity import retry, stop_after_attempt, wait_exponential
@@ -155,8 +156,13 @@ def web_search(state):
def load_documents(file_or_url: str, is_url: bool = True) -> list:
try:
if is_url:
- loader = WebBaseLoader(file_or_url)
- loader.requests_per_second = 1
+ # A .pdf URL must be parsed as a PDF; WebBaseLoader would run an HTML
+ # parser over the binary body and embed decoded garbage.
+ if urlparse(file_or_url).path.lower().endswith(".pdf"):
+ loader = PyPDFLoader(file_or_url)
+ else:
+ loader = WebBaseLoader(file_or_url)
+ loader.requests_per_second = 1
else:
file_extension = os.path.splitext(file_or_url)[1].lower()
if file_extension == '.pdf':
@@ -190,7 +196,12 @@ else:
# Clean up the temporary file
os.unlink(tmp_file.name)
-if docs:
+# Streamlit re-runs this whole script on every widget interaction (e.g. asking a
+# question), so ingest only when the source actually changes β€” otherwise every
+# question would delete the collection and re-embed the entire document.
+source_key = url if input_option == "URL" else (uploaded_file.name if uploaded_file else None)
+
+if docs and st.session_state.get("ingested_source") != source_key:
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
chunk_size=500, chunk_overlap=100
)
@@ -203,7 +214,7 @@ if docs:
# Try to delete the collection if it exists
client.delete_collection(collection_name)
except Exception:
- pass
+ pass
client.create_collection(
collection_name=collection_name,
@@ -220,6 +231,10 @@ if docs:
# Add documents to the vectorstore
vectorstore.add_documents(all_splits)
retriever = vectorstore.as_retriever()
+ st.session_state.ingested_source = source_key
+ st.session_state.retriever = retriever
+elif st.session_state.get("retriever") is not None:
+ retriever = st.session_state.retriever
class GraphState(TypedDict):
diff --git a/rag_tutorials/knowledge_graph_rag_citations/knowledge_graph_rag.py b/rag_tutorials/knowledge_graph_rag_citations/knowledge_graph_rag.py
index 64d3e549..3db32e40 100644
--- a/rag_tutorials/knowledge_graph_rag_citations/knowledge_graph_rag.py
+++ b/rag_tutorials/knowledge_graph_rag_citations/knowledge_graph_rag.py
@@ -144,13 +144,16 @@ class KnowledgeGraphManager:
def semantic_search(self, query: str) -> List[Dict]:
"""Search for relevant entities based on query."""
+ # CONTAINS on the whole question can never match an entity name/description,
+ # so match any term (case-insensitively) instead of the full raw query.
+ terms = re.findall(r"[A-Za-z0-9]{3,}", query) or [query]
with self.driver.session() as session:
# Simple text matching (in production, use vector embeddings)
result = session.run(
"""
MATCH (e:Entity)
- WHERE e.name CONTAINS $query
- OR e.description CONTAINS $query
+ WHERE any(t IN $terms WHERE toLower(e.name) CONTAINS toLower(t)
+ OR toLower(e.description) CONTAINS toLower(t))
RETURN e.name as name,
e.description as description,
e.source_doc as source,
@@ -158,7 +161,7 @@ class KnowledgeGraphManager:
e.type as type
LIMIT 10
""",
- query=query
+ terms=terms
)
return [dict(record) for record in result]