GLM4——Function calling(函数调用)

作用:

Function Calling可以根据用户的输入自行判断何时需要调用哪些函数,并且可以根据目标函数的描述生成符合要求的请求参数。开发人员可以使用函数调用能力,通过GPT实现:

在进行自然语言交流时,通过调用外部工具回答问题(类似于ChatGPT插件);

将自然语言转换为调用API时使用的参数,或者查询数据库时使用的条件;

假设我们要创建一个具备查询航班功能的聊天机器人。我们定义如下两个外部函数供模型选择调用:

  1. 查询两地之间某日航班号函数:get_flight_number(departure: str, destination: str, date: str)

  2. 查询航班号以及日期查询票价函数:get_ticket_price(flight_number: str, date: str)

一、定义本地函数

为了向模型描述外部函数库,需要向 tools 字段传入可以调用的函数列表。参数如下表:

参数名称 类型 是否必填 参数说明
type String 设置为function
function Object
name String 函数名称
description String 用于描述函数功能。模型会根据这段描述决定函数调用方式
parameters Object parameters Object是数所接受的参数。若调用函数时不需要传入参数,省略该参数即可

代码

tools = [
    {
   
   
        "type": "function",
        "function": {
   
   
            "name": "get_flight_number",
            "description": "根据始发地、目的地和日期,查询对应日期的航班号",
            "parameters": {
   
   
                "type": "object",
                "properties": {
   
   
                    "departure": {
   
   
                        "description": "出发地",
                        "type": "string"
                    },
                    "destination": {
   
   
                        "description": "目的地",
                        "type": "string"
                    },
                    "date": {
   
   
                        "description": "日期",
                        "type": "string",
                    }
                },
                "required": [ "departure", "destination", "date" ]
            },
        }
    },
    {
   
   
        "type": "function",
        "function": {
   
   
            "name": "get_ticket_price",
            "description": "查询某航班在某日的票价",
            "parameters": {
   
   
                "type": "object",
                "properties": {
   
   
                    "flight_number": {
   
   
                        "description": "航班号",
                        "type": "string"
                    },
                    "date": {
   
   
                        "description"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

gm0012

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

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

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

打赏作者

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

抵扣说明:

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

余额充值