Qdrant Hybrid Search
Code
1import typer2from kern.agent import Agent3from kern.knowledge.knowledge import Knowledge4from kern.vectordb.qdrant import Qdrant5from kern.vectordb.search import SearchType6from rich.prompt import Prompt78COLLECTION_NAME = "thai-recipes"910vector_db = Qdrant(11 collection=COLLECTION_NAME,12 url="http://localhost:6333",13 search_type=SearchType.hybrid,14)1516knowledge = Knowledge(17 name="My Qdrant Vector Knowledge Base",18 vector_db=vector_db,19)2021knowledge.insert(22 name="Recipes",23 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",24 metadata={"doc_type": "recipe_book"},25)2627def qdrantdb_agent(user: str = "user"):28 agent = Agent(29 user_id=user,30 knowledge=knowledge,31 search_knowledge=True,32 )3334 while True:35 message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")36 if message in ("exit", "bye"):37 break38 agent.print_response(message)3940if __name__ == "__main__":41 typer.run(qdrantdb_agent)Note
To use hybrid search without internet access,
pre-cache the model and pass fastembed_kwargs={"local_files_only": True, "cache_dir": "/path/to/local/model/cache"}
to load models from the cache and disable downloads:
1Qdrant(2 collection="my_collection",3 search_type=SearchType.hybrid,4 fastembed_kwargs={5 "local_files_only": True,6 "cache_dir": "/path/to/cached/models",7 },8)Usage
Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall dependencies
1uv pip install -U qdrant-client typer rich pypdf openai kern-aiRun Qdrant
1docker run -d --name qdrant -p 6333:6333 qdrant/qdrant:latestRun Agent
1python cookbook/08_knowledge/vector_db/qdrant_db/qdrant_db_hybrid_search.py