大语言模型学习:10.智能体Agent

书生浦语大模型实战营学习笔记6

定义

即P(感知)—> P(规划)—>A(行动)。类似人类「做事情」的过程,Agent的核心功能,可以归纳为三个步骤的循环:感知(Perception)、规划(Planning)和行动(Action)。感知(Perception)是指Agent从环境中收集信息并从中提取相关知识的能力,规划(Planning)是指Agent为了某一目标而作出的决策过程,行动(Action)是指基于环境和规划做出的动作。其中,Policy是Agent做出Action的核心决策,而行动又通过观察(Observation)成为进一步Perception的前提和基础,形成自主地闭环学习过程。

组成

智能体范式

Agent的处理更强调workflow,更像一个flow-engineering

ReAct

ReAct: Synergizing Reasoning and Acting in Language Models

ReAct是这几种范式里面最基础的。核心原理是:自己选择需要使用的工具,并使用工具获取输出。

关于ReAct,这里有个Repo实现了一个简易的ReAct Agent,可以去看看具体实现。在这里简单的提一下:

  1. 首先定义工具类。这里以谷歌搜索为例:

    class Tools:
    def __init__(self) -> None:
    self.toolConfig = self._tools() def _tools(self):
    tools = [
    {
    'name_for_human': '谷歌搜索',
    'name_for_model': 'google_search',
    'parameters': [
    {
    'name': 'search_query',
    'description': '搜索关键词或短语',
    'required': True,
    'schema': {'type': 'string'},
    }
    ],
    }
    ]
    return tools def google_search(self, search_query: str):
    ...
  2. 构建系统提示:直接在prompt里告诉模型可以调用的工具(build_system_input),模型就会自己输出自己要调用的工具,之后Agent解析模型自己的输出(parse_latest_plugin_call)并调用工具(call_plugin)。

    TOOL_DESC = """{name_for_model}: Call this tool to interact with the {name_for_human} API. What is the {name_for_human} API useful for? {description_for_model} Parameters: {parameters} Format the arguments as a JSON object."""
    REACT_PROMPT = """Answer the following questions as best you can. You have access to the following tools: {tool_descs} Use the following format: Question: the input question you must answer
    Thought: you should always think about what to do
    Action: the action to take, should be one of [{tool_names}]
    Action Input: the input to the action
    Observation: the result of the action
    ... (this Thought/Action/Action Input/Observation can be repeated zero or more times)
    Thought: I now know the final answer
    Final Answer: the final answer to the original input question Begin!
    """ class Agent:
    def __init__(self, path: str = '') -> None:
    self.path = path
    self.tool = Tools()
    self.system_prompt = self.build_system_input()
    self.model = InternLM2Chat(path) def build_system_input(self):
    tool_descs, tool_names = [], []
    for tool in self.tool.toolConfig:
    tool_descs.append(TOOL_DESC.format(**tool))
    tool_names.append(tool['name_for_model'])
    tool_descs = '\n\n'.join(tool_descs)
    tool_names = ','.join(tool_names)
    sys_prompt = REACT_PROMPT.format(tool_descs=tool_descs, tool_names=tool_names)
    return sys_prompt def parse_latest_plugin_call(self, text):
    plugin_name, plugin_args = '', ''
    i = text.rfind('\nAction:')
    j = text.rfind('\nAction Input:')
    k = text.rfind('\nObservation:')
    if 0 <= i < j: # If the text has `Action` and `Action input`,
    if k < j: # but does not contain `Observation`,
    text = text.rstrip() + '\nObservation:' # Add it back.
    k = text.rfind('\nObservation:')
    plugin_name = text[i + len('\nAction:') : j].strip()
    plugin_args = text[j + len('\nAction Input:') : k].strip()
    text = text[:k]
    return plugin_name, plugin_args, text def call_plugin(self, plugin_name, plugin_args):
    plugin_args = json5.loads(plugin_args)
    if plugin_name == 'google_search':
    return '\nObservation:' + self.tool.google_search(**plugin_args) def text_completion(self, text, history=[]):
    text = "\nQuestion:" + text
    response, his = self.model.chat(text, history, self.system_prompt)
    print(response)
    plugin_name, plugin_args, response = self.parse_latest_plugin_call(response)
    if plugin_name:
    response += self.call_plugin(plugin_name, plugin_args)
    response, his = self.model.chat(response, history, self.system_prompt)
    return response, his

AutoGPT

AutoGPT范式通过将任务发送给任务执行智能体A,将问题与A的结果存储至记忆,再将A的结果发送给任务创建智能体B,将B的结果存储至记忆,再将记忆发送给A,如此迭代直至符合条件。

ReWoo

ReWoo将用户输入进行计划拆分后运行,并将所有的结果整合为最后输出。

Agent与LangChain的关系

Agent属于Langchain的一部分。

