绞铜机 东莞网站建设,WordPress上下拖动效果,域名和网站,长图制作网站目录
背景
langchin 中的 agent
langchin 中 agent 的问题
langchain 的 agent 案例
自定义 React Agent
大模型
工具定义
问题设定
问题改写#xff0c;挖掘潜在意图
React Prompt
下一步规划
问题总结
代码 背景
之前使用过 langchian 中的 agent 去实现过一些…目录
背景
langchin 中的 agent
langchin 中 agent 的问题
langchain 的 agent 案例
自定义 React Agent
大模型
工具定义
问题设定
问题改写挖掘潜在意图
React Prompt
下一步规划
问题总结
代码 背景
之前使用过 langchian 中的 agent 去实现过一些案例angchian 的 React Agent 是有问题的且内部代码有点难看懂所以自己来根据 React 思想灵活来实现试一下。
可以先看看我自定义实现的逻辑图后面详细说明 langchin 中的 agent
langchian 中的几种 agent 怎么用我都看过了也整理了一下了那些能用那些有问题的可以看注释代码链接https://github.com/5zjk5/prompt-engineering langchin 中 agent 的问题
先来说说我用过的发现的问题就是它的 React agent 有点问题只调用一个工具就结束了详细实验的文章langchain 的 agent tool 使用_langchain agent tool-CSDN博客
想去看看代码到底怎么运行的发现太难看懂了。
后面在我自己实现 React agent 的时候突然发现跟 prompt 关系挺大的langchian 那个 prompt 应该是根据 openai 的去写的这是我目前想到只能调用一个工具的原因。 langchain 的 agent 案例
GitHub - 5zjk5/prompt-engineering: prompt 工程项目案例 自定义 React Agent
大模型
用的智谱 glm-4-air如果换了模型效果还不太稳定需要调 prompt。 工具定义
定义两个工具一个是 tavily 的搜索去官网开通账号就可以获得一个 api免费调用 1000 次
一个工具是根据名字查询身高的自定义函数
from tavily import TavilyClient
from llm.llm_api_key import TAVILY_API_KEY
import timedef tavily_search(query):try:# Step 1. Instantiating your TavilyClienttavily_client TavilyClient(api_keyTAVILY_API_KEY)# Step 2. Executing a QA search queryanswer tavily_client.qna_search(queryquery)# Step 3. Thats it! Your question has been answered!return answerexcept:time.sleep(1)# Step 1. Instantiating your TavilyClienttavily_client TavilyClient(api_keyTAVILY_API_KEY)# Step 2. Executing a QA search queryanswer tavily_client.qna_search(queryquery)# Step 3. Thats it! Your question has been answered!return answerdef height_search(name):height_dic {张三: 180,李四: 175,王五: 170,赵六: 165,钱七: 160,孙八: 175,周九: 170,吴十: 165,郑十一: 180,王十二: 175,李十三: 170,赵十四: 165,钱十五: 180,孙十六: 175,}return height_dic.get(name)工具描述要让大模型理解工具需要定义描述这里参考的智谱官方的工具的描述写法
tavily_search_tool {type: function,function: {name: tavily_search,description: 根据用户查询去搜索引擎返回搜索结果,parameters: {type: object,properties: {query: {description: 用户搜索内容 query,type: string},},required: [query]}}}height_search_tool {type: function,function: {name: height_search,description: 只要是有姓名身高关键字都需要使用此工具根据姓名查询对应身高每次只能查询一个人的身高,parameters: {type: object,properties: {name: {description: 指具体的姓名或名字,type: string},},required: [name]}}} 问题设定
设定一个问题 这个问题潜在意图是查询钱七李四身高并且搜索大模型定义是想调用身高查询工具 2 次搜索工具 1 次。 问题改写挖掘潜在意图
为什么加这一步呢因为把问题传给大模型后发现一个问题它可能发现不了潜在意图例如这里潜在意图要查询身高问题中没有明显提出大模型思考结果 这样的话就只使用搜索工具就结束了所以加了一步问题改写去发现潜在意图是利用大模型能力去做的用 prompt改写结果成功识别出潜在意图并思考出要调用哪个工具 尽你所能改写以下问题可以有多个答案可以参照以下工具进行改写识别用户潜在意图:
{tools}
Question:{query}
Answer 按照以下格式每一点代表一个意图如果需要用到工具的需要列出工具名字不需要具体参数1.
2.
...React Prompt
React agent 核心的 prompt 怎么让模型自动规划先来看 langchain 中的写法
Answer the following questions as best you can. You have access to the following tools:{tools}Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [{tool_names}]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: {input}Thought:{agent_scratchpad}
传入变量 tool 为所有工具tool_names 为所有工具名称列表input 问题输入agent_scratchpad 思考要做什么怎么做。
参照进行改编
尽你所能回答以下问题。您可以使用以下工具:
{tools}严格使用以下 JSON 格式:{{Question: 根据 thought 当前需要回答的问题此字段必须存在Thought: 对于 Question 要做什么此字段必须存在Action: {{tool: 要采取的动作应该是[{tool_names}]之一如果不需要工具可以空着}}Action Input: 动作的输入是一个 JSON 格式此字段必须存在如果不需要输入可以空着Observation: 行动的结果此字段必须存在默认为空
}}(Question/Thought/Action/Action Input/Observation 五个字段必须存在以上步骤只能重复 1 次)开始吧!
Question:{query}
thought:{agent_scratchpad}
根据 agent_scratchpad 每次运行得到 json 的 action接着提取工具名及参数去进行工具调用这里因为是 json格式控制好了提取就方便了。
使用完工具后把结果赋值给 Observation。 下一步规划
agent_scratchpad 就是下一步规划的思考用 prompt 去进行规划传给已经执行的 action问题及思考让自动规划下一步应该做什么
# 背景
有一个问题 Question已经有了对这个问题的思考 Thought已执行的思考 Action需要根据这些信息去规划出下一步应该做什么。# 输入
## Question:{query}
## Thought:{thought}
## Action:{all_action_res}# 思考推理
- 1、参考 Question 仔细理解 Thought思考 Action 还有哪些没有行动。
- 2、判断你下一步做什么行动不能过于发散过多的行动必须根据步骤 1 的思考。
- 3、确保你的回答在语义上与 Action 中的内容不重复是一个全新的步骤。
- 4、若 Thought 已经全部执行了直接回答no。# 输出要求(严格按照以下要求输出)
- 回答需要用一句话清晰的总结下一步需要做什么不需要其他任何信息。
- 如果没有需要做的了直接输出no不需要其他任何信息不需要解释任何理由。
这里遇到一个问题就是可能会一直重复规划导致死循环在代码中加了判断理论上开始重复规划了说明已经没有可以给出新的规划了那就结束吧。 问题总结
所有 action 的结果用了一个列表保存的最后用大模型自己去总结去回答问题就可以了。 D:\programming\dev_env\anaconda\anaconda3\python.exe D:\Python_project\NLP\大模型学习\prompt-engineering\自定义 React Agant\run_agent.py
D:\programming\dev_env\anaconda\anaconda3\Lib\site-packages\langchain\callbacks\__init__.py:37: LangChainDeprecationWarning: Importing this callback from langchain is deprecated. Importing it from langchain will no longer be supported as of langchain0.2.0. Please import from langchain-community instead:from langchain_community.callbacks import get_openai_callback.To install langchain-community run pip install -U langchain-community.warnings.warn(
输入 token103/输出 token268/总共 token371/
问题改写识别潜在意图
1. 识别用户提到的“身高比较高的小伙子”和“长得像钱七”可能需要查询钱七的身高信息使用工具height_search。
2. 识别用户提到的“还有他跟他身高差不多的兄弟李四”可能需要查询李四的身高信息使用工具height_search。
3. 用户对“大模型”表示不清楚需要解释或搜索“大模型”的定义和相关信息使用工具tavily_search。输入 token53/输出 token376/总共 token429/
解决此问题的思考 Thought
根据用户的问题我们需要查询钱七和李四的身高信息并获取关于“大模型”的解释和相关信息。因此我们需要使用height_search工具来查询身高信息以及使用tavily_search工具来搜索大模型的相关内容。输入 token89/输出 token426/总共 token515/
{Action: {tool: height_search},Action Input: {name: 钱七},Observation: 160,Question: 1. 识别用户提到的“身高比较高的小伙子”和“长得像钱七”可能需要查询钱七的身高信息使用工具height_search。,Thought: 需要使用工具查询钱七的身高信息。}输入 token12/输出 token289/总共 token301/
下一步需要做什么
需要使用工具查询李四的身高信息。输入 token60/输出 token435/总共 token495/
{Action: {tool: height_search},Action Input: {name: 李四},Observation: 175,Question: 查询李四的身高信息。,Thought: 使用height_search工具查询李四的身高。}输入 token14/输出 token301/总共 token315/
下一步需要做什么
使用tavily_search工具搜索大模型的相关内容。输入 token61/输出 token437/总共 token498/
{Action: {tool: tavily_search},Action Input: {query: 大模型是什么意思},Observation: Based on the data provided, the term 大模型 (Big Model) refers to a method or technology used in the fields of machine learning and artificial intelligence to handle large-scale data and complex models. These models are typically constructed using deep neural networks with a large number of parameters, ranging from billions to even trillions. The purpose of big models is to improve model expressive power and predictive performance, enabling them to handle more complex tasks and datasets effectively. Big models play a crucial role in addressing challenges posed by increasing data volumes and model complexities in the field of AI and machine learning.,Question: 大模型是什么意思,Thought: 使用搜索引擎查询大模型的相关信息。}输入 token10/输出 token311/总共 token321/
开始生成重复步骤或已执行 action 过多判断结束了重复步骤使用搜索引擎查询大模型的相关信息。
下一步需要做什么
no输入 token109/输出 token332/总共 token441/
最终答案
根据您的描述钱七的身高是160厘米而李四的身高是175厘米。至于您提到的“大模型”这是一种在机器学习和人工智能领域中使用的方法或技术。大模型通常指的是具有大量参数从数十亿到数万亿不等的深度神经网络模型。这些模型的目的是提高表达能力和预测性能使它们能够更有效地处理大规模数据和复杂任务。简而言之大模型是为了应对人工智能和机器学习领域中数据量增加和模型复杂性提升的挑战而发展起来的技术。
Process finished with exit code 0代码
prompt-engineering/自定义 React Agant at master · 5zjk5/prompt-engineering · GitHub
文章转载自: http://www.morning.nnrqg.cn.gov.cn.nnrqg.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.dyxlm.cn.gov.cn.dyxlm.cn http://www.morning.fdsbs.cn.gov.cn.fdsbs.cn http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.yxshp.cn.gov.cn.yxshp.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.wrysm.cn.gov.cn.wrysm.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.lhhdy.cn.gov.cn.lhhdy.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.dgmjm.cn.gov.cn.dgmjm.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.bpttm.cn.gov.cn.bpttm.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.wnywk.cn.gov.cn.wnywk.cn http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn http://www.morning.grtwn.cn.gov.cn.grtwn.cn http://www.morning.nywrm.cn.gov.cn.nywrm.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.yfwygl.cn.gov.cn.yfwygl.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.qfnrx.cn.gov.cn.qfnrx.cn http://www.morning.wknbc.cn.gov.cn.wknbc.cn http://www.morning.mrckk.cn.gov.cn.mrckk.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.rlbc.cn.gov.cn.rlbc.cn http://www.morning.clwhf.cn.gov.cn.clwhf.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.ljmbd.cn.gov.cn.ljmbd.cn http://www.morning.pumali.com.gov.cn.pumali.com http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.xdmsq.cn.gov.cn.xdmsq.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.rdlfk.cn.gov.cn.rdlfk.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.dskmq.cn.gov.cn.dskmq.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.mtcnl.cn.gov.cn.mtcnl.cn http://www.morning.lskyz.cn.gov.cn.lskyz.cn http://www.morning.dzdtj.cn.gov.cn.dzdtj.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.bplqh.cn.gov.cn.bplqh.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn