Qwen-Agent 内置RAG学习

1、概述

Qwen-Agent的内置rag系统,默认基于BM25 关键词检索算法实现文档匹配,是轻量级内存式RAG,Assistant 传入files参数即激活内置 RAG,适合小批量文件处理。

【内存加载流程】

1、文档解析与分块

doc_parser(qwen_agent\tools\simple_doc_parser.py)按文件后缀pdf、txt、docx等解析文件,按默认500字符将文本切分成多个文本块,生成{page_content, metadata}的字典列表

2、内存存储

分块列表存储在内存中,不持久化到磁盘

3、检索执行

retrieval遍历内存中的分块列表,默认使用BM25打分排序(不支持向量检索),返回Top-N相关块

4、模型推理

将得分最高的若干个文本块拼接到prompt,给大模型生成回答

2、Qwen-Agent的多文件 RAG 问答程序

python 复制代码
import os
from qwen_agent.agents import Assistant

llm_cfg = {
    "model": "qwen-turbo",
    "api_key": os.getenv('DASHSCOPE_API_KEY'),
    "temperature": 0.8
}

system_instruction = "你是一个文档问答助手,必须根据提供的文档内容回答问题,不要编造答案。"

# 获取文件夹下所有文件
file_dir = os.path.join('./', 'docs')
files = []
if os.path.exists(file_dir):
    # 遍历目录下的所有文件
    for file in os.listdir(file_dir):
        file_path = os.path.join(file_dir, file)
        if os.path.isfile(file_path):  # 确保是文件而不是目录
            files.append(file_path)
print('files=', files)

bot = Assistant(
    llm=llm_cfg,
    system_message=system_instruction,
    function_list=[],
    files=files
)

messages = []
query = "介绍下雇主责任险"
# 将用户请求添加到聊天历史。
messages.append({'role': 'user', 'content': query})
response = []
current_index = 0

print("助手回答:", end='')
for response in bot.run(messages=messages):
    if current_index == 0:
        # 尝试获取并打印召回的文档内容
        if hasattr(bot, 'retriever') and bot.retriever:
            print("\n===== 召回的文档内容 =====")
            retrieved_docs = bot.retriever.retrieve(query)
            if retrieved_docs:
                for i, doc in enumerate(retrieved_docs):
                    print(f"\n文档片段 {i+1}:")
                    print(f"内容: {doc.page_content}")
                    print(f"元数据: {doc.metadata}")
            else:
                print("没有召回任何文档内容")
            print("===========================\n")

    current_response = response[0]['content'][current_index:]
    current_index = len(response[0]['content'])
    print(current_response, end='')

# 将机器人的回应添加到聊天历史。
messages.append({'role': 'assistant', 'content': response[-1]['content']})

【运行如图】

文档解析:Start parsing / Finished parsing

文本分块:Start chunking / Finished chunking

BM25检索:Building prefix dict from the default dictionary

第一次运行在工作目录下会有workspace,里面保存了解析阶段的缓存,Qwen-Agent 下次启动程序时,会优先读取 workspace 里面的缓存文件。

相关推荐
妙妙屋(zy)13 小时前
Claude Code+CC-Switch+CC-Connect+飞书使用教程
ai
小七-七牛开发者16 小时前
Coding Agent 规则管理:CLAUDE.md、Skills、Hooks、Subagents 到底怎么选?
ai·大模型·agent·claude·token·loop·mcp·claudecode·ai coding
doiito1 天前
左脚踩右脚:让 LLM 自进化的 Agent 轨迹训练法——为什么它能补上主流范式的最后一块拼图
ai·系统设计
带刺的坐椅1 天前
从 Claude Code 隐私争议,看 SolonCode 的设计选择
ai·llm·agent·claudecode·soloncode·codingplan
lincats1 天前
Claude Code项目越写越乱?这套清理流程能救你
ai·ai agent·claude code
云燕实验室CloudLab2 天前
《AI开始"抱团"思考了!多智能体 + 思维图到底有多强?》
ai·学习工具·智慧学伴
小七-七牛开发者2 天前
论文解读:DeepSeek DSpark 在真实高并发推理服务中,如何保证 Token 生成又好又快?
ai·大模型·编程·ai coding
doiito2 天前
【Agent Harness】Gliding Horse 核心设计理念,不跟风开发自己的AI Agent
ai·rust·架构设计·系统设计·ai agent
doiito3 天前
【Agent Harness】Gliding Horse 的 L2 作战地图:让多 Agent 协作从“摸黑”变成“透明”
ai·rust·架构设计·系统设计·ai agent
xiezhr3 天前
逛GitHub发现一款免费带有AI功能的数据库管理工具DBX
ai·开源软件·自然语言·数据库管理工具