LanceDB Vector Database
Use LanceDB as a vector database for your Knowledge Base.
Setup
1uv pip install lancedbExample
1import typer2from typing import Optional3from rich.prompt import Prompt45from kern.agent import Agent6from kern.knowledge.knowledge import Knowledge7from kern.vectordb.lancedb import LanceDb8from kern.vectordb.search import SearchType910# LanceDB Vector DB11vector_db = LanceDb(12 table_name="recipes",13 uri="/tmp/lancedb",14 search_type=SearchType.keyword,15)1617# Knowledge Base18knowledge_base = Knowledge(19 vector_db=vector_db,20)2122def lancedb_agent(user: str = "user"):23 agent = Agent(24 knowledge=knowledge_base,25 debug_mode=True,26 )2728 while True:29 message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")30 if message in ("exit", "bye"):31 break32 agent.print_response(message, session_id=f"{user}_session")3334if __name__ == "__main__":35 # Comment out after first run36 knowledge_base.insert(37 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"38 )3940 typer.run(lancedb_agent)Async Support ⚡
LanceDB also supports asynchronous operations, enabling concurrency and leading to better performance.
1# install lancedb - `pip install lancedb`2import asyncio34from kern.agent import Agent5from kern.knowledge.knowledge import Knowledge6from kern.vectordb.lancedb import LanceDb78# Initialize LanceDB9vector_db = LanceDb(10 table_name="recipes",11 uri="tmp/lancedb", # You can change this path to store data elsewhere12)1314# Create knowledge base15knowledge_base = Knowledge(16 vector_db=vector_db,17)18agent = Agent(knowledge=knowledge_base, debug_mode=True)1920if __name__ == "__main__":21 # Load knowledge base asynchronously22 asyncio.run(knowledge_base.ainsert(23 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"24 )25 )2627 # Create and use the agent asynchronously28 asyncio.run(agent.aprint_response("How to make Tom Kha Gai", markdown=True))Tip
Use aload() and aprint_response() methods with asyncio.run() for non-blocking operations in high-throughput applications.
LanceDb Params
| Parameter | Type | Default | Description |
|---|---|---|---|
uri | str | - | The URI to connect to. |
table | LanceTable | - | The Lance table to use. |
table_name | str | - | The name of the table to use. |
connection | DBConnection | - | The database connection to use. |
api_key | str | - | The API key to use. |
embedder | Embedder | - | The embedder to use. |
search_type | SearchType | vector | The search type to use. |
distance | Distance | cosine | The distance to use. |
nprobes | int | - | The number of probes to use. More Info |
reranker | Reranker | - | The reranker to use. More Info |
use_tantivy | bool | - | Whether to use tantivy. |