请遵循 Pinecone 设置指南 中的说明,快速开始使用 Pinecone。
我们目前暂不支持 Pinecone v6.x.x。我们正在积极努力实现兼容性。在此期间,我们建议使用 Pinecone v5.4.2 以获得最佳体验。
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 还支持异步操作,能够实现并发并带来更好的性能。
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 参数
| 参数 | 类型 | 默认值 | 描述 |
|---|
name | str | - | Pinecone 索引的名称 |
dimension | int | - | 嵌入向量的维度 |
spec | Union[Dict, ServerlessSpec, PodSpec] | - | 索引规格 |
embedder | Optional[Embedder] | None | 用于创建嵌入向量的 Embedder 实例(如果未提供,则默认为 OpenAIEmbedder) |
metric | Optional[str] | "cosine" | 用于相似性搜索的度量标准 |
additional_headers | Optional[Dict[str, str]] | None | 要传递给 Pinecone 客户端的其他标头 |
pool_threads | Optional[int] | 1 | 要用于 Pinecone 客户端的线程数 |
namespace | Optional[str] | None | Pinecone 索引的命名空间 |
timeout | Optional[int] | None | Pinecone 操作的超时时间 |
index_api | Optional[Any] | None | Index API 对象 |
api_key | Optional[str] | None | Pinecone API 密钥 |
host | Optional[str] | None | Pinecone 主机 |
config | Optional[Config] | None | Pinecone 配置 |
use_hybrid_search | bool | False | 是否使用混合搜索 |
hybrid_alpha | float | 0.5 | 混合搜索的 alpha 值 |
开发者资源