Skip to content

智能体 Agent 介绍



简介


智能体 Agent

  • 百度百科 能够感知环境并采取行动以实现特定目标的代理体。 它可以是软件、硬件或一个系统,具备自主性、适应性和交互能力。 智能体通过感知环境中的变化,根据自身学习到的知识和算法进行判断和决策,进而执行动作以影响环境或达到预定的目标。
  • Langchain: 代理的核心思想是使用语言模型来选择要采取的一系列操作。在代理中,语言模型被用作推理引擎来确定要采取哪些操作以及按什么顺序

  • Amazon AWS: 一种可以与环境交互、收集数据并利用数据执行自我决定任务以达到预定目标的软件程序。人类设定目标,但人工智能代理会独立选择实现这些目标所需的最佳行动。
  • IBM:AI 代理是能够通过设计其工作流和利用可用工具,代表用户或其他系统自主执行任务的系统或程序。这些代理不仅限于自然语言处理,还包括决策、解决问题、与外部环境交互和执行操作等功能

ai agent


ChatGPT 助理


Assistants 助理

由 GPT-4 等大型语言模型提供支持,能够为用户执行任务。这些助手根据模型上下文窗口中嵌入的指令进行操作。他们通常还可以使用允许助理执行更复杂任务的工具,例如运行代码或从文件中检索信息

Assistants playground


Assistants 助理


助理工具 Assistant Tools

使用 Assistants API 创建的助手可以配备工具,使他们能够执行更复杂的任务或与您的应用程序交互。我们为助手提供内置工具,但您也可以定义自己的工具来使用函数调用扩展其功能。Assistants API 目前支持以下工具:文件搜索 代码解释器 函数调用

Assistant Tools


函数调用 Function calling

助手 API 支持函数调用。函数调用允许您向 Assistants API 描述函数,并让它智能地返回需要调用的函数及其参数。

function call


Dify Agent


agent 应用配置

agent应用配置


搜索工具调用

搜索工具调用


代码解释器工具

  • python
  • javascript

代码解释器工具


文生图工具调用

文生图工具调用


更多工具

  • 内置工具
  • 自定义工具
  • 工作流工具

更多工具 dify


自定义工具

  • 外部工具暴露 http api
  • 通过 swagger 规范描述 api
  • 把 api 导入到 dify

自定义工具 dify


Web 自动化 Agent

web自动化 agent demo

