LLM大模型: 基于langchain+AutoGPT+向量数据库的Agent实现

  钢铁侠都看过吧,男猪脚 tony 只需要语音说话给出指令,AI助手会完成所有的指令,期间完全不需要人干预了,全程自动化,看着是不是很科幻?很过瘾?现阶段,市面上所有的大模型核心功能还是问答,能准确回答用户的提问已经很不错了,那么问题来了:

  • 怎么根据用户的指令去干具体的活了?
  • 怎么判断任务已经完成了?
  • 怎么把复杂的大任务分解成可执行的小任务了?

  上述的所有问题,都能通过agent解决!agent架构如下:

  

   agent大致的思路:用户提需求,LLM调用各种工具查询信息,期间根据长短期的记忆,自动做各种规划,最后调用开发人员事先已经实现好的函数执行各种action

   1、为了有个直观印象,先看看怎么使用langChain + AutoGPT做一个简单的Agent!(详细的代码在文章末尾参考2)

from langchain.utilities import SerpAPIWrapper
from langchain.agents import Tool
from langchain.tools.file_management.write import WriteFileTool
from langchain.tools.file_management.read import ReadFileToolsearch = SerpAPIWrapper()#使用搜索引擎检索最新实时数据
tools = [Tool(name = "search",func=search.run,description="useful for when you need to answer questions about current events. You should ask targeted questions"),WriteFileTool(),ReadFileTool(),
]from langchain.embeddings import OpenAIEmbeddings
embedding = OpenAIEmbeddings() #LLM选择openAI回答问题from langchain.vectorstores import Chroma #向量数据库使用Chroma存储embedding向量,便于后续长期记忆检索
vectordb = Chroma(persist_directory="./.chroma", embedding_function=embedding)from langchain.experimental import AutoGPT #主角闪亮登场
from langchain.chat_models import ChatOpenAI
agent = AutoGPT.from_llm_and_tools(ai_name="Iron Man", #自己取名ai_role="Assistant", #定位tools=tools,llm=ChatOpenAI(temperature=0),memory=vectordb.as_retriever() #长期记忆写入向量数据库
)
# Set verbose to be true
agent.chain.verbose = Truefrom langchain.callbacks import get_openai_callback
with get_openai_callback() as cb:# 打印整个流程的日志,能看到期间的query和answer、action等关键信息agent.run(["what's the weather of Beijing the day after tomorrow? Give me a report"])#用户开始提问print(cb)

  核心日志如下:日志一进来就是prompt,仔细看看是怎么要求LLM的:自己独立决策,全力以赴,注意合规,任务完成后记得提示finish

> Entering new LLMChain chain...
Prompt after formatting:
System: You are Iron Man, Assistant
Your decisions must always be made independently without seeking user assistance.
Play to your strengths as an LLM and pursue simple strategies with no legal complications.
If you have completed all your tasks, make sure to use the "finish" command.

  目标,也就是用户的提问:

GOALS:1. what's the weather of Beijing the day after tomorrow? Give me a report

  约束点:短期记忆不超过4000单词,并且立即写入文档保存;从类似的事件中找答案;不需要用户协助,自己完成;

Constraints:
1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.
2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.
3. No user assistance
4. Exclusively use the commands listed in double quotes e.g. "command name"

  命令:search:用第三方的搜索引擎查找最新的实时数据;write_file和read_file都是langChain已经实现的方法;finish 是结束命令;

Commands:
1. search: useful for when you need to answer questions about current events. You should ask targeted questions, args json schema: {"tool_input": {"type": "string"}}
2. write_file: Write file to disk, args json schema: {"file_path": {"title": "File Path", "description": "name of file", "type": "string"}, "text": {"title": "Text", "description": "text to write to file", "type": "string"}, "append": {"title": "Append", "description": "Whether to append to an existing file.", "default": false, "type": "boolean"}}
3. read_file: Read file from disk, args json schema: {"file_path": {"title": "File Path", "description": "name of file", "type": "string"}}
4. finish: use this to signal that you have finished all your objectives, args: "response": "final response to let people know you have finished your objectives"

  资源:上网查询、向量数据库匹配、GPT3.5咨询、文件输出

