Qwen-Agent 是阿里通义千问团队开源的基于 Qwen 大语言模型的 Agent 开发框架,为开发者提供构建具备指令遵循、工具调用、任务规划与记忆管理能力的 AI 智能体应用的完整能力栈,也是 Qwen Chat 的后端支撑框架。
之前我们用LangChain搭建了一个简单的RAG例子,下面用Qwen-Agent来搭建RAG,这里举了两个例子。
1、环境安装
这里用conda新建一个环境
conda create -n qwen-agent python=3.11conda activate qwen-agent
然后安装依赖
pip install -U "qwen-agent[rag,code_interpreter,gui,mcp]"
或者,使用 pip install -U qwen-agent 来安装最小依赖。
可使用双括号指定如下的可选依赖:[gui] 用于提供基于 Gradio 的 GUI 支持;[rag] 用于支持 RAG;[code_interpreter] 用于提供代码解释器相关支持;[mcp] 用于支持 MCP。
这里只是简单使用,就直接安装全部依赖了。
2、例子1:简单的使用
import loggingimport ioimport osfrom qwen_agent.agents import Assistant# 创建一个自定义的日志处理器来捕获日志输出class LogCapture:def __init__(self):self.log_capture_string = io.StringIO()self.log_handler = logging.StreamHandler(self.log_capture_string)self.log_handler.setLevel(logging.INFO)self.log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')self.log_handler.setFormatter(self.log_formatter)# 获取 qwen_agent 的日志记录器self.logger = logging.getLogger('qwen_agent_logger')self.logger.setLevel(logging.INFO)self.logger.addHandler(self.log_handler)# 也可以捕获根日志记录器的输出self.root_logger = logging.getLogger()self.root_logger.setLevel(logging.INFO)self.root_logger.addHandler(self.log_handler)def get_log(self):return self.log_capture_string.getvalue()def clear_log(self):self.log_capture_string.truncate(0)self.log_capture_string.seek(0)# 初始化日志捕获器log_capture = LogCapture()# 步骤 1:配置您所使用的 LLM。llm_cfg = {# 使用 DashScope 提供的模型服务:'model': 'qwen-max','model_server': 'dashscope','api_key': os.getenv("DASHSCOPE_API_KEY"),'generate_cfg': {'top_p': 0.8}}# 步骤 2:创建一个智能体。这里我们以 `Assistant` 智能体为例,它能够使用工具并读取文件。system_instruction = ''tools = []files = ['G:\\AI\\workspace\\vscode\\202601\\123.pdf'] # 给智能体一个 PDF 文件阅读。# 清除之前的日志log_capture.clear_log()# 创建智能体bot = Assistant(llm=llm_cfg,system_message=system_instruction,function_list=tools,files=files)# 步骤 3:作为聊天机器人运行智能体。messages = [] # 这里储存聊天历史。query = "客户经理被客户投诉一次,扣多少分?"# 将用户请求添加到聊天历史。messages.append({'role': 'user', 'content': query})response = []current_index = 0# 运行智能体for response in bot.run(messages=messages):# 在第一次响应时,分析日志以查找召回的文档内容if current_index == 0:# 获取日志内容log_content = log_capture.get_log()print("\n===== 从日志中提取的检索信息 =====")# 查找与检索相关的日志行retrieval_logs = [line for line in log_content.split('\n')if any(keyword in line.lower() for keyword in['retriev', 'search', 'chunk', 'document', 'ref', 'token'])]# 打印检索相关的日志for log_line in retrieval_logs:print(log_line)# 尝试从日志中提取文档内容# 通常在日志中会有类似 "retrieved document: ..." 或 "content: ..." 的行content_logs = [line for line in log_content.split('\n')if any(keyword in line.lower() for keyword in['content', 'text', 'document', 'chunk'])]print("\n===== 可能包含文档内容的日志 =====")for log_line in content_logs:print(log_line)print("===========================\n")current_response = response[0]['content'][current_index:]current_index = len(response[0]['content'])print(current_response, end='')# 将机器人的回应添加到聊天历史。messages.extend(response)# 运行结束后,分析完整的日志print("\n\n===== 运行结束后的完整日志分析 =====")log_content = log_capture.get_log()# 尝试从日志中提取更多信息print("\n1. 关键词提取:")keyword_logs = [line for line in log_content.split('\n') if 'keywords' in line.lower()]for log_line in keyword_logs:print(log_line)print("\n2. 文档处理:")doc_logs = [line for line in log_content.split('\n') if 'doc' in line.lower() or 'chunk' in line.lower()]for log_line in doc_logs:print(log_line)print("\n3. 检索相关:")retrieval_logs = [line for line in log_content.split('\n') if 'retriev' in line.lower() or 'search' in line.lower() or 'ref' in line.lower()]for log_line in retrieval_logs:print(log_line)print("\n4. 可能包含文档内容的日志:")content_logs = [line for line in log_content.split('\n') if 'content:' in line.lower() or 'text:' in line.lower()]for log_line in content_logs:print(log_line)print("===========================\n")
3、例子2:RAG助手
import loggingimport ioimport osfrom qwen_agent.agents import Assistantdef get_file_list(folder_path):# 初始化文件列表file_list = []# 遍历文件夹for root, dirs, files in os.walk(folder_path):for file in files:# 获取文件的完整路径file_path = os.path.join(root, file)# 将文件路径添加到列表中file_list.append(file_path)return file_list# 步骤 2:创建一个智能体。这里我们以 `Assistant` 智能体为例,它能够使用工具并读取文件。system_instruction = ''tools = []file_list = get_file_list('G:\\Video\\电影\\聚客第6期\\L1\\4_RAG高级技术\\第3章_RAG高级技术\\docs')print(file_list)# 步骤 1:配置您所使用的 LLMllm_cfg = {# 使用 DashScope 提供的模型服务:'model': 'qwen-max','model_server': 'dashscope','api_key': os.getenv('DASHSCOPE_API_KEY'),# 如果这里没有设置 'api_key',它将读取 `DASHSCOPE_API_KEY` 环境变量。# 使用与 OpenAI API 兼容的模型服务,例如 vLLM 或 Ollama:# 'model': 'Qwen2-7B-Chat',# 'model_server': 'http://localhost:8000/v1', # base_url,也称为 api_base# 'api_key': 'EMPTY',# (可选) LLM 的超参数:'generate_cfg': {'top_p': 0.8}}# 步骤 2:创建一个智能体。这里我们以 `Assistant` 智能体为例,它能够使用工具并读取文件。system_instruction = '你是一位保险专家,根据你的经验来精准的回答用户提出的问题'tools = []bot = Assistant(llm=llm_cfg,system_message=system_instruction,function_list=tools,files=file_list)# 步骤 3:作为聊天机器人运行智能体。messages = [] # 这里储存聊天历史。while True:query = input('用户请求: ')if query == '-1':break# 将用户请求添加到聊天历史。messages.append({'role': 'user', 'content': query})response = []current_index = 0for response in bot.run(messages=messages):# 流式输出current_response = response[0]['content'][current_index:]current_index = len(response[0]['content'])print(current_response, end='')# 将机器人的回应添加到聊天历史。messages.extend(response)
