了解如何使用用户 ID、文档类型和年份等元数据在传统 RAG 中过滤知识。本示例演示了如何设置带筛选器的知识库并进行有效查询。
from agno.agent import Agent
from agno.knowledge.text import TextKnowledgeBase
from agno.utils.media import (
SampleDataFileExtension,
download_knowledge_filters_sample_data,
)
from agno.vectordb.lancedb import LanceDb
# 下载所有示例简历并获取其路径
downloaded_cv_paths = download_knowledge_filters_sample_data(
num_files=5, file_extension=SampleDataFileExtension.TXT
)
# 初始化 LanceDB
# 默认情况下,它将数据存储在 /tmp/lancedb
vector_db = LanceDb(
table_name="recipes",
uri="tmp/lancedb", # 您可以更改此路径以将数据存储在其他位置
)
# 步骤 1:使用文档和元数据初始化知识库
knowledge_base = TextKnowledgeBase(
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)
# 步骤 2:使用不同的过滤器组合查询知识库
# 选项 1:在 Agent 上设置过滤器
agent = Agent(
knowledge=knowledge_base,
search_knowledge=False,
add_references=True,
knowledge_filters={"user_id": "jordan_mitchell"},
)
# 查询 Jordan Mitchell 的经验和技能
agent.print_response(
"Tell me about Jordan Mitchell's experience and skills",
markdown=True,
)
# 选项 2:在 run/print_response 上设置过滤器
# agent = Agent(
# knowledge=knowledge_base,
# add_references=True,
# search_knowledge=False,
# )
# 将 Taylor Brooks 作为候选人进行查询
# agent.print_response(
# "Tell me about Taylor Brooks as a candidate",
# knowledge_filters={"user_id": "taylor_brooks"},
# markdown=True,
# )
安装库
pip install -U agno lancedb openai
运行示例
python filtering-traditional-RAG.py