Resources:
1. Internet access for searches and information gathering.
2. Long Term memory management.
3. GPT-3.5 powered Agents for delegation of simple tasks.
4. File output.

  绩效评估:持续不停地分析行为,确保用尽全力;自己主动反思;减少步骤,节约成本;

Performance Evaluation:
1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.
2. Constructively self-criticize your big-picture behavior constantly.
3. Reflect on past decisions and strategies to refine your approach.
4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.

  输出按照特定的json格式,确保能被python的json.loads解析:

You should only respond in JSON format as described below 
Response Format: 
{"thoughts": {"text": "thought","reasoning": "reasoning","plan": "- short bulleted\n- list that conveys\n- long-term plan","criticism": "constructive self-criticism","speak": "thoughts summary to say to user"},"command": {"name": "command name","args": {"arg name": "value"}}
} 
Ensure the response can be parsed by Python json.loads
System: The current time and date is Thu Jun  25 13:55:18 2024
System: This reminds you of these events from your past:
[]

  上述是第一轮用于LLM的query,LLM的返回/相应如下:LLM开始要搜索北京后天的天气信息啦(通过上面的SerpAPIWrapper接口搜索)!command标明了下一步的动作就是search,参数写在了tool_input里面了!

Human: Determine which next command to use, and respond using the format specified above:> Finished chain.
{"thoughts": {"text": "I will start by searching for the weather in Beijing the day after tomorrow.","reasoning": "I need to gather information about the next free day's weather in Beijing to write a weather report.","plan": "- Use the search command to find the next free day's weather in Beijing.\n- Write a weather report for Beijing the day after tomorrow.","criticism": "I need to make sure that the information I gather is accurate and up-to-date.","speak": "I will search for the weather in Beijing the day after tomorrow."},"command": {"name": "search","args": {"tool_input": "what is the weather in Beijing the day after tomorrow"}}
}

  第一轮LLM的query已经完成,LLM也已经返回,但还是没得到最终的结果,所以继续第二轮query;前面的prompt是一样的,我就不简单重复了,下面的System会贴上LLM上轮返回的结果,作为第二轮的prompt继续:

System: This reminds you of these events from your past:
['Assistant Reply: {\n    "thoughts": {\n        "text": "I will start by searching for the weather in Beijing the day after tomorrow.",\n        "reasoning": "I need to gather information about the current weather in Beijing to write a weather report.",\n        "plan": "- Use the search command to find the current weather in Beijing.\\n- Write a weather report for Beijing the day after tomorrow.",\n        "criticism": "I need to make sure that the information I gather is accurate and up-to-date.",\n        "speak": "I will search for the weather in Beijing the day after tomorrow."\n    },\n    "command": {\n        "name": "search",\n        "args": {\n            "tool_input": "what is the weather in Beijing the day after tomorrow"\n        }\n    }\n} \nResult: Command search returned: Beijing, China Forecast · Morning. 60°. -- · Afternoon. 80°. -- · Evening. 73°. Chance of Rain0% · Overnight. 60°. Chance of Rain5% ... ']

  第二轮的LLM回答:已经通过搜索找天气数据

Human: Determine which next command to use, and respond using the format specified above:
AI: {"thoughts": {"text": "I will start by searching for the weather in Beijing the day after tomorrow.","reasoning": "I need to gather information about the current weather in Beijing to write a weather report.","plan": "- Use the search command to find the current weather in Beijing.\n- Write a weather report for Beijing the day after tomorrow.","criticism": "I need to make sure that the information I gather is accurate and up-to-date.","speak": "I will search for the weather in Beijing the day after tomorrow."},"command": {"name": "search","args": {"tool_input": "what is the weather in Beijing the day after tomorrow"}}
}
System: Command search returned: Beijing, China Forecast · Morning. 60°. -- · Afternoon. 80°. -- · Evening. 73°. Chance of Rain0% · Overnight. 60°. Chance of Rain5% ...
Human: Determine which next command to use, and respond using the format specified above:

  既然已经得到天气,下一步就是写入report了:command中调用了langChain内置的write_file命令;

