当前位置:   article > 正文

LangChain-17 FunctionCalling 利用大模型对函数进行回调 扩展大模型的额外的能力 比如实现加减乘除等功能_langchain function call

langchain function call

请添加图片描述

背景介绍

引用:
Function Calling是一种允许用户在使用大型语言模型(如GPT系列)处理特定问题时,定制并调用外部函数的功能。这些外部函数可以是专门为处理特定任务(如数据分析、图像处理等)而设计的代码块。通过Function Calling,大模型可以调用这些外部函数获取信息,然后根据这些信息生成相应的输出,从而实现更加复杂和专业化的任务处理能力。

安装依赖


pip install -qU langchain-core langchain-openai
  • 1
  • 2

编写代码

这里封装了一些类:加、减、乘、除
通过: llm_with_tools = llm.bind_tools([Add, Multiply, Subtract]) 将它们组合起来,接着交给大模型。

from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI


class Multiply(BaseModel):
    """Multiply two integers together."""
    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")


class Add(BaseModel):
    """Add two integers together."""
    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")


class Subtract(BaseModel):
    """Subtract two integers."""
    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")


llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
llm_with_tools = llm.bind_tools([Add, Multiply, Subtract])

message1 = llm_with_tools.invoke("what's 3 * 12")
print(f"message1: {message1}")

message2 = llm_with_tools.invoke("what's 3 + 12'")
print(f"message2: {message2}")

message3 = llm_with_tools.invoke("what's 3 - 12'")
print(f"message3: {message3}")
  • 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

运行结果

➜ python3 test17.py
message1: content='3 * 12 is equal to 36.' response_metadata={'finish_reason': 'stop', 'logprobs': None}
message2: content='The result of 3 + 12 is 15.' response_metadata={'finish_reason': 'stop', 'logprobs': None}
message3: content='' additional_kwargs={'tool_calls': [{'id': 'call_aXV9LQfp4COEzB5b93nazfkv', 'function': {'arguments': '// Using the Subtract function from the functions namespace\nfunctions.Subtract({a: 3, b: 12});', 'name': 'python'}, 'type': 'function'}]} response_metadata={'finish_reason': 'tool_calls', 'logprobs': None}
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

转换函数

我们可以通过工具,将函数转换为JSON的形式

import json
from langchain_core.utils.function_calling import convert_to_openai_tool


def multiply(a: int, b: int) -> int:
    """Multiply two integers together.

    Args:
        a: First integer
        b: Second integer
    """
    return a * b


print(json.dumps(convert_to_openai_tool(multiply), indent=2))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出结果

➜ python3 test17.py
{
  "type": "function",
  "function": {
    "name": "multiply",
    "description": "Multiply two integers together.",
    "parameters": {
      "type": "object",
      "properties": {
        "a": {
          "type": "integer",
          "description": "First integer"
        },
        "b": {
          "type": "integer",
          "description": "Second integer"
        }
      },
      "required": [
        "a",
        "b"
      ]
    }
  }
}
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/914397
推荐阅读
相关标签
  

闽ICP备14008679号