LangChain_Agents学习文档

        在LangChain中的Agents篇,有一个Agents Type,叫做“zero-shot-react-description”,其描述为:使用ReAct框架,来抉择使用什么工具,这个抉择的标准是基于对于每一个tool的描述决定的。它不限制tool的数量,但要求所有提供的tool都必须配有一个描述。

        我们来看一下他的实现结果:        

         显然,效果不错,很好地实现了ReAct框架。除了“zero-shot-react-description”以外,官方提供的技术文档里,还包括了以下几种:

react-docstore

This agent uses the ReAct framework to interact with a docstore. Two tools must be provided: a Search tool and a Lookup tool (they must be named exactly as so). The Search tool should search for a document, while the Lookup tool should lookup a term in the most recently found document. This agent is equivalent to the original ReAct paper, specifically the Wikipedia example.

self-ask-with-search

This agent utilizes a single tool that should be named Intermediate Answer. This tool should be able to lookup factual answers to questions. This agent is equivalent to the original self ask with search paper, where a Google search API was provided as the tool.

conversational-react-description

This agent is designed to be used in conversational settings. The prompt is designed to make the agent helpful and conversational. It uses the ReAct framework to decide which tool to use, and uses memory to remember the previous conversation interactions.

        但其实如果我们去翻LangChain库的源代码,其实还有以下几种:

    CHAT_ZERO_SHOT_REACT_DESCRIPTION = "chat-zero-shot-react-description"
    CHAT_CONVERSATIONAL_REACT_DESCRIPTION = "chat-conversational-react-description"
    STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION = (
        "structured-chat-zero-shot-react-description"

        这里没有在技术文档体现的原因,我想可能是因为作者觉得这些只是上面种类的分支,没有再去做详细介绍的必要。

        那我们一步一步来,一个个深入探索。

Question 1:

        它是怎么实现Agent的自主判断选择能力的?即我们的第一个图中的“I need to find out ……”,因为要知道,ChatGPT的数据库是只到2021年9月,但他是知道2021年9月前的日本首相是谁,那怎么让他知道他现在知道的是不一定对的,然后让他主动去用SerpAPI去查找现在的结果呢?

        答案是Description和FewShot。

        Description就是去描述这个tool该如何使用,没错!就是自然语言的描述,第一时间其实蛮难接受的,但细想下来其实又很合理,因为LLM模型的参数都是几十亿到上千亿的,不可能去从中选择出哪些参数是和我想要的结果紧密相关的,那解决方案不如让模型去自己判断。而具体到Description的具体效果如何,这个取决于你的模型的应变能力,对于ChatGPT来说,其实相对来说就比较完善,给出相应的Description,只要描述的恰当,就能去调用工具,然后返回结果。

        但很多模型,尤其是本地端模型,由于参数较少(参数大的模型,大多数显卡跑不起来),其对工具的描述是不接受的,而且我们调用工具往往不是一次调用,是多次的,如何让模型知道要多次调用呢?也是给他一个模板,比如让它判断结果还需要查询,那么就让他以Action:……开头,如果结果是最终结果,那么以Final Answer:……开头,但显然,这也需要模型足够灵活,足够理解人类的意图,这是小参数模型的普遍缺点。

        那么,FewShot就很需要了,也就是给模型一些样例,让模型按照这个样例去做,让它觉得,它曾经这么回答过。一般来说,给三四个模板就可以了,这也是FewShot的好处。

        如果以上两个方法都不行,那很遗憾,只能从模型下手了,也就是微调。其实,微调和FewShot本质上没什么区别,只不过一个是跑代码前给了FewShot,一个是跑代码途中给了FewShot,以及整体的样本量也是一个区别,但本质的方法是一样的,都是给他样本去学习。这一般是最终方案,针对于那些模型较小,想要让模型做一些特化回答的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要安装tf_agents包,可以使用以下命令: ```bash pip install tf_agents ``` 接下来是一个使用tf_agents实现自定义环境的示例代码: ```python import numpy as np import tensorflow as tf from tf_agents.environments import py_environment from tf_agents.specs import array_spec from tf_agents.trajectories import time_step as ts class CustomEnv(py_environment.PyEnvironment): def __init__(self): # 定义环境状态的维度和取值范围 self._observation_spec = array_spec.BoundedArraySpec(shape=(2,), dtype=np.float32, minimum=-1, maximum=1, name='observation') # 定义动作的维度和取值范围 self._action_spec = array_spec.BoundedArraySpec(shape=(), dtype=np.int32, minimum=0, maximum=1, name='action') # 初始化状态 self._state = np.zeros(shape=(2,), dtype=np.float32) def action_spec(self): return self._action_spec def observation_spec(self): return self._observation_spec def _reset(self): # 重置状态 self._state = np.zeros(shape=(2,), dtype=np.float32) # 返回初始状态的时间步信息 return ts.restart(self._state) def _step(self, action): # 计算新的状态 if action == 0: self._state[0] += 0.1 else: self._state[0] -= 0.1 self._state[1] += np.random.normal(loc=0, scale=0.05) # 判断是否达到终止状态 if self._state[0] >= 1: return ts.termination(self._state, reward=1) elif self._state[0] <= -1: return ts.termination(self._state, reward=-1) # 返回时间步信息 else: return ts.transition(self._state, reward=0.1, discount=0.9) ``` 这个示例代码实现了一个简单的自定义环境,环境状态为一个二维向量,动作为一个0/1值,代表向左或向右移动。状态的第一个维度表示位置,第二个维度表示随机噪声。当位置超出[-1, 1]的范围时,环境进入终止状态,返回奖励+1或-1。否则,返回奖励0.1,折扣因子为0.9的时间步信息。 要训练强化学习模型,需要使用tf_agents提供的Agent和ReplayBuffer等类。具体实现方式可以参考tf_agents的官方文档

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值