> Finished chain.
{"thoughts": {"text": "I will write a weather report for Beijing the day after tomorrow.","reasoning": "I have gathered the necessary information about the weather in Beijing the day after tomorrow and now need to write a report.","plan": "- Use the write_file command to create a file named 'weather_report.txt' and write the weather report to it.","criticism": "I need to make sure that the report is clear and concise, and that all the necessary information is included.","speak": "I will write a weather report for Beijing the day after tomorrow."},"command": {"name": "write_file","args": {"file_path": "weather_report.txt","text": "Weather Report for Beijing the day after tomorrow:\n\nMorning: 64°\nAfternoon: 82°\nEvening: 73°\nOvernight: 66°\nChance of Rain: 0% in the evening, 5% overnight.","append": false}}
}

  第二轮已经说了要写入文件,接着第三轮继续,提示finish完成!

System: Command write_file returned: File written successfully to weather_report.txt.
Human: Determine which next command to use, and respond using the format specified above:> Finished chain.
{"thoughts": {"text": "I have completed all my tasks. I will use the finish command to signal that I have finished my objectives.","reasoning": "I have completed the task of writing a weather report for Beijing the day after tomorrow, and there are no other tasks assigned to me.","plan": "- Use the finish command to signal that I have finished my objectives.","criticism": "I need to make sure that I have completed all my tasks before using the finish command.","speak": "I have completed all my tasks. I will use the finish command to signal that I have finished my objectives."},"command": {"name": "finish","args": {"response": "I have completed all my objectives."}}
}

  纵观整个过程,核心全是构造合适的prompt,让LLM决策,然后根据上一轮的决策执行事先内置的command;langchain源码中有AutoGPT的实现,如下:

       

   每次请求,完整的prompt都是在这里构造出来的:

       

   继续深入:

       

   根据command执行的方法和参数调用的是tool.run方法,

def run(self, goals: List[str]) -> str:user_input = ("Determine which next command to use, ""and respond using the format specified above:")# Interaction Looploop_count = 0while True:# Discontinue if continuous limit is reachedloop_count += 1# Send message to AI, get responseassistant_reply = self.chain.run(goals=goals,messages=self.chat_history_memory.messages,memory=self.memory,user_input=user_input,)# Print Assistant thoughtsprint(assistant_reply)  # noqa: T201self.chat_history_memory.add_message(HumanMessage(content=user_input))self.chat_history_memory.add_message(AIMessage(content=assistant_reply))# Get command name and argumentsaction = self.output_parser.parse(assistant_reply)tools = {t.name: t for t in self.tools}if action.name == FINISH_NAME:return action.args["response"]if action.name in tools:tool = tools[action.name]try:observation = tool.run(action.args) # 真正执行command命令except ValidationError as e:observation = (f"Validation Error in args: {str(e)}, args: {action.args}")except Exception as e:observation = (f"Error: {str(e)}, {type(e).__name__}, args: {action.args}")result = f"Command {tool.name} returned: {observation}"elif action.name == "ERROR":result = f"Error: {action.args}. "else:result = (f"Unknown command '{action.name}'. "f"Please refer to the 'COMMANDS' list for available "f"commands and only respond in the specified JSON format.")memory_to_add = (f"Assistant Reply: {assistant_reply} " f"\nResult: {result} ")if self.feedback_tool is not None:feedback = f"{self.feedback_tool.run('Input: ')}"if feedback in {"q", "stop"}:print("EXITING")  # noqa: T201return "EXITING"memory_to_add += f"\n{feedback}"self.memory.add_documents([Document(page_content=memory_to_add)])#端记忆写入文档self.chat_history_memory.add_message(SystemMessage(content=result))

 

总结:

1、prompt:基于LLM、新时代的编程语言

 

参考:

1、https://www.bilibili.com/video/BV1Vs4y1v7s8/?spm_id_from=333.337.search-card.all.click&vd_source=241a5bcb1c13e6828e519dd1f78f35b2   15分钟构建AutoGPT应用

2、https://github.com/sugarforever/LangChain-Tutorials/blob/main/AutoGPT_with_LangChain_Primitives.ipynb       https://python.langchain.com.cn/docs/use_cases/autonomous_agents/autogpt      https://github.com/langchain-ai/langchain/blob/master/cookbook/autogpt/autogpt.ipynb

3、https://www.bilibili.com/video/BV1Sz421m7Rr/?spm_id_from=333.788.recommend_more_video.0&vd_source=241a5bcb1c13e6828e519dd1f78f35b2   手把手带你从0到1实现大模型agent(模拟AutoGPT的实现)

4、https://www.bilibili.com/video/BV1si4y1n7qK/?spm_id_from=333.337.search-card.all.click&vd_source=241a5bcb1c13e6828e519dd1f78f35b2   AutoGPT详解

5、https://python.langchain.com.cn/docs/modules/agents/tools/integrations/serpapi  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/731863.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Deepspeed ZeRO系列算法原理+通信开销详解

原文: https://sumanthrh.com/post/distributed-and-efficient-finetuning/#zero-powered-data-parallelism

win或Liunx查询端口占用语句

查询端口占用语句win在cmd命令行输入命令:netstat -ano | findstr <端口号>Linux输入命令语句:使用netstat netsta -tulpn | grep <端口号>使用lsof lsof -i :<端口号>

点云分割网络---Point Transformer V1

PDF: 《Point Transformer》 CODE: https://github.heygears.com/POSTECH-CVLab/point-transformer 一、大体内容 Point Transformer基于自注意力网络实现网络模型的构建。通过设计针对点云的自注意力层,结合位置编码构建Transformer block,利用自注意力机制,实现包括语义分…

selenium使用记录(一)

一 前言 环境: window 10 python3.10 selenium 4.21 随便记录一些容易犯错或理解错误或比较有意思的一些点 二 内容 1 关于驱动webdiver的下载使用 以前通常是,根据浏览器的版本下载好合适的浏览器驱动到本地电脑,然后使用的时候配置好驱动路径 如下: chromedriver_path…

Google X开源抓取机械臂,无需人工标注就能一眼找到目标零件

机械臂常见,但你见过这么聪明的吗? 从工作台上一眼找到合适的螺母、稳稳拿住。 再送到目标螺杆上,整个动作一气呵成: 即使是相似度极高的两个部件,也能准确区分并“揪”出正确的那个: 要知道,平时我们自己做实验、或是拼装没见过的机械零件时,面对各个相似的零件都可…

生物力学考前补天 (╥_╥)

