GPT实战系列-如何让LangChain的Agent选择工具

GPT实战系列-如何让LangChain的Agent选择工具

LangChain

GPT实战系列-LangChain如何构建基通义千问的多工具链

GPT实战系列-构建多参数的自定义LangChain工具

GPT实战系列-通过Basetool构建自定义LangChain工具方法

GPT实战系列-一种构建LangChain自定义Tool工具的简单方法

GPT实战系列-搭建LangChain流程简单应用

GPT实战系列-简单聊聊LangChain搭建本地知识库准备

GPT实战系列-LangChain + ChatGLM3构建天气查询助手

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-简单聊聊LangChain

大模型查询工具助手之股票免费查询接口

随着OpenAI的GPT-4这样的大型语言模型(LLMs)已经风靡全球,现在让它们自动执行各种任务,如回答问题、翻译语言、分析文本等。LLMs是在交互上真正体验到像“人工智能”。

如何管理这些模块呢?

LangChain在这方面发挥重要作用。LangChain使构建由LLMs驱动的应用程序变得简单,使用LangChain,可以在统一的界面中轻松与不同类型的LLMs进行交互,管理模型版本,管理对话版本,并将LLMs连接在一起。

在这里插入图片描述

对于任何用户输入,当知道工具使用的具体顺序时,Chain就很好。但是对于某些情况,使用哪些工具,调用多少次取决于用户输入。在这些情况下,我们就希望让模型决定使用工具的次数和顺序。这就是Agent。

LangChain自带了许多内置的Agent,这些Agent针对 不同情况,类型。

举个例子,如果尝试一下 OpenAI 工具代理,它利用新的 OpenAI 工具调用 API。

设置环境变量

import getpass
import os

os.environ["OPENAI_API_KEY"] = getpass.getpass()

定义Tools

同前篇所示,为了让Agent可以选,实现三个自定义工具 Tools,首先需要做一些配置初始化的工作,导入langchain相关的包。

from langchain_core.tools import tool

@tool
def multiply(first_int: int, second_int: int) -> int:
    """Multiply two integers together."""
    return first_int * second_int

@tool
def add(first_int: int, second_int: int) -> int:
    "Add two integers."
    return first_int + second_int

@tool
def exponentiate(base: int, exponent: int) -> int:
    "Exponentiate the base to the exponent power."
    return base**exponent

tools = [multiply, add, exponentiate]

构建Prompt

实现代码,创建Prompt模版,配置大模型,以及输出解析函数。

from langchain import hub
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_openai import ChatOpenAI

prompt = hub.pull("hwchase17/openai-tools-agent")

创建并调用Agent

把各碎片链接起来,建立Agent,

#引用OpenAI模型,创建代理
model = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0)

agent = create_openai_tools_agent(model, tools, prompt)

# 执行
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

agent_executor.invoke(
    {
        "input": "Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result"
    }
)

输出结果:

> Entering new AgentExecutor chain...
Invoking: `exponentiate` with `{'base': 3, 'exponent': 5}`
243

Invoking: `add` with `{'first_int': 12, 'second_int': 3}`
15

Invoking: `multiply` with `{'first_int': 243, 'second_int': 15}`
3645

Invoking: `exponentiate` with `{'base': 3645, 'exponent': 2}`
13286025The result of raising 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.

> Finished chain.

{'input': 'Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result',
 'output': 'The result of raising 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.'}

是不是很有趣?

LangChain是一个Python框架,可以使用LLMs构建应用程序。它与各种模块连接,使与LLM和提示管理,一切变得简单。

觉得有用 收藏 收藏 收藏

点个赞 点个赞 点个赞

End

GPT专栏文章:

GPT实战系列-实战Qwen通义千问在Cuda 12+24G部署方案_通义千问 ptuning-CSDN博客

GPT实战系列-ChatGLM3本地部署CUDA11+1080Ti+显卡24G实战方案

GPT实战系列-Baichuan2本地化部署实战方案

GPT实战系列-让CodeGeeX2帮你写代码和注释_codegeex 中文-CSDN博客

GPT实战系列-ChatGLM3管理工具的API接口_chatglm3 api文档-CSDN博客

GPT实战系列-大话LLM大模型训练-CSDN博客

GPT实战系列-LangChain + ChatGLM3构建天气查询助手

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(二)

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(一)

GPT实战系列-ChatGLM2模型的微调训练参数解读

GPT实战系列-如何用自己数据微调ChatGLM2模型训练

GPT实战系列-ChatGLM2部署Ubuntu+Cuda11+显存24G实战方案

GPT实战系列-Baichuan2等大模型的计算精度与量化

GPT实战系列-GPT训练的Pretraining,SFT,Reward Modeling,RLHF

GPT实战系列-探究GPT等大模型的文本生成-CSDN博客

  • 40
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ChatGLM2-6B是一个基于GPT模型的对话生成模型,用于生成自然语言对话。***UDM/ChatGLM2-6B)获取更多关于该模型的详细信息。此外,您也可以在[Hugging Face](https://huggingface.co/THUDM/chatglm2-6b)上找到该模型的地址。 如果您希望使用chaglm2-6b langchain,您可以按照以下步骤进行操作: 1. 创建虚拟环境: ``` conda create -n langchain-chatglm python==3.10.9 ``` 2. 激活虚拟环境: ``` conda activate langchain-chatglm ``` 3. 部署ChatGLM: ``` md c:\ChatGLM cd c:\ChatGLM git clone https://github.com/imClumsyPanda/langchain-ChatGLM cd langchain-ChatGLM pip3 install -r requirements.txt ``` 如果出现报错(通常是传输超时),您可以尝试重新运行几次。 4. 启动ChatGLM2: ``` python.exe ./webui.py --model-name chatglm2-6b ``` 请注意,以上步骤是为了使用chaglm2-6b langchain,确保您已经按照要求正确设置了虚拟环境并部署了ChatGLM2。希望这能帮助到您!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ChatGLM2-6B的P-Tuning微调](https://blog.csdn.net/weixin_43815222/article/details/131553200)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [windows10下本地部署langChain-ChatGLM2-6B](https://blog.csdn.net/qq_43335960/article/details/131085293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex_StarSky

你的鼓励是创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值