了解如何使用用户特定的元数据通过 PDF 文档在 Pinecone 中过滤知识库搜索。
from os import getenv
from agno.agent import Agent
from agno.knowledge.pdf import PDFKnowledgeBase
from agno.utils.media import (
SampleDataFileExtension,
download_knowledge_filters_sample_data,
)
from agno.vectordb.pineconedb import PineconeDb
# 下载所有示例简历并获取其路径
downloaded_cv_paths = download_knowledge_filters_sample_data(
num_files=5, file_extension=SampleDataFileExtension.PDF
)
# 初始化 Pinecone
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,
)
# 步骤 1:使用文档和元数据初始化知识库
knowledge_base = PDFKnowledgeBase(
path=[
{
"path": downloaded_cv_paths[0],
"metadata": {
"user_id": "jordan_mitchell",
"document_type": "cv",
"year": 2025,
},
},
{
"path": downloaded_cv_paths[1],
"metadata": {
"user_id": "taylor_brooks",
"document_type": "cv",
"year": 2025,
},
},
{
"path": downloaded_cv_paths[2],
"metadata": {
"user_id": "morgan_lee",
"document_type": "cv",
"year": 2025,
},
},
{
"path": downloaded_cv_paths[3],
"metadata": {
"user_id": "casey_jordan",
"document_type": "cv",
"year": 2025,
},
},
{
"path": downloaded_cv_paths[4],
"metadata": {
"user_id": "alex_rivera",
"document_type": "cv",
"year": 2025,
},
},
],
vector_db=vector_db,
)
# 将所有文档加载到向量数据库中
knowledge_base.load(recreate=True, upsert=True)
# 步骤 2:使用不同的过滤组合查询知识库
# ------------------------------------------------------------------------------
agent = Agent(
knowledge=knowledge_base,
search_knowledge=True,
)
agent.print_response(
"Tell me about Jordan Mitchell's experience and skills",
knowledge_filters={"user_id": "hey"},
markdown=True,
)
安装库
pip install -U agno pinecone pinecone-text openai
运行示例
python cookbook/agent_concepts/knowledge/filters/filtering_pinecone.py