当前位置: 首页 > news >正文

【大语言模型】LangChain 核心模块介绍(Agents)

【大语言模型】LangChain 核心模块介绍(Agents)

  • 一、简介
  • 二、Agents 的核心概念
  • 三、实战案例
    • 3.1、需求说明
    • 3.2、实现思路
    • 3.3、完整源码

一、简介

我们都知道四肢的绝大部分动作都是由大脑决定的,大脑通过中枢神经下发自己的操作指令,四肢就可以按照我们的需求进行动作。

在这里插入图片描述

而大语言模型其实就相当于一个大脑的角色,其本身是无法直接进行任何操作的。那么就需要一个”中枢神经“,帮助它下发指令,传递反馈信息,这个中枢神经就是 Agent。

在这里插入图片描述
在官方文档中,有这样的一句说明,讲出了 Agents 的核心。

The core idea of agents is to use a language model to choose a sequence of actions to take.
代理的核心思想是使用语言模型来选择要采取的一系列行动。

如果在前面学习的过程中,通过 LangChain 让大模型了解的是“怎么思考”。那 Agents 的作用就是让大模型自己去决定“怎么做”。其实就是基于ReaAct理念的一种实践方式。

二、Agents 的核心概念

Agents 的核心思想是使用语言模型来选择要采取的一系列操作。语言模型所起到的作用为,被用作推理引擎来确定要采取哪些操作以及按什么顺序。在这个过程里面主要包含以下核心组件:

  • Schema
    • AgentAction
    • AgentFinish
    • Intermediate Steps
  • Agent
    • Agent Inputs
    • Agent Outputs
  • AgentExecutor
  • Tools
    • Considerations
  • Toolkits

三、实战案例

3.1、需求说明

通过 Agent 实现调用 Selenium 打开、退出浏览器、获取当前 URL 等操作。

3.2、实现思路

  • 实现工具包。
  • 初始化 Agent

3.3、完整源码

封装 Selenium 工具: 将会使用到的行为操作,提前封装起来

from selenium import webdriverclass WebAutoFramework:def __init__(self):self.driver = Noneif self.driver is None:# self.driver = webdriver.Firefox()option = webdriver.FirefoxOptions()option.binary_location = r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe"self.driver = webdriver.Firefox(options=option)  # 这里添加的是driver的绝对路径self.driver.implicitly_wait(5)def open(self, url):self.driver.get(url)def quit(self):self.driver.quit()def get_title(self):return self.driver.title

定义工具池:将所有的行为操作,按照要求的tools规范定义清楚。

from langchain_core.tools import toolfrom Artificial_intelligence.大语言模型应用开发框架L2.核心模块Agents.web_auto_selenium import WebAutoFrameworkweb = WebAutoFramework()@tool
def open_web(url):"""打开一个网页"""web.open(url)# if __name__ == '__main__':
#     print(open.name)
#     print(open.args)
#     print(open.description)@tool
def quit_web():"""退出浏览器"""web.quit()@tool
def get_title():"""获取网页标题:return:"""print(web.get_title())tools = [open_web, quit_web, get_title]

初始化 Agent 并执行, 将封装好的 tools 传入 agent 中:

from langchain import hub
from langchain.agents import create_openai_functions_agent, AgentExecutor
from langchain_openai import ChatOpenAIfrom Artificial_intelligence.大语言模型应用开发框架L2.核心模块Agents.create_selenium_tools import tools
import osos.environ["OPENAI_API_KEY"] = "xxxxxxxxxxxxxxx"  # 将个人token替换到这个位置
os.environ["OPENAI_API_BASE"] = "xxxxxxxxxxxxxxx"prompt = hub.pull("hwchase17/openai-functions-agent")llm = ChatOpenAI(model="gpt-3.5-turbo")# 初始化 Agent
agent = create_openai_functions_agent(llm, tools, prompt)
# 初始化 Agent 的配置
agent_executor = AgentExecutor(agent=agent,tools=tools, verbose=True,return_intermediate_steps=True, handle_parsing_errors=True)
if __name__ == '__main__':agent_executor.invoke({"input": """请打开 https://www.baidu.com/ 网站返回当前的网页的标题,再退出浏览器。"""})

执行结果:

> Entering new AgentExecutor chain...Invoking: `open_web` with `{'url': 'https://www.baidu.com/'}`None
Invoking: `get_title` with `{}`百度一下,你就知道
None
Invoking: `quit_web` with `{}`None我已经打开了 https://www.baidu.com/ 网站,并获取了当前网页的标题为 "百度一下,你就知道"。现在已经退出了浏览器。如果您有任何其他问题或需要进一步帮助,请随时告诉我。> Finished chain.进程已结束,退出代码为 0
http://www.lryc.cn/news/503603.html

相关文章:

  • 19C-RAC 环境mgmtca.trc.1过大
  • 基于Spring Boot的同城宠物照看系统的设计与实现
  • 爬虫学习案例5
  • 视频监控汇聚平台方案设计:Liveweb视频智能监管系统方案技术特点与应用
  • ansible自动化运维(三)jinja2模板roles角色管理
  • 队列+宽搜_429. N 叉树的层序遍历_二叉树最大宽度
  • Windows11安装及使用nvm
  • (一)机器学习 - 入门
  • 【解决】k8s使用kubeadm初始化集群失败问题整理
  • apache-dubbo
  • ECharts柱状图-柱图2,附视频讲解与代码下载
  • 【新人系列】Python 入门(十六):正则表达式
  • HTML综合
  • 孚盟云 MailAjax.ashx SQL注入漏洞复现
  • 解决“VMware虚拟机报Intel VT-x”错误
  • NiceGUI `ui.table` 基础
  • 分布式 Raft算法 总结
  • C++ 中面向对象编程如何实现动态绑定?
  • 微服务-01
  • 这是一个vue3 + scss的数字滚动效果
  • 数字证书管理工具 openssl keytool
  • Polars数据聚合与旋转实战教程
  • 引用类型集合的深拷贝,无需手动写循环:Apache Commons Lang (SerializationUtils)
  • HTML、CSS表格的斜表头样式设置title 画对角线
  • docker 安装mysql 5.7 详细保姆级教程
  • Kioptrix level3
  • UE5 Lyra项目源码分析-关卡配置加载
  • Cursor重置机器码-解决Too many free trials.
  • transformer学习笔记-自注意力机制(2)
  • 呼叫中心呼入大模型如何对接传统呼叫中心系统?