当前位置: 首页 > news >正文

llamaindex实战-ChatEngine-ReAct Agent模式

概述

ReAct 是一种基于Agent的聊天模式,构建在数据查询引擎之上。对于每次聊天交互,代理都会进入一个 ReAct 循环:

  • 首先决定是否使用查询引擎工具并提出适当的输入

  • (可选)使用查询引擎工具并观察其输出

  • 决定是否重复或给出最终答复

这种方法很灵活,因为它可以灵活地选择是否查询知识库,它是基于Agent来实现的。然而,表现也更依赖于LLM的质量。您可能需要进行更多强制,以确保它选择在正确的时间查询知识库,而不是产生幻觉答案。

实现逻辑

  1. 构建和使用本地大模型。这里使用的是gemma2这个模型,也可以配置其他的大模型。

  2. 从文档中构建索引

  3. 把索引转换成查询引擎:index.as_chat_engine,并设置chat_mode为react。

注意:我这里使用的是本地大模型gemm2,效果可能没有openai的好。

实现代码

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.ollama import Ollamalocal_model = "/opt/models/BAAI/bge-base-en-v1.5"# bge-base embedding model
Settings.embed_model = HuggingFaceEmbedding(model_name=local_model)
# ollama
Settings.llm = Ollama(model="gemma2", request_timeout=360.0)from llama_index.core import VectorStoreIndex, SimpleDirectoryReaderdata = SimpleDirectoryReader(input_dir="./data/paul_graham/").load_data()
index = VectorStoreIndex.from_documents(data)# 设置使用react模式
chat_engine = index.as_chat_engine(chat_mode="react", llm=Settings.llm, verbose=True)response = chat_engine.chat( "Use the tool to answer what did Paul Graham do in the summer of 1995?")

输出

从以下输出可以看到,不同大模型的输出不太相同。Agent通过查询引擎获取到了对应的索引和文本信息。

$ python chat_react.py 
> Running step 3e748b23-a1bb-4807-89f6-7bda3b418b86. Step input: Use the tool to answer what did Paul Graham do in the summer of 1995?
Thought: The current language of the user is: English. I need to use a tool to help me answer the question.
Action: query_engine_tool
Action Input: {'input': 'What did Paul Graham do in the summer of 1995?'}
Observation: He worked on his Lisp-based web server.  
​
> Running step 5f4592b6-f1d0-4fcf-8b03-a50d46641ef2. Step input: None
Thought: I can answer without using any more tools. I'll use the user's language to answer
Answer: In the summer of 1995, Paul Graham worked on his Lisp-based web server.

实现分析

从以下实现代码中可以看到,当聊天模式是REACT模式时,会创建一个AgentRunner,并把查询引擎作为工具放入Agent工具列表中。

  def as_chat_engine(self,chat_mode: ChatMode = ChatMode.BEST,llm: Optional[LLMType] = None,**kwargs: Any,) -> BaseChatEngine:    if chat_mode in [ChatMode.REACT, ChatMode.OPENAI, ChatMode.BEST]:# use an agent with query engine tool in these chat modes# NOTE: lazy importfrom llama_index.core.agent import AgentRunnerfrom llama_index.core.tools.query_engine import QueryEngineTool
​# convert query engine to toolquery_engine_tool = QueryEngineTool.from_defaults(query_engine=query_engine)
​return AgentRunner.from_llm(tools=[query_engine_tool],llm=llm,**kwargs,)

小结

通过REACT模式,会创建一个Agent,并把查询引擎作为工具放到该Agent中。然后,通过查询引擎的能力来查询想要的内容。

http://www.lryc.cn/news/495148.html

相关文章:

  • redis快速进门
  • 从0开始linux(39)——线程(2)线程控制
  • International Journal of Medical Informatics投稿经历时间节点
  • BUUCTF—Reverse—Java逆向解密(10)
  • CLIP-MMA: Multi-Modal Adapter for Vision-Language Models
  • 三维扫描仪-3d扫描建模设备自动检测尺寸
  • vue3+ant design vue实现日期选择器默认显示当前年,并限制用户只能选择当前年及之前~
  • 【electron-vite】搭建electron+vue3框架基础
  • 05《存储器层次结构与接口》计算机组成与体系结构 系列课
  • elasticsearch报错fully-formed single-node cluster with cluster UUID
  • Milvus×Florence:一文读懂如何构建多任务视觉模型
  • DAPP
  • 生产环境中,nginx 最多可以代理多少台服务器,这个应该考虑哪些参数 ?怎么计算呢
  • 【深度学习|目标跟踪】StrongSORT 详解(以及StrongSORT++)
  • 23种设计模式-原型(Prototype)设计模式
  • Qt—QLineEdit 使用总结
  • go-zero使用自定义模板实现统一格式的 body 响应
  • BUGKU printf
  • 深度学习:梯度下降法
  • `console.log`调试完全指南
  • ROS VSCode调试方法
  • 16 —— Webpack多页面打包
  • 微服务即时通讯系统的实现(服务端)----(3)
  • .net6.0 mvc 传递 model 实体参数(无法对 null 引用执行运行时绑定)
  • VUE 入门级教程:开启 Vue.js 编程之旅
  • Ubantu系统docker运行成功拉取失败【成功解决】
  • mvn-mac操作小记
  • 机器学习——生成对抗网络(GANs):原理、进展与应用前景分析
  • 「Mac畅玩鸿蒙与硬件33」UI互动应用篇10 - 数字猜谜游戏
  • Ps:存储 Adobe PDF