{
  "openapi": "3.1.0",
  "info": {
    "title": "Web自动化Agent",
    "description": "霍格沃兹测试开发学社 Web自动化Agent ceshiren.com",
    "version": "0.1"
  },
  "servers": [
    {
      "url": "http://host.docker.internal:8000",
      "description": "docker",
      "name": "docker",
      "tags": ["docker"]
    }
  ],
  "paths": {
    "/open": {
      "get": {
        "summary": "Open",
        "description": "Opens a URL in the default browser.\n:param url:\n:return:",
        "operationId": "open",
        "parameters": [
          {
            "name": "url",
            "in": "query",
            "required": true,
            "schema": { "type": "string", "title": "Url" }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": { "application/json": { "schema": {} } }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HTTPValidationError" }
              }
            }
          }
        }
      }
    },
    "/click": {
      "get": {
        "summary": "Click",
        "description": "click element\n:param by: 尽可能使用css selector定位,带上id class组合的匹配条件\n:param value:\n:return:",
        "operationId": "click",
        "parameters": [
          {
            "name": "by",
            "in": "query",
            "required": true,
            "schema": { "title": "By" }
          },
          {
            "name": "value",
            "in": "query",
            "required": true,
            "schema": { "title": "Value" }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": { "application/json": { "schema": {} } }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HTTPValidationError" }
              }
            }
          }
        }
      }
    },
    "/send_keys": {
      "get": {
        "summary": "Send Keys",
        "description": "send keys to element\n :param by: 尽可能使用css selector定位,带上id class组合的匹配条件\n :param value:\n :param text:\n :return:",
        "operationId": "send_keys",
        "parameters": [
          {
            "name": "by",
            "in": "query",
            "required": true,
            "schema": { "title": "By" }
          },
          {
            "name": "value",
            "in": "query",
            "required": true,
            "schema": { "title": "Value" }
          },
          {
            "name": "text",
            "in": "query",
            "required": true,
            "schema": { "title": "Text" }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": { "application/json": { "schema": {} } }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HTTPValidationError" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "HTTPValidationError": {
        "properties": {
          "detail": {
            "items": { "$ref": "#/components/schemas/ValidationError" },
            "type": "array",
            "title": "Detail"
          }
        },
        "type": "object",
        "title": "HTTPValidationError"
      },
      "ValidationError": {
        "properties": {
          "loc": {
            "items": { "anyOf": [{ "type": "string" }, { "type": "integer" }] },
            "type": "array",
            "title": "Location"
          },
          "msg": { "type": "string", "title": "Message" },
          "type": { "type": "string", "title": "Error Type" }
        },
        "type": "object",
        "required": ["loc", "msg", "type"],
        "title": "ValidationError"
      }
    }
  }
}

Agent 架构


智能体是使用 LLM 来决定应用程序的控制流的系统

agent router

  • LLM 可以在潜在路径之间路由
  • LLM 可以决定调用众多工具
  • LLM 可以决定生成的答案是否足够或需要更多工作
  • 规划模式:计划执行 ReAct

计划执行 Plan-and-Execute

Plan-and-Execute

这与典型的 ReAct 风格代理相比,您需要一步一步地思考。 这种 "计划和执行 "风格代理的优势如下: 显式长期规划(即使是非常强大的 LLM 也难以应对) 能够在执行步骤中使用较小/较弱的模型,只在规划步骤中使用较大/较好的模型


ReAct 推理

react

rewoo


结构化输出

  • 提示工程:指示 LLM 以特定格式响应 json xml
  • 输出解析器:LLM 调后用处理 pydantic
  • 工具调用输出:利用一些 LLMs 来生成结构化输出 chatgpt
尽可能有益且准确地回应人类。

您可以访问以下工具:

[{"name": "google_search", "description": "用于执行 Google SERP 搜索并提取片段和网页的工具。输入应为搜索查询。", "parameters": {"type": "object", "properties": {"query": {"type": "string", "description": "搜索关键词"}}, "required": ["query"]}}]

使用 json blob 通过提供 action 键(工具名称)和 action_input 键(工具输入)来指定工具。
有效的“action”值:“最终答案”或 google_search

每个 $JSON_BLOB 仅提供一个操作,如下所示:

```
{
“action”:$TOOL_NAME,
“action_input”:$ACTION_INPUT
}
```

遵循以下格式:

问题:输入要回答的问题

想法:考虑之前和后续步骤

操作:
```
$JSON_BLOB
```

观察:操作结果
...(重复想法/操作/观察 N 次)

想法:我知道该如何回应

操作:
```
{
“action”:“最终答案”,
“action_input”:“对人类的最终回应”
}
```
开始!提醒始终使用单个操作的有效 json blob 进行响应。如有必要,请使用工具。如果合适,请直接回复。格式是操作:```$JSON_BLOB```然后观察:。

工具调用代理

  • 工具访问:LLM 可以选择并使用多种工具来完成任务。 tool call langgraph
  • 常用工具:代码解析器 搜索 通用工具调用 通用函数调用

记忆 Memory

  • 记忆:记忆是一种认知功能,它使人们能够存储、检索和使用信息来了解他们的现在和未来。
  • 短期记忆:允许代理访问在序列的早期步骤中获取的信息。
  • 长期记忆:使代理能够调用以前交互中的信息,例如对话中的过去消息。

Memory


Agent 智能体核心元素总结

  • LLM 大语言模型 chatgpt qwen llama
  • Planning:规划 路由 ReAct
  • Parser:结构化输出解析 json
  • Tools:工具集合与调用能力 tool function
  • Memery:记忆 短期 长期

agent


总结

  • ChatGPT 助理
  • Dify Agent
  • Agent 架构