码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 大模型从入门到应用——LangChain:代理(Agents)-[工具(Tools):多输入工具和工具输入模式]


    分类目录:《大模型从入门到应用》总目录

    LangChain系列文章:

    • 基础知识
    • 快速入门
      • 安装与环境配置
      • 链(Chains)、代理(Agent:)和记忆(Memory)
      • 快速开发聊天模型
    • 模型(Models)
      • 基础知识
      • 大型语言模型(LLMs)
        • 基础知识
        • LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(Human Input LLM)
        • 缓存LLM的调用结果
        • 加载与保存LLM类、流式传输LLM与Chat Model响应和跟踪tokens使用情况
      • 聊天模型(Chat Models)
        • 基础知识
        • 使用少量示例和响应流式传输
      • 文本嵌入模型
        • Aleph Alpha、Amazon Bedrock、Azure OpenAI、Cohere等
        • Embaas、Fake Embeddings、Google Vertex AI PaLM等
    • 提示(Prompts)
      • 基础知识
      • 提示模板
        • 基础知识
        • 连接到特征存储
        • 创建自定义提示模板和含有Few-Shot示例的提示模板
        • 部分填充的提示模板和提示合成
        • 序列化提示信息
      • 示例选择器(Example Selectors)
      • 输出解析器(Output Parsers)
    • 记忆(Memory)
      • 基础知识
      • 记忆的类型
        • 会话缓存记忆、会话缓存窗口记忆和实体记忆
        • 对话知识图谱记忆、对话摘要记忆和会话摘要缓冲记忆
        • 对话令牌缓冲存储器和基于向量存储的记忆
      • 将记忆添加到LangChain组件中
      • 自定义对话记忆与自定义记忆类
      • 聊天消息记录
      • 记忆的存储与应用
    • 索引(Indexes)
      • 基础知识
      • 文档加载器(Document Loaders)
      • 文本分割器(Text Splitters)
      • 向量存储器(Vectorstores)
      • 检索器(Retrievers)
    • 链(Chains)
      • 基础知识
      • 通用功能
        • 自定义Chain和Chain的异步API
        • LLMChain和RouterChain
        • SequentialChain和TransformationChain
        • 链的保存(序列化)与加载(反序列化)
      • 链与索引
        • 文档分析和基于文档的聊天
        • 问答的基础知识
        • 图问答(Graph QA)和带来源的问答(Q&A with Sources)
        • 检索式问答
        • 文本摘要(Summarization)、HyDE和向量数据库的文本生成
    • 代理(Agents)
      • 基础知识
      • 代理类型
      • 自定义代理(Custom Agent)
      • 自定义MRKL代理
      • 带有ChatModel的LLM聊天自定义代理和自定义多操作代理(Custom MultiAction Agent)
      • 工具
        • 基础知识
        • 自定义工具(Custom Tools)
        • 多输入工具和工具输入模式
        • 人工确认工具验证和Tools作为OpenAI函数
      • 工具包(Toolkit)
      • 代理执行器(Agent Executor)
        • 结合使用Agent和VectorStore
        • 使用Agents的异步API和创建ChatGPT克隆
        • 处理解析错误、访问中间步骤和限制最大迭代次数
        • 为代理程序设置超时时间和限制最大迭代次数和为代理程序和其工具添加共享内存
      • 计划与执行
    • 回调函数(Callbacks)

    ----### 多输入工具
    本节演示如何在Agent中使用需要多个输入的工具,推荐的方法是使用StructuredTool类。

    import os
    os.environ["LANGCHAIN_TRACING"] = "true"
    from langchain import OpenAI
    from langchain.agents import initialize_agent, AgentType
    
    llm = OpenAI(temperature=0)
    from langchain.tools import StructuredTool
    
    def multiplier(a: float, b: float) -> float:
        """Multiply the provided floats."""
        return a * b
    
    tool = StructuredTool.from_function(multiplier)
    # Structured tools are compatible with the STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION agent type. 
    agent_executor = initialize_agent([tool], llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
    agent_executor.run("What is 3 times 4")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    日志输出:

    Entering new AgentExecutor chain...
    
    Thought: I need to multiply 3 and 4
    Action:
    
    • 1
    • 2
    • 3
    • 4

    {
    “action”: “multiplier”,
    “action_input”: {“a”: 3, “b”: 4}
    }

    
    Observation: 12
    Thought: I know what to respond
    Action:
    
    • 1
    • 2
    • 3
    • 4

    {
    “action”: “Final Answer”,
    “action_input”: “3 times 4 is 12”
    }

    
    Finished chain.
    
    • 1
    • 2

    输出:

    '3 times 4 is 12'
    
    • 1
    使用字符串格式的多输入工具

    与结构化工具相比,另一种方法是使用常规的Tool类,并接受单个字符串作为输入。然后,工具必须处理解析逻辑,从文本中提取相关值,这将使工具表示与Agent提示紧密耦合。如果底层语言模型无法可靠地生成结构化模式,这仍然很有用。

    让我们以乘法函数为例。为了使用它,我们将告诉Agent生成以逗号分隔的长度为两个的Action Input。然后,我们编写一个简单的包装器,将字符串分割成两部分(以逗号为界),并将两个解析后的值作为整数传递给乘法函数。

    from langchain.llms import OpenAI
    from langchain.agents import initialize_agent, Tool
    from langchain.agents import AgentType
    
    • 1
    • 2
    • 3

    以下是乘法函数及其字符串解析器的示例:

    def multiplier(a, b):
        return a * b
    
    def parsing_multiplier(string):
        a, b = string.split(",")
        return multiplier(int(a), int(b))
    llm = OpenAI(temperature=0)
    tools = [
        Tool(
            name = "Multiplier",
            func=parsing_multiplier,
            description="useful for when you need to multiply two numbers together. The input to this tool should be a comma separated list of numbers of length two, representing the two numbers you want to multiply together. For example, `1,2` would be the input if you wanted to multiply 1 by 2."
        )
    ]
    mrkl = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
    mrkl.run("What is 3 times 4")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    日志输出:

    Entering new AgentExecutor chain...
     I need to multiply two numbers
    Action: Multiplier
    Action Input: 3,4
    Observation: 12
    Thought: I now know the final answer
    Final Answer: 3 times 4 is 12
    
    Finished chain.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输出:

    '3 times 4 is 12'
    
    • 1

    工具输入模式

    默认情况下,工具通过检查函数签名来推断参数模式。对于更严格的要求,可以指定自定义输入模式,以及自定义的验证逻辑。

    from typing import Any, Dict
    
    from langchain.agents import AgentType, initialize_agent
    from langchain.llms import OpenAI
    from langchain.tools.requests.tool import RequestsGetTool, TextRequestsWrapper
    from pydantic import BaseModel, Field, root_validator
    
    llm = OpenAI(temperature=0)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    我们还需安装tldextract:

    !pip install tldextract > /dev/null
    notice A new release of pip is available: 23.0.1 -> 23.1
    notice To update, run: pip install --upgrade pip
    
    • 1
    • 2
    • 3

    输入:

    import tldextract
    
    _APPROVED_DOMAINS = {
        "langchain",
        "wikipedia",
    }
    
    class ToolInputSchema(BaseModel):
    
        url: str = Field(...)
        
        @root_validator
        def validate_query(cls, values: Dict[str, Any]) -> Dict:
            url = values["url"]
            domain = tldextract.extract(url).domain
            if domain not in _APPROVED_DOMAINS:
                raise ValueError(f"Domain {domain} is not on the approved list:"
                                 f" {sorted(_APPROVED_DOMAINS)}")
            return values
        
    tool = RequestsGetTool(args_schema=ToolInputSchema, requests_wrapper=TextRequestsWrapper())
    agent = initialize_agent([tool], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=False)
    # This will succeed, since there aren't any arguments that will be triggered during validation
    answer = agent.run("What's the main title on langchain.com?")
    print(answer)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    输出:

    The main title of langchain.com is "LANG CHAIN 🦜️🔗 Official Home Page"
    
    • 1

    将会报错的输入:

    agent.run("What's the main title on google.com?")
    
    • 1

    输出:

    ---------------------------------------------------------------------------
    
    ValidationError                           Traceback (most recent call last)
    
    Cell In[7], line 1
    ----> 1 agent.run("What's the main title on google.com?")
    
    
    File ~/code/lc/lckg/langchain/chains/base.py:213, in Chain.run(self, *args, **kwargs)
        211     if len(args) != 1:
        212         raise ValueError("`run` supports only one positional argument.")
    --> 213     return self(args[0])[self.output_keys[0]]
        215 if kwargs and not args:
        216     return self(kwargs)[self.output_keys[0]]
    
    
    File ~/code/lc/lckg/langchain/chains/base.py:116, in Chain.__call__(self, inputs, return_only_outputs)
        114 except (KeyboardInterrupt, Exception) as e:
        115     self.callback_manager.on_chain_error(e, verbose=self.verbose)
    --> 116     raise e
        117 self.callback_manager.on_chain_end(outputs, verbose=self.verbose)
        118 return self.prep_outputs(inputs, outputs, return_only_outputs)
    
    
    File ~/code/lc/lckg/langchain/chains/base.py:113, in Chain.__call__(self, inputs, return_only_outputs)
        107 self.callback_manager.on_chain_start(
        108     {"name": self.__class__.__name__},
        109     inputs,
        110     verbose=self.verbose,
        111 )
        112 try:
    --> 113     outputs = self._call(inputs)
        114 except (KeyboardInterrupt, Exception) as e:
        115     self.callback_manager.on_chain_error(e, verbose=self.verbose)
    
    
    File ~/code/lc/lckg/langchain/agents/agent.py:792, in AgentExecutor._call(self, inputs)
        790 # We now enter the agent loop (until it returns something).
        791 while self._should_continue(iterations, time_elapsed):
    --> 792     next_step_output = self._take_next_step(
        793         name_to_tool_map, color_mapping, inputs, intermediate_steps
        794     )
        795     if isinstance(next_step_output, AgentFinish):
        796         return self._return(next_step_output, intermediate_steps)
    
    
    File ~/code/lc/lckg/langchain/agents/agent.py:695, in AgentExecutor._take_next_step(self, name_to_tool_map, color_mapping, inputs, intermediate_steps)
        693         tool_run_kwargs["llm_prefix"] = ""
        694     # We then call the tool on the tool input to get an observation
    --> 695     observation = tool.run(
        696         agent_action.tool_input,
        697         verbose=self.verbose,
        698         color=color,
        699         **tool_run_kwargs,
        700     )
        701 else:
        702     tool_run_kwargs = self.agent.tool_run_logging_kwargs()
    
    
    File ~/code/lc/lckg/langchain/tools/base.py:110, in BaseTool.run(self, tool_input, verbose, start_color, color, **kwargs)
        101 def run(
        102     self,
        103     tool_input: Union[str, Dict],
       (...)
        107     **kwargs: Any,
        108 ) -> str:
        109     """Run the tool."""
    --> 110     run_input = self._parse_input(tool_input)
        111     if not self.verbose and verbose is not None:
        112         verbose_ = verbose
    
    
    File ~/code/lc/lckg/langchain/tools/base.py:71, in BaseTool._parse_input(self, tool_input)
         69 if issubclass(input_args, BaseModel):
         70     key_ = next(iter(input_args.__fields__.keys()))
    ---> 71     input_args.parse_obj({key_: tool_input})
         72 # Passing as a positional argument is more straightforward for
         73 # backwards compatability
         74 return tool_input
    
    
    File ~/code/lc/lckg/.venv/lib/python3.11/site-packages/pydantic/main.py:526, in pydantic.main.BaseModel.parse_obj()
    
    
    File ~/code/lc/lckg/.venv/lib/python3.11/site-packages/pydantic/main.py:341, in pydantic.main.BaseModel.__init__()
    
    
    ValidationError: 1 validation error for ToolInputSchema
    __root__
      Domain google is not on the approved list: ['langchain', 'wikipedia'] (type=value_error)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    参考文献:
    [1] LangChain官方网站:https://www.langchain.com/
    [2] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/
    [3] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/

  • 相关阅读:
    弘辽科技:做好产品标题,让链接免费流量快速爆起来
    【Linux】Centos 8 服务器部署:阿里云端口开放与应用实例教程
    “益路同行”栏目人物专访 第0010期——中国公益万里行发起人李现
    实现PXE批量网络装机及kickstrat无人值守安装
    Win11关闭【显示更多选项】|【show more options】
    异常检测 | MATLAB实现基于支持向量机和孤立森林的数据异常检测(结合t-SNE降维和DBSCAN聚类)
    JWT认证、drf-jwt安装和简单使用、实战之使用Django auth的User表自动签发、实战之自定义User表,手动签发
    【Leetcode每日一题:907.子数组的最小值之和~~~单调栈】
    三个经典示例解释什么是「零知识证明」
    c++ list容器使用详解
  • 原文地址:https://blog.csdn.net/hy592070616/article/details/132839727
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号