Qdrant Vector Database
Use Qdrant as a vector database for your Knowledge Base.
Setup
Follow the instructions in the Qdrant Setup Guide to install Qdrant locally. Here is a guide to get API keys: Qdrant API Keys.
Example
1import os2import typer3from typing import Optional4from rich.prompt import Prompt56from kern.agent import Agent7from kern.knowledge.knowledge import Knowledge8from kern.vectordb.qdrant import Qdrant910api_key = os.getenv("QDRANT_API_KEY")11qdrant_url = os.getenv("QDRANT_URL")12collection_name = "thai-recipe-index"1314vector_db = Qdrant(15 collection=collection_name,16 url=qdrant_url,17 api_key=api_key,18)1920knowledge_base = Knowledge(21 vector_db=vector_db,22)2324def qdrant_agent(user: str = "user"):25 agent = Agent(26 knowledge=knowledge_base,27 debug_mode=True,28 )2930 while True:31 message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")32 if message in ("exit", "bye"):33 break34 agent.print_response(message)3536if __name__ == "__main__":37 knowledge_base.insert(38 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"39 )4041 typer.run(qdrant_agent)Async Support ⚡
Qdrant also supports asynchronous operations, enabling concurrency and leading to better performance.
1import asyncio23from kern.agent import Agent4from kern.knowledge.knowledge import Knowledge5from kern.vectordb.qdrant import Qdrant67COLLECTION_NAME = "thai-recipes"89# Initialize Qdrant with local instance10vector_db = Qdrant(11 collection=COLLECTION_NAME,12 url="http://localhost:6333"13)1415# Create knowledge base16knowledge_base = Knowledge(17 vector_db=vector_db,18)1920agent = Agent(knowledge=knowledge_base)2122if __name__ == "__main__":23 # Load knowledge base asynchronously24 asyncio.run(knowledge_base.ainsert(25 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"26 )27 )2829 # Create and use the agent asynchronously30 asyncio.run(agent.aprint_response("How to make Tom Kha Gai", markdown=True))Tip
Using aload() and aprint_response() with asyncio provides non-blocking operations, making your application more responsive under load.
Qdrant Params
| Name | Type | Default | Description |
|---|---|---|---|
collection | str | - | Name of the Qdrant collection |
embedder | Embedder | OpenAIEmbedder() | Embedder for embedding the document contents |
distance | Distance | Distance.cosine | Distance metric for similarity search |
location | Optional[str] | None | Location of the Qdrant database |
url | Optional[str] | None | URL of the Qdrant server |
port | Optional[int] | 6333 | Port number for the Qdrant server |
grpc_port | int | 6334 | gRPC port number for the Qdrant server |
prefer_grpc | bool | False | Whether to prefer gRPC over HTTP |
https | Optional[bool] | None | Whether to use HTTPS |
api_key | Optional[str] | None | API key for authentication |
prefix | Optional[str] | None | Prefix for the Qdrant API |
timeout | Optional[float] | None | Timeout for Qdrant operations |
host | Optional[str] | None | Host address for the Qdrant server |
path | Optional[str] | None | Path to the Qdrant database |
fastembed_kwargs | Optional[dict] | None | Additional kwargs passed to SparseTextEmbedding. |