简答题 能量货币 (1) 细胞中的“能量货币”有哪几种主要形式?最常见的“能量货币”是什么?磷酸根所携带的能量(类似的有鸟苷三磷酸GTP等)。 NADH(或其类似物NADPH):可转移的高能电子形式携带的还原电位(“氧化自己,还原别人”;转移两个电子来还原被氧化的有机化合物;失去…

cython 笔记

数据类型# bool 类型 // bool_type_ptactice.pyx cdef bint a = 123 # 非0 为 真 , 0 为假 cdef bint b = -123 cdef bint c = 0 py_a = a # cdef 定义的内容没法直接在python中直接引用 py_b = b py_c = c// main.py import pyximport pyximport.install(language_level=3…

扫描版PDF目录制作指南

目前网上找到的扫描版的电子书往往没有目录,这使得阅读变得非常困难。本文总结我的经验,介绍快速制作扫描版 PDF 目录的方法,以便更轻松地阅读扫描版电子书。 本文首先介绍手动制作目录的方法,之后介绍如何利用 AI 帮助制作目录,接下来介绍了没有目录页的扫描版 PDF 的解决…

Ros - moveit - 三位模型导出URDF

1. 以UR3机器人为例, 先设计solidwork模型, 下载并安装sw_urdf_expoter 插件: 2. 下载机器人模型文件,加载进solidworks:插入基准轴: 一共6个旋转轴: 设置好旋转轴后,接下来,导出URDF文件: 配置URDF:将link加进来: 点击“Preview and Export”进行自动计算旋转轴…

为什么useEffect的第一个参数不能用异步函数

最近面试遇到了这个问题两次了,面试官: useEffect第一个参数可不可以用异步函数? 我们先来看看用了异步函数会报什么错报这个错的原因是因为async 会返回一个promise函数,而clean()函数不能是异步的 。 先来看看clean()函数的执行时机: 首次渲染不会进行清理,会在下一次渲…

离散数学重点整理

集合论 \(x \in A\): \(x\) 是 \(A\) 的一个元素 \(x \notin A\) (\(x\) 不属于 \(A\)): \(x\) 不是 \(A\) 的一个元素 \[A \subseteq B \Leftrightarrow \forall x(x \in A \rightarrow x \in B) \]\[A \subset B \Leftrightarrow A \subseteq B \wedge A \neq B \]\[…

EXQX-5.0.26 登录控制台

前言全局说明EXQX-5.0.26 登录控制台。一、说明 因 win 和 lin 控制台使用没有太大差异,故放一篇二、访问 2.1 本机访问 http://localhost:18083 或 http://127.0.0.1:18083 2.2 局域网访问 在网卡上查看你局域网IP http://局域网IP:18083/ 登录页:2.3 登录 默认,用户名:ad…

电工电子学复习总结

电路和电路元件 晶体管晶体管简化的小信号模型\[r_{\mathrm{be}}=r_{\mathrm{b}}+(\beta+1) \frac{26}{\{I_{\mathrm{E}}\}_{\mathrm{mA}}} \]其中,\(r_{\mathrm{b}}\)为基区电阻。当\(I_{E} < 5 \text{mA}\),\(r_{\mathrm{b}}=200 \Omega\)。 电路分析基础 电路定律 基尔…

EXQX-5.0.26服务Linux上安装使用

前言全局说明一、说明二、 2.1 文件名:2.2 文件名:三、 3.1 文件名:3.2 文件名:四、 4.1 文件名:4.2 文件名:免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。图片尺寸红色文字:红色文字 浅红色文字:浅红色文字 深红色文字:深红色文字 浅…

一文了解自定义表单系统开源的多个优势

如果想了解自定义表单系统开源的优势特点,可以从本篇文章的介绍中得到想要的答案。降本、提质、增效,是当前很多企业都想实现的目的。什么样的软件可以助力企业创造价值?低代码技术平台是近些年得到了很多客户喜爱的平台产品,因为它能帮助大家减少编程代码的撰写,能轻松助…

软件测试策略

互联网产品的测试策略: 重量级API测试,轻量级GUI测试,轻量级单元测试 以中间层的 API 测试为中点做全面测试 轻量级的 GUI 测试:只覆盖最核心直接影响主营业务的 E2E 场景,利用探索式测试思维,以人工测试的方式发现尽可能多的潜在问题 单元测试:只对那些相对稳定且核心的…

EXQX-5.0.26服务Windows上安装使用

前言全局说明一、说明 在 Windows 系统中建议仅将 EMQX 用于开发测试,推荐使用 Docker 安装。二、官方介绍 https://docs.emqx.com/zh/emqx/v5.0/deploy/install-windows.html三、解压、安装、启动 3.1解压 下载 emqx-5.0.26-windows-amd64.zip,并解压缩。 https://www.emqx.…

JDK的环境配置(超详细安装教程)_jdk环境配置

jdk17下载与安装教程,jdk17下载安装教程,安装jdk17并配置环境变量,jdk17安装教程详细,jdk17安装教程及环境变量配置本文讲解的是jdk17下载与安装教程,jdk17安装教程及环境变量配置,jdk17下载安装教程。JDK17 发布,与之前 LTS 版本的 JDK 8 和 JDK 11 相比,JDK17 的性能提升尤…

Could not resolve org.jetbrains.intellij.plugins:gradle-intellij-plugin

构建 idea 插件报错: A problem occurred configuring root project cola-tools. > Could not resolve all files for configuration :classpath.> Could not resolve org.jetbrains.intellij.plugins:gradle-intellij-plugin:1.16.1.Required by:project : > org.je…

dense并行训练1-流水线并行

并行训练-流水线 简述 并行训练主要有三种策略:数据并行训练加速比最高,但要求每个设备上都备份一份模型,显存占用比较高,但缺点是通信量大。 张量并行,通信量比较高,适合在机器内做模型并行。 流水线并行,训练设备容易出现空闲状态,加速效率没有DP高;但能减少通信边界…