设置

请遵循 Pinecone 设置指南 中的说明,快速开始使用 Pinecone。

pip install pinecone

我们目前暂不支持 Pinecone v6.x.x。我们正在积极努力实现兼容性。在此期间,我们建议使用 Pinecone v5.4.2 以获得最佳体验。

示例

agent_with_knowledge.py
import os
import typer
from typing import Optional
from rich.prompt import Prompt

from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.pineconedb import PineconeDb

api_key = os.getenv("PINECONE_API_KEY")
index_name = "thai-recipe-hybrid-search"

vector_db = PineconeDb(
    name=index_name,
    dimension=1536,
    metric="cosine",
    spec={"serverless": {"cloud": "aws", "region": "us-east-1"}},
    api_key=api_key,
    use_hybrid_search=True,
    hybrid_alpha=0.5,
)

knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=vector_db,
)

def pinecone_agent(user: str = "user"):
    run_id: Optional[str] = None

    agent = Agent(
        run_id=run_id,
        user_id=user,
        knowledge=knowledge_base,
        show_tool_calls=True,
        debug_mode=True,
    )

    if run_id is None:
        run_id = agent.run_id
        print(f"Started Run: {run_id}\n")
    else:
        print(f"Continuing Run: {run_id}\n")

    while True:
        message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
        if message in ("exit", "bye"):
            break
        agent.print_response(message)

if __name__ == "__main__":
    # 首次运行时注释掉此行
    knowledge_base.load(recreate=True, upsert=True)

    typer.run(pinecone_agent)

异步支持 ⚡

Pinecone 还支持异步操作,能够实现并发并带来更好的性能。

async_pinecone.py
import asyncio
from os import getenv

from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.pineconedb import PineconeDb

api_key = getenv("PINECONE_API_KEY")
index_name = "thai-recipe-index"

vector_db = PineconeDb(
    name=index_name,
    dimension=1536,
    metric="cosine",
    spec={"serverless": {"cloud": "aws", "region": "us-east-1"}},
    api_key=api_key,
)

knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=vector_db,
)

agent = Agent(
    knowledge=knowledge_base,
    # 在响应中显示工具调用
    show_tool_calls=True,
    # 启用代理搜索知识库
    search_knowledge=True,
    # 启用代理读取聊天记录
    read_chat_history=True,
)

if __name__ == "__main__":
    # 首次运行时注释掉此行
    asyncio.run(knowledge_base.aload(recreate=False, upsert=True))

    # 创建并使用代理
    asyncio.run(agent.aprint_response("How to make Tom Kha Gai", markdown=True))

在大型吞吐量应用程序中使用 aload()aprint_response() 方法配合 asyncio.run() 进行非阻塞操作。

PineconeDb 参数

参数类型默认值描述
namestr-Pinecone 索引的名称
dimensionint-嵌入向量的维度
specUnion[Dict, ServerlessSpec, PodSpec]-索引规格
embedderOptional[Embedder]None用于创建嵌入向量的 Embedder 实例(如果未提供,则默认为 OpenAIEmbedder)
metricOptional[str]"cosine"用于相似性搜索的度量标准
additional_headersOptional[Dict[str, str]]None要传递给 Pinecone 客户端的其他标头
pool_threadsOptional[int]1要用于 Pinecone 客户端的线程数
namespaceOptional[str]NonePinecone 索引的命名空间
timeoutOptional[int]NonePinecone 操作的超时时间
index_apiOptional[Any]NoneIndex API 对象
api_keyOptional[str]NonePinecone API 密钥
hostOptional[str]NonePinecone 主机
configOptional[Config]NonePinecone 配置
use_hybrid_searchboolFalse是否使用混合搜索
hybrid_alphafloat0.5混合搜索的 alpha 值

开发者资源