构建LangChain应用程序的示例代码:16、具有插件检索功能的自定义代理(Custom Agent with PlugIn Retrieval)

自定义代理与插件检索

这个笔记本结合了两个概念,以构建一个能够与AI插件交互的自定义代理:

自定义代理与工具检索:这引入了检索多个工具的概念,这在尝试使用任意多的插件时非常有用。
自然语言API链:这在OpenAPI端点周围创建了自然语言包装器。这很有用,因为(1)插件在幕后使用OpenAPI端点,(2)将它们包装在NLAChain中可以让路由器代理更容易地调用它们。

这个笔记本引入的新思想是使用检索来选择不是工具本身,而是要使用的OpenAPI规范集。然后我们可以从这些OpenAPI规范生成工具。这在尝试让代理使用插件时非常有用。可能更有效的是先选择插件,然后选择端点,而不是直接选择端点。这是因为插件可能包含更多有用的信息用于选择。

设置环境

进行必要的导入等。

import re
from typing import Union

from langchain.agents import (
    AgentExecutor,
    AgentOutputParser,
    LLMSingleActionAgent,
)
from langchain.chains import LLMChain
from langchain.prompts import StringPromptTemplate
from langchain_community.agent_toolkits import NLAToolkit
from langchain_community.tools.plugin import AIPlugin
from langchain_core.agents import AgentAction, AgentFinish
from langchain_openai import OpenAI

设置LLM

llm = OpenAI(temperature=0)

设置插件

加载并索引插件

urls = [
    "https://datasette.io/.well-known/ai-plugin.json",
    "https://api.speak.com/.well-known/ai-plugin.json",
    "https://www.wolframalpha.com/.well-known/ai-plugin.json",
    "https://www.zapier.com/.well-known/ai-plugin.json",
    "https://www.klarna.com/.well-known/ai-plugin.json",
    "https://www.joinmilo.com/.well-known/ai-plugin.json",
    "https://slack.com/.well-known/ai-plugin.json",
    "https://schooldigger.com/.well-known/ai-plugin.json",
]

AI_PLUGINS = [AIPlugin.from_url(url) 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Hugo_Hoo

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值