大语言模型学习: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. KingbaseES V8R6备份恢复案例之---备份crond计划任务的调整

    案例说明: 在KingbaseES V8R6数据库通常备份建立的crond任务,默认由root用户建立,写入到/etc/cron.d/KINGBASECRON文件中,在有的生产环境限制了普通用户使用r ...

  2. KingbaseESV8R6普通用户无权限执行vacuum

    背景 数据库日志有如下提示: WARNING: skipping "pivot_t1" --- only table or database owner can vacuum it ...

  3. java:寻找两个字符串的最长公共子串

    java:寻找两个字符串的最长公共子串 // 找一个字符串的所有子串 public static List<String> findAllStr(String s) { List<S ...

  4. Scala 简单分词求和

    1 package chapter07 2 3 object Test17_CommonWordCount { 4 def main(args: Array[String]): Unit = { 5 ...

  5. 企业数据清洗项目实践day3

    封装昨天的生成json方法,生成标准字段方法以及生成最终文件的方法. 1 import pandas as pd 2 import xlwt 3 """ 4 rank的一 ...

  6. 动图演示步骤 Vmware安装Centos-7 最小安装/图形化界面及常见错误参考,基础配置推荐

    程序软件工具安装篇 --[Linux](Vmware/Centos-7) 目录 程序软件工具安装篇 --[Linux](Vmware/Centos-7) ①:文件准备工作 虚拟机工具安装文件 系统镜像 ...

  7. #最小生成树,Trie#CF888G Xor-MST

    题目 给定 \(n\) 个结点的无向完全图.每个点有一个点权为 \(a_i\) . 连接 \(i\) 号结点和 \(j\) 号结点的边的边权为 \(a_i\oplus a_j\) . 求这个图的 MS ...

  8. #启发式合并,链表#洛谷 3201 [HNOI2009] 梦幻布丁

    题目 \(n\)个布丁摆成一行,进行\(m\)次操作. 每次将某个颜色的布丁全部变成另一种颜色的, 然后再询问当前一共有多少段颜色. (\(n,m\leq 10^5,col\leq 10^6\)) 分 ...

  9. npm/cnpm 设置镜像地址

    npm 查看当前镜像源: npm config get registry # https://registry.npmjs.org/ 修改当前镜像源: npm config set registry ...

  10. 踩坑指南:入门OpenTenBase之监控篇

    本次监控将采用Prometheus.Grafana可视化工具以及postgres_exporter对OpenTenBase进行全面监控和优化. 安装监控 Docker安装 1.Docker要求 Cen ...