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 pinecone
Info

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 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.pineconedb import PineconeDb
9
10api_key = os.getenv("PINECONE_API_KEY")
11index_name = "thai-recipe-hybrid-search"
12
13vector_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)
22
23knowledge_base = Knowledge(
24 vector_db=vector_db,
25)
26
27def pinecone_agent(user: str = "user"):
28 agent = Agent(
29 knowledge=knowledge_base,
30 debug_mode=True,
31 )
32
33 while True:
34 message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
35 if message in ("exit", "bye"):
36 break
37 agent.print_response(message)
38
39if __name__ == "__main__":
40 # Comment out after first run
41 knowledge_base.insert(
42 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
43 )
44
45 typer.run(pinecone_agent)

Async Support ⚡

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

1import asyncio
2from os import getenv
3
4from kern.agent import Agent
5from kern.knowledge.knowledge import Knowledge
6from kern.vectordb.pineconedb import PineconeDb
7
8api_key = getenv("PINECONE_API_KEY")
9index_name = "thai-recipe-index"
10
11vector_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)
18
19knowledge_base = Knowledge(
20 vector_db=vector_db,
21)
22
23agent = Agent(
24 knowledge=knowledge_base,
25 # Enable the agent to search the knowledge base
26 search_knowledge=True,
27 # Enable the agent to read the chat history
28 read_chat_history=True,
29)
30
31if __name__ == "__main__":
32 # Load knowledge base asynchronously
33 asyncio.run(knowledge_base.ainsert(
34 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
35 )
36 )
37
38 # Create and use the agent asynchronously
39 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

ParameterTypeDefaultDescription
namestr-The name of the Pinecone index
dimensionint-The dimension of the embeddings
specUnion[Dict, ServerlessSpec, PodSpec]-The index spec
embedderOptional[Embedder]NoneEmbedder instance for creating embeddings (defaults to OpenAIEmbedder if not provided)
metricOptional[str]"cosine"The metric used for similarity search
additional_headersOptional[Dict[str, str]]NoneAdditional headers to pass to the Pinecone client
pool_threadsOptional[int]1The number of threads to use for the Pinecone client
namespaceOptional[str]NoneThe namespace for the Pinecone index
timeoutOptional[int]NoneThe timeout for Pinecone operations
index_apiOptional[Any]NoneThe Index API object
api_keyOptional[str]NoneThe Pinecone API key
hostOptional[str]NoneThe Pinecone host
configOptional[Config]NoneThe Pinecone config
use_hybrid_searchboolFalseWhether to use hybrid search
hybrid_alphafloat0.5The alpha value for hybrid search