Spaces:
Sleeping
Sleeping
File size: 4,367 Bytes
b2c2387 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | """
RAG query engine with hybrid retrieval + reranking (LlamaIndex).
Pipeline:
question -> retriever (wide candidate pool) -> reranker (narrows to best matches)
-> query engine -> Claude -> answer
Install (add to requirements.txt):
llama-index-core>=0.11.0
llama-index-llms-anthropic>=0.3.0
llama-index-embeddings-huggingface>=0.3.0
sentence-transformers>=3.0.0
"""
import os
from llama_index.core import Settings, VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.postprocessor import SentenceTransformerRerank
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.anthropic import Anthropic
# ---------------------------------------------------------------------------
# 1. GLOBAL SETTINGS — LLM + embedding model, set once for the whole app.
# ---------------------------------------------------------------------------
# Reads ANTHROPIC_API_KEY from environment — set this as a secret in your
# hosting environment, do not hardcode it.
Settings.llm = Anthropic(
model="claude-sonnet-4-6", # match whichever Claude model you're using today
api_key=os.environ["ANTHROPIC_API_KEY"],
)
# Swap model_name to match whatever sentence-transformers model you're
# already running for embeddings.
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-base-en-v1.5")
# ---------------------------------------------------------------------------
# 2. INDEX — build once from your document store, cache the result.
# ---------------------------------------------------------------------------
def build_index(data_dir: str = "data") -> VectorStoreIndex:
"""
Loads your content and builds a LlamaIndex vector index over it.
If you already have embeddings stored somewhere (e.g. an existing vector
store), replace this with the matching LlamaIndex vector store loader
instead of rebuilding from raw files.
"""
documents = SimpleDirectoryReader(data_dir).load_data()
index = VectorStoreIndex.from_documents(documents)
return index
# ---------------------------------------------------------------------------
# 3. RERANKER — narrows a wide candidate pool down to the most relevant few.
# ---------------------------------------------------------------------------
# Small cross-encoder: runs on CPU, no GPU required, fast enough for
# real-time use on a handful of candidates. top_n is how many survive
# reranking and get passed to Claude.
reranker = SentenceTransformerRerank(
model="cross-encoder/ms-marco-MiniLM-L-6-v2",
top_n=5,
)
# ---------------------------------------------------------------------------
# 4. QUERY ENGINE — retrieval (wide) + reranking (narrow) + Claude, wired together.
# ---------------------------------------------------------------------------
def build_query_engine(index: VectorStoreIndex) -> RetrieverQueryEngine:
"""
similarity_top_k=20 -> retrieve a wide candidate pool (this is the pool
the reranker gets to work with; too narrow here defeats the point of
reranking).
node_postprocessors=[reranker] -> automatically reranks those 20 down
to the top 5 before building the prompt sent to Claude.
"""
retriever = index.as_retriever(similarity_top_k=20)
query_engine = RetrieverQueryEngine.from_args(
retriever=retriever,
node_postprocessors=[reranker],
)
return query_engine
# ---------------------------------------------------------------------------
# STREAMLIT INTEGRATION EXAMPLE
# ---------------------------------------------------------------------------
# import streamlit as st
# from rag_engine import build_index, build_query_engine
#
# @st.cache_resource
# def get_query_engine():
# index = build_index("data") # your content folder
# return build_query_engine(index)
#
# query_engine = get_query_engine()
#
# question = st.text_input("Ask a question")
# if question:
# response = query_engine.query(question)
# st.write(str(response))
#
# # Optional: show which sources were used, post-rerank
# with st.expander("Sources"):
# for node in response.source_nodes:
# st.write(f"- {node.node.metadata.get('file_name', 'unknown')} "
# f"(score: {node.score:.3f})") |