Pinecone Vector Database
Use Pinecone as a vector database for your Knowledge Base.
Setup
Follow the instructions in the Pinecone Setup Guide to get started quickly with Pinecone.
1uv pip install pineconeInfo
We do not yet support Pinecone v6.x.x. We are actively working to achieve compatibility. In the meantime, we recommend using Pinecone v5.4.2 for the best experience.
Example
1import os2import typer3from typing import Optional4from rich.prompt import Prompt56from kern.agent import Agent7from kern.knowledge.knowledge import Knowledge8from kern.vectordb.pineconedb import PineconeDb910api_key = os.getenv("PINECONE_API_KEY")11index_name = "thai-recipe-hybrid-search"1213vector_db = PineconeDb(14 name=index_name,15 dimension=1536,16 metric="cosine",17 spec={"serverless": {"cloud": "aws", "region": "us-east-1"}},18 api_key=api_key,19 use_hybrid_search=True,20 hybrid_alpha=0.5,21)2223knowledge_base = Knowledge(24 vector_db=vector_db,25)2627def pinecone_agent(user: str = "user"):28 agent = Agent(29 knowledge=knowledge_base,30 debug_mode=True,31 )3233 while True:34 message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")35 if message in ("exit", "bye"):36 break37 agent.print_response(message)3839if __name__ == "__main__":40 # Comment out after first run41 knowledge_base.insert(42 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"43 )4445 typer.run(pinecone_agent)Async Support ⚡
Pinecone also supports asynchronous operations, enabling concurrency and leading to better performance.
1import asyncio2from os import getenv34from kern.agent import Agent5from kern.knowledge.knowledge import Knowledge6from kern.vectordb.pineconedb import PineconeDb78api_key = getenv("PINECONE_API_KEY")9index_name = "thai-recipe-index"1011vector_db = PineconeDb(12 name=index_name,13 dimension=1536,14 metric="cosine",15 spec={"serverless": {"cloud": "aws", "region": "us-east-1"}},16 api_key=api_key,17)1819knowledge_base = Knowledge(20 vector_db=vector_db,21)2223agent = Agent(24 knowledge=knowledge_base,25 # Enable the agent to search the knowledge base26 search_knowledge=True,27 # Enable the agent to read the chat history28 read_chat_history=True,29)3031if __name__ == "__main__":32 # Load knowledge base asynchronously33 asyncio.run(knowledge_base.ainsert(34 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"35 )36 )3738 # Create and use the agent asynchronously39 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.
PineconeDb Params
| Parameter | Type | Default | Description |
|---|---|---|---|
name | str | - | The name of the Pinecone index |
dimension | int | - | The dimension of the embeddings |
spec | Union[Dict, ServerlessSpec, PodSpec] | - | The index spec |
embedder | Optional[Embedder] | None | Embedder instance for creating embeddings (defaults to OpenAIEmbedder if not provided) |
metric | Optional[str] | "cosine" | The metric used for similarity search |
additional_headers | Optional[Dict[str, str]] | None | Additional headers to pass to the Pinecone client |
pool_threads | Optional[int] | 1 | The number of threads to use for the Pinecone client |
namespace | Optional[str] | None | The namespace for the Pinecone index |
timeout | Optional[int] | None | The timeout for Pinecone operations |
index_api | Optional[Any] | None | The Index API object |
api_key | Optional[str] | None | The Pinecone API key |
host | Optional[str] | None | The Pinecone host |
config | Optional[Config] | None | The Pinecone config |
use_hybrid_search | bool | False | Whether to use hybrid search |
hybrid_alpha | float | 0.5 | The alpha value for hybrid search |