智能体Agent-书生浦语大模型实战营学习笔记6&大语言模型10的更多相关文章

  1. C语言中setjmp与longjmp学习笔记

    C语言中setjmp与longjmp学习笔记 一.基础介绍 头文件:#include<setjmp.h> 原型:  int setjmp(jmp_buf envbuf) ,然而longjm ...

  2. 人工智能中小样本问题相关的系列模型演变及学习笔记(二):生成对抗网络 GAN

    [说在前面]本人博客新手一枚,象牙塔的老白,职业场的小白.以下内容仅为个人见解,欢迎批评指正,不喜勿喷![握手][握手] [再啰嗦一下]本文衔接上一个随笔:人工智能中小样本问题相关的系列模型演变及学习 ...

  3. 【学习笔记】大数据技术原理与应用(MOOC视频、厦门大学林子雨)

    1 大数据概述 大数据特性:4v volume velocity variety value 即大量化.快速化.多样化.价值密度低 数据量大:大数据摩尔定律 快速化:从数据的生成到消耗,时间窗口小,可 ...

  4. 深度学习中的序列模型演变及学习笔记(含RNN/LSTM/GRU/Seq2Seq/Attention机制)

    [说在前面]本人博客新手一枚,象牙塔的老白,职业场的小白.以下内容仅为个人见解,欢迎批评指正,不喜勿喷![认真看图][认真看图] [补充说明]深度学习中的序列模型已经广泛应用于自然语言处理(例如机器翻 ...

  5. CTR预估模型演变及学习笔记

    [说在前面]本人博客新手一枚,象牙塔的老白,职业场的小白.以下内容仅为个人见解,欢迎批评指正,不喜勿喷![握手][握手] [再啰嗦一下]如果你对智能推荐感兴趣,欢迎先浏览我的另一篇随笔:智能推荐算法演 ...

  6. Coursera台大机器学习基础课程学习笔记1 -- 机器学习定义及PLA算法

    最近在跟台大的这个课程,觉得不错,想把学习笔记发出来跟大家分享下,有错误希望大家指正. 一机器学习是什么? 感觉和 Tom M. Mitchell的定义几乎一致, A computer program ...

  7. 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性

    基于.net的分布式系统限流组件   在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...

  8. 【大数据】Sqoop学习笔记

    第1章 Sqoop简介 Sqoop是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型数据库(例如 : MyS ...

  9. 【大数据】Kafka学习笔记

    第1章 Kafka概述 1.1 消息队列 (1)点对点模式(一对一,消费者主动拉取数据,消息收到后消息清除) 点对点模型通常是一个基于拉取或者轮询的消息传送模型,这种模型从队列中请求信息,而不是将消息 ...

  10. 【大数据】Scala学习笔记

    第 1 章 scala的概述1 1.1 学习sdala的原因 1 1.2 Scala语言诞生小故事 1 1.3 Scala 和 Java  以及 jvm 的关系分析图 2 1.4 Scala语言的特点 ...

随机推荐

  1. CVS Excell乱码怎么修改

    问题描述 日常办公,从网页或者其他web端下载的表格(excel格式或csv格式),打开后发现中文乱码,如下: 常规解法 百度会发现,大部分提供的方法,就是通过excel的另存为,然后选择相应的编码, ...

  2. 解密数仓的SQL ON ANYWHERE技术

    本文分享自华为云社区<GaussDB DWS的SQL ON ANYWHERE技术解密>,作者:tooooooooooomy. 1. 前言 适用版本:[8.1.1(及以上)] 查询分析是大数 ...

  3. #直径,线段树#51nod 1766 树上的最远点对

    题目 多组询问,在 \([a,b]\) 和 \([c,d]\) 中分别选一个点 \(x,y\) ,使得 \(dis(x,y)\) 最大 分析 考虑直径的一个性质,两个点集两条直径的四个端点可能成为合并 ...

  4. Docker 解决 `denied: requested access to the resource is denied`

    背景 由于不可描述的原因,相对于以前,最近在更加频繁的迁移服务器,简单的 Shell 脚本已经不能满足需求了,于是将所有的项目 Docker 化. 部分不含敏感配置的项目准备放到 DockerHub ...

  5. OpenHarmony加速行业应用落地,多款软件发行版正通过兼容性测评

    4 月 25 日,OpenAtom OpenHarmony(以下简称"OpenHarmony")技术日在深圳举办,大会聚焦 OpenHarmony 3.1 Release 版本核心 ...

  6. SpringBoot中bean的生命周期

    目录 概述 使用场景 代码演示bean初始化 TestSupport BeanPostProcessorImpl log 代码 概述 Bean 生命周期管理是 Spring Boot 中的关键功能之一 ...

  7. Python 集合(Sets)1

    集合 集合用于在单个变量中存储多个项.集合是 Python 中的 4 种内置数据类型之一,用于存储数据集合,其他 3 种是列表(List).元组(Tuple)和字典(Dictionary),它们都具有 ...

  8. openGauss每日一练第6天

    学习地址 https://www.modb.pro/course/133 学习目标 学习 openGauss 创建模式.修改模式属性和删除模式 模式是一组数据库对象的集合,主要用于控制对数据库对象的访 ...

  9. opencv读取中文路径图片

    点击查看代码 img = cv2.imdecode(np.fromfile(filename, dtype=np.uint8), cv2.IMREAD_GRAYSCALE)

  10. DevEco Device Tool 3.1 Beta1版本发布,产品化配置优化添加自定义烧录器

    原文:https://mp.weixin.qq.com/s/lVENZqc-1getmkoSgCJiEg,点击链接查看更多技术内容.   HUAWEI DevEco Device Tool(以下内容简 ...