|
| 1 | +""" |
| 2 | +Hybrid Search Example |
| 3 | +===================== |
| 4 | +Demonstrates the three search modes available in RAGLight: |
| 5 | + - "semantic" : vector similarity only (default) |
| 6 | + - "bm25" : keyword-based BM25 search only |
| 7 | + - "hybrid" : BM25 + semantic combined via Reciprocal Rank Fusion (RRF) |
| 8 | +
|
| 9 | +Requirements: |
| 10 | + - Ollama running locally with llama3 (or any model you prefer) |
| 11 | + - rank_bm25 installed (pip install raglight includes it) |
| 12 | +""" |
| 13 | + |
| 14 | +import uuid |
| 15 | +from raglight.rag.builder import Builder |
| 16 | +from raglight.config.settings import Settings |
| 17 | +from dotenv import load_dotenv |
| 18 | + |
| 19 | +load_dotenv() |
| 20 | +Settings.setup_logging() |
| 21 | + |
| 22 | +persist_directory = "./hybridDb" |
| 23 | +model_embeddings = Settings.DEFAULT_EMBEDDINGS_MODEL |
| 24 | +model_name = "llama3.1:8b" |
| 25 | +collection_name = str(uuid.uuid4()) |
| 26 | +data_path = "./src/raglight" # folder to ingest — adjust to your own documents |
| 27 | + |
| 28 | +# ── 1. Semantic search (default behaviour) ────────────────────────────────── |
| 29 | +print("\n=== Semantic search ===") |
| 30 | +rag_semantic = ( |
| 31 | + Builder() |
| 32 | + .with_embeddings(Settings.HUGGINGFACE, model_name=model_embeddings) |
| 33 | + .with_vector_store( |
| 34 | + Settings.CHROMA, |
| 35 | + persist_directory=persist_directory, |
| 36 | + collection_name=collection_name, |
| 37 | + search_type=Settings.SEARCH_SEMANTIC, # default — can be omitted |
| 38 | + ) |
| 39 | + .with_llm(Settings.OLLAMA, model_name=model_name, system_prompt=Settings.DEFAULT_SYSTEM_PROMPT) |
| 40 | + .build_rag(k=5) |
| 41 | +) |
| 42 | +rag_semantic.vector_store.ingest(data_path=data_path) |
| 43 | +response = rag_semantic.generate("How do I create a RAG pipeline with RAGLight?") |
| 44 | +print(response) |
| 45 | + |
| 46 | +# ── 2. BM25-only search ────────────────────────────────────────────────────── |
| 47 | +print("\n=== BM25 search ===") |
| 48 | +rag_bm25 = ( |
| 49 | + Builder() |
| 50 | + .with_embeddings(Settings.HUGGINGFACE, model_name=model_embeddings) |
| 51 | + .with_vector_store( |
| 52 | + Settings.CHROMA, |
| 53 | + persist_directory=persist_directory, |
| 54 | + collection_name=collection_name + "_bm25", |
| 55 | + search_type=Settings.SEARCH_BM25, |
| 56 | + ) |
| 57 | + .with_llm(Settings.OLLAMA, model_name=model_name, system_prompt=Settings.DEFAULT_SYSTEM_PROMPT) |
| 58 | + .build_rag(k=5) |
| 59 | +) |
| 60 | +rag_bm25.vector_store.ingest(data_path=data_path) |
| 61 | +response = rag_bm25.generate("What classes are available in the vectorstore module?") |
| 62 | +print(response) |
| 63 | + |
| 64 | +# ── 3. Hybrid search (BM25 + semantic via RRF) ─────────────────────────────── |
| 65 | +print("\n=== Hybrid search (RRF) ===") |
| 66 | +rag_hybrid = ( |
| 67 | + Builder() |
| 68 | + .with_embeddings(Settings.HUGGINGFACE, model_name=model_embeddings) |
| 69 | + .with_vector_store( |
| 70 | + Settings.CHROMA, |
| 71 | + persist_directory=persist_directory, |
| 72 | + collection_name=collection_name + "_hybrid", |
| 73 | + search_type=Settings.SEARCH_HYBRID, |
| 74 | + alpha=0.5, # weight of semantic vs BM25 in the RRF merge (0=BM25 only, 1=semantic only) |
| 75 | + ) |
| 76 | + .with_llm(Settings.OLLAMA, model_name=model_name, system_prompt=Settings.DEFAULT_SYSTEM_PROMPT) |
| 77 | + .build_rag(k=5) |
| 78 | +) |
| 79 | +rag_hybrid.vector_store.ingest(data_path=data_path) |
| 80 | +response = rag_hybrid.generate("Explain the Builder pattern used in RAGLight") |
| 81 | +print(response) |
| 82 | + |
| 83 | +# ── 4. Hybrid search via VectorStoreConfig (high-level API) ───────────────── |
| 84 | +print("\n=== Hybrid search via RAGPipeline (high-level API) ===") |
| 85 | +from raglight.rag.simple_rag_api import RAGPipeline |
| 86 | +from raglight.config.rag_config import RAGConfig |
| 87 | +from raglight.config.vector_store_config import VectorStoreConfig |
| 88 | +from raglight.models.data_source_model import FolderSource |
| 89 | + |
| 90 | +vector_store_config = VectorStoreConfig( |
| 91 | + embedding_model=Settings.DEFAULT_EMBEDDINGS_MODEL, |
| 92 | + provider=Settings.HUGGINGFACE, |
| 93 | + database=Settings.CHROMA, |
| 94 | + persist_directory=persist_directory, |
| 95 | + collection_name=collection_name + "_api", |
| 96 | + search_type=Settings.SEARCH_HYBRID, # <-- hybrid mode |
| 97 | + hybrid_alpha=0.5, |
| 98 | +) |
| 99 | + |
| 100 | +config = RAGConfig( |
| 101 | + llm=model_name, |
| 102 | + provider=Settings.OLLAMA, |
| 103 | + k=5, |
| 104 | + knowledge_base=[FolderSource(path=data_path)], |
| 105 | +) |
| 106 | + |
| 107 | +pipeline = RAGPipeline(config, vector_store_config) |
| 108 | +pipeline.build() |
| 109 | +response = pipeline.generate("How does the ChromaVS vector store work?") |
| 110 | +print(response) |
0 commit comments