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 os
2import typer
3from typing import Optional
4from rich.prompt import Prompt
5
6from kern.agent import Agent
7from kern.knowledge.knowledge import Knowledge
8from kern.vectordb.qdrant import Qdrant
9
10api_key = os.getenv("QDRANT_API_KEY")
11qdrant_url = os.getenv("QDRANT_URL")
12collection_name = "thai-recipe-index"
13
14vector_db = Qdrant(
15 collection=collection_name,
16 url=qdrant_url,
17 api_key=api_key,
18)
19
20knowledge_base = Knowledge(
21 vector_db=vector_db,
22)
23
24def qdrant_agent(user: str = "user"):
25 agent = Agent(
26 knowledge=knowledge_base,
27 debug_mode=True,
28 )
29
30 while True:
31 message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
32 if message in ("exit", "bye"):
33 break
34 agent.print_response(message)
35
36if __name__ == "__main__":
37 knowledge_base.insert(
38 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
39 )
40
41 typer.run(qdrant_agent)

Async Support ⚡

Qdrant also supports asynchronous operations, enabling concurrency and leading to better performance.

1import asyncio
2
3from kern.agent import Agent
4from kern.knowledge.knowledge import Knowledge
5from kern.vectordb.qdrant import Qdrant
6
7COLLECTION_NAME = "thai-recipes"
8
9# Initialize Qdrant with local instance
10vector_db = Qdrant(
11 collection=COLLECTION_NAME,
12 url="http://localhost:6333"
13)
14
15# Create knowledge base
16knowledge_base = Knowledge(
17 vector_db=vector_db,
18)
19
20agent = Agent(knowledge=knowledge_base)
21
22if __name__ == "__main__":
23 # Load knowledge base asynchronously
24 asyncio.run(knowledge_base.ainsert(
25 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
26 )
27 )
28
29 # Create and use the agent asynchronously
30 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

NameTypeDefaultDescription
collectionstr-Name of the Qdrant collection
embedderEmbedderOpenAIEmbedder()Embedder for embedding the document contents
distanceDistanceDistance.cosineDistance metric for similarity search
locationOptional[str]NoneLocation of the Qdrant database
urlOptional[str]NoneURL of the Qdrant server
portOptional[int]6333Port number for the Qdrant server
grpc_portint6334gRPC port number for the Qdrant server
prefer_grpcboolFalseWhether to prefer gRPC over HTTP
httpsOptional[bool]NoneWhether to use HTTPS
api_keyOptional[str]NoneAPI key for authentication
prefixOptional[str]NonePrefix for the Qdrant API
timeoutOptional[float]NoneTimeout for Qdrant operations
hostOptional[str]NoneHost address for the Qdrant server
pathOptional[str]NonePath to the Qdrant database
fastembed_kwargsOptional[dict]NoneAdditional kwargs passed to SparseTextEmbedding.