如何使用 Quivr Assistants 从 PDF 生成文档摘要?
2026/9/11 14:11:52 网站建设 项目流程

如何使用 Quivr Assistants 从 PDF 生成文档摘要?

【免费下载链接】quivrOpiniated RAG for integrating GenAI in your apps 🧠 Focus on your product rather than the RAG. Easy integration in existing products with customisation! Any LLM: GPT4, Groq, Llama. Any Vectorstore: PGVector, Faiss. Any Files. Anyway you want.项目地址: https://gitcode.com/GitHub_Trending/qui/quivr

Quivr 的 Assistant 是另一种 Brain:它是一种 AI Agent,能针对特定输入生成可直接使用的输出。文档给出的典型例子就是“输入一个 PDF,收到该文档的摘要作为结果”(见 Quivr Assistants 文档 与 Summary 说明)。本文介绍如何用 Quivr 内置的 Summary Assistant 完成这个任务:先在本地把 Quivr 跑起来,然后通过界面或 API 上传一个 PDF,最终通过邮件收到摘要,或把摘要写入一个已有的 Brain。适用前提是你已安装 Docker 与 Docker Compose,并拥有 OpenAI API Key——后端摘要链路在源码中固定使用gpt-4o(见 summary.py 中ChatLiteLLM(model="gpt-4o", max_tokens=2000)),因此必须配置OPENAI_API_KEY

准备:在本地运行 Quivr

按 安装文档 的步骤操作:

# Step 1: 克隆仓库(如已在本地可直接跳过) git clone https://gitcode.com/GitHub_Trending/qui/quivr && cd quivr # Step 2: 复制 .env.example 为 .env cp .env.example .env # Step 3: 编辑 .env,填入 OPENAI_API_KEY vim .env # Step 4: 启动项目 cd backend && supabase start cd ../ docker compose pull docker compose up

启动后按文档给出的地址确认服务可用:

  • 前端应用:http://localhost:3000/login,使用admin@quivr.app/admin登录;
  • 后端 API 文档:http://localhost:5050/docs;
  • Supabase 控制台:http://localhost:54323。

如果登录页和 Swagger 文档都能访问,说明环境准备完成,可以继续。

在界面中使用 Summary Assistant

Quivr Assistants 文档 给出的操作流程是三步:

  1. 选择你的 Quivr Assistant:在 Assistants 页面(对应源码 frontend/app/assistants/page.tsx)选择 Summary。
  2. 提供输入:上传要摘要的 PDF 文件。Summary 的输入约束是“One document to summarize”,即只能有一个 PDF。
  3. 选择结果去向:选择通过邮件接收结果,或把结果上传到一个已存在的 Brain。

通过 API 生成摘要

如果你希望脚本化验证这条链路,可以直接调用后端接口。接口定义在 assistant_routes.py,两个路由都声明了AuthBearer()依赖,因此请求需要携带 Bearer 令牌;在 http://localhost:5050/docs 的 Swagger UI 中也可以直接尝试。

查看可用的 Assistant

curl "http://localhost:5050/assistants" \ -H "Authorization: Bearer YOUR_BEARER_TOKEN"

GET /assistants返回当前注册的 Assistant 列表,其中 Summary 条目的关键字段(来自 summary_inputs()):

  • name:Summary
  • description:Summarize a set of documents
  • input_description:One document to summarize
  • output_description:A summary of the document with key points and main themes
  • inputs.files: 要求一个keydoc_to_summarize、允许扩展名pdf的文件
  • outputs:email(通过邮件发送)与brain(类型uuid,即目标 Brain 的 uuid)

如果响应里能看到 Summary 条目,说明 Assistant 可用。

调用摘要接口

POST /assistant/process是 multipart 请求:表单字段input是一段 JSON 字符串(对应 InputAssistant,包含nameinputsoutputs三部分),files是上传的文件。示例:

curl -X POST "http://localhost:5050/assistant/process" \ -H "Authorization: Bearer YOUR_BEARER_TOKEN" \ -F 'input={"name":"summary","inputs":{"files":[{"key":"doc_to_summarize","value":"report.pdf"}]},"outputs":{"email":{"activated":true},"brain":{"activated":true,"value":"<BRAIN_UUID>"}}}' \ -F 'files=@report.pdf'

占位符替换说明:

  • YOUR_BEARER_TOKEN:你的 Bearer 认证令牌;
  • report.pdfinputJSON 里files[0].value必须与files=@后实际上传的文件名完全一致,且必须是唯一的 PDF;
  • <BRAIN_UUID>:接收结果的目标 Brain 的 uuid,仅当brain.activatedtrue时需要;如果只想通过邮件接收,把brainactivated设为false或省略即可,但emailbrain至少有一个激活。

结果验证与常见报错

成功路径:请求通过后,Summary Assistant 用UnstructuredPDFLoader解析 PDF,按chunk_size=1000chunk_overlap=100切分文档,用 Map-Reduce 链(MapReduceDocumentsChain,reduce 侧token_max=4000)先逐段总结、再汇总成最终摘要。reduce 提示词要求最终摘要包含SummaryKey Points加粗小节的 markdown 结构,并保留与原文相同的语言。随后摘要会被生成并处理成结果文件(create_and_upload_processed_file(content, filename, "Summary")),按你的选择通过邮件送达或写入所选 Brain。在邮箱或对应 Brain 中看到摘要文件,即为完成。

失败路径:接口校验失败时返回 HTTP 400,detail字段是对应错误文本。以下信息都来自 SummaryAssistant.check_input():

错误信息(原文)触发条件
No file was uploaded没有上传任何文件,或inputs.files[0].value为空
Only one file can be uploaded一次上传了多个文件,或inputs.files数组超过 1 项
No files key were given in the inputinputs.files缺失
The key of the file should be doc_to_summarize文件条目的key不是doc_to_summarize
The key of the file should be the same as the name of the filevalue与上传文件的实际文件名不一致
No output was selectedoutputs.email.activatedoutputs.brain.activated都没有设为true

限制

  • 仅支持pdf扩展名,且每次只能处理一个文件;
  • 摘要模型在源码中固定为gpt-4omax_tokens=2000),本地实例依赖.env中的OPENAI_API_KEY,不能换用本地模型;
  • 每次使用有默认计费Pricing(cost=20, description="Credits per use")(见 outputs.py),会消耗用户额度;
  • GET /assistants当前只返回 Summary 一个条目(differencecrawler分支在路由中尚未注册进列表),所以界面与 API 层面能直接使用的就是 Summary。

【免费下载链接】quivrOpiniated RAG for integrating GenAI in your apps 🧠 Focus on your product rather than the RAG. Easy integration in existing products with customisation! Any LLM: GPT4, Groq, Llama. Any Vectorstore: PGVector, Faiss. Any Files. Anyway you want.项目地址: https://gitcode.com/GitHub_Trending/qui/quivr

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询