用法
本示例中使用本地 PgVector 数据库。请确保它正在运行
knowledge_base.py
knowledge_base 与 Agent 一起使用:
agent.py
JSONKnowledgeBase 也支持异步加载。
async_knowledge_base.py
参数
JSONKnowledgeBase 是 AgentKnowledge 类的一个子类,可以访问相同的参数。
开发者资源
- 查看 同步加载 Cookbook
- 查看 异步加载 Cookbook
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
了解如何在知识库中使用本地 JSON 文件。
from agno.knowledge.json import JSONKnowledgeBase
from agno.vectordb.pgvector import PgVector
knowledge_base = JSONKnowledgeBase(
path="data/json",
# 表名: ai.json_documents
vector_db=PgVector(
table_name="json_documents",
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
),
)
knowledge_base 与 Agent 一起使用:
from agno.agent import Agent
from knowledge_base import knowledge_base
agent = Agent(
knowledge=knowledge_base,
search_knowledge=True,
)
agent.knowledge.load(recreate=False)
agent.print_response("Ask me about something from the knowledge base")
pip install qdrant-client
import asyncio
from pathlib import Path
from agno.agent import Agent
from agno.knowledge.json import JSONKnowledgeBase
from agno.vectordb.qdrant import Qdrant
COLLECTION_NAME = "json-reader"
vector_db = Qdrant(collection=COLLECTION_NAME, url="http://localhost:6333")
knowledge_base = JSONKnowledgeBase(
path=Path("tmp/docs"),
vector_db=vector_db,
num_documents=5, # 搜索时返回的文档数量
)
# 使用 knowledge_base 初始化 Agent
agent = Agent(
knowledge=knowledge_base,
search_knowledge=True,
)
if __name__ == "__main__":
# 首次运行时注释掉此行
asyncio.run(knowledge_base.aload(recreate=False))
# 创建并使用 Agent
asyncio.run(
agent.aprint_response(
"Ask anything from the json knowledge base", markdown=True
)
)
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
path | Union[str, Path] | - | 指向 JSON 文件的路径。可以指向单个 JSON 文件或 JSON 文件目录。 |
reader | JSONReader | JSONReader() | 一个 JSONReader,用于将 JSON 文件转换为向量数据库的 Documents。 |
JSONKnowledgeBase 是 AgentKnowledge 类的一个子类,可以访问相同的参数。