PDFBytesKnowledgeBase字节或 IO 流中读取 PDF 内容,将其转换为向量嵌入并加载到向量数据库中。这在使用动态生成的 PDF、API 响应或文件上传时非常有用,无需将文件保存到磁盘。

用法

我们在此示例中使用本地 LanceDB 数据库。请确保它正在运行

pip install pypdf
knowledge_base.py
from agno.agent import Agent
from agno.knowledge.pdf import PDFBytesKnowledgeBase
from agno.vectordb.lancedb import LanceDb

vector_db = LanceDb(
    table_name="recipes_async",
    uri="tmp/lancedb",
)

with open("data/pdfs/ThaiRecipes.pdf", "rb") as f:
    pdf_bytes = f.read()

knowledge_base = PDFBytesKnowledgeBase(
    pdfs=[pdf_bytes],
    vector_db=vector_db,
)
knowledge_base.load(recreate=False)  # 首次运行后注释掉此行

agent = Agent(
    knowledge=knowledge_base,
    search_knowledge=True,
)

agent.print_response("How to make Tom Kha Gai?", markdown=True)

参数

参数类型默认值描述
pdfsUnion[List[bytes], List[IO]]-PDF 内容列表(以字节或 IO 流形式)。
exclude_filesList[str][]要排除的文件模式列表(从基类继承)。
readerUnion[PDFReader, PDFImageReader]PDFReader()将 PDF 转换为向量数据库的 Document 的 PDFReader 或 PDFImageReader。

PDFBytesKnowledgeBaseAgentKnowledge 类的子类,拥有相同的参数访问权限。

开发者资源