前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何理解 ChatGPT API function calling?

如何理解 ChatGPT API function calling?

作者头像
一个正经的测试
发布2024-01-22 17:06:49
2520
发布2024-01-22 17:06:49
举报
Function call

概述

函式呼叫(function calling) 可说是这次ChatGPT API 更新的杀手级更新。所谓函式呼叫,就是让你把外部函式的形状写入ChatGPT API,这样ChatGPT API 就能输出对的格式来呼叫你的函式。让我们透过官方发布的例子来了解:

例子

假如你现在问ChatGPT 「波士顿目前天气如何」,ChatGPT 会吐给你乱回答的内容,因为它的训练资料只到2021 年。假如要有最新的资料,会需要呼叫外部的API,例如有个天气API,呼叫后可以拿到最新的天气资讯。

假设今天这个天气API 的输入格式要两个参数,一个是地点,需要是字串;第二个是单位,需要是摄氏或华氏。过去的作法是,你要在指令(prompt) 里面跟ChatGPT 说,请输出一个JSON 字串,里面要包含地点与单位,这两个栏位。但这时就有个问题,即使你有下这个指令,ChatGPT 的输出仍可能会不如你预期。最常见的状况是,它会回「好的没问题,以下是JSON 字串的输出」这个开头的回复

💣 然后这时候就炸掉了,因为当你把「好的没问题,以下是JSON 字串的输出」输入到天气API,天气API 会没办法处理。这是过去要做ChatGPT 应用程式会遇到的一个痛点。要能有效整合,就需要有对的格式。格式错误将可能让程式运行出问题,弄不好整组炸掉就悲剧了。

函式呼叫这功能就是让ChatGPT API 能用对的格式跟外部工具互动。今天改成用函式呼叫的功能,ChatGPT API 会依据你定义好的格式,给出对的输出。以这边为例,它不会再用「好的没问题,以下是JSON 字串的输出」开头。而是会直接输出一个JSON 字串。这时你可以拿去打天气API。

API调用

代码语言:javascript
复制
import requests
import os
import json

url = "https://api.openai.com/v1/chat/completions"

auth = ('', os.getenv('OPENAI_API_KEY'))

headers = {
  "Content-Type": "application/json",
}

data = {
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {"role": "user", "content": "What is the weather like in Boston?"}
  ],
  "functions": [
    {
      "name": "get_current_weather",
      "description": "Get the current weather in a given location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA"
          },
          "unit": {
            "type": "string",
            "enum": ["celsius", "fahrenheit"]
          }
        },
        "required": ["location"]
      }
    }
  ]
}

response = requests.post(url, auth=auth, headers=headers, data=json.dumps(data))

print(response.text)

这时会拿到

代码语言:javascript
复制
{
  "id": "chatcmpl-123",
  ...
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": null,
      "function_call": {
        "name": "get_current_weather",
        "arguments": "{ \"location\": \"Boston, MA\"}"
      }
    },
    "finish_reason": "function_call"
  }]
}

打完天气API,你拿到波士顿:22 度、单位:摄氏,接着再喂回去给ChatGPT API,这时ChatGPT API 就能精准回覆「波士顿现在的天气是摄氏22 度」

把上面的结果拿去呼叫天气API,会拿到

代码语言:javascript
复制
import requests
import os
import json

url = "https://api.openai.com/v1/chat/completions"

auth = ('', os.getenv('OPENAI_API_KEY'))

headers = {
  "Content-Type": "application/json",
}

data = {
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {"role": "user", "content": "What is the weather like in Boston?"},
    {"role": "assistant", "content": None, "function_call": {"name": "get_current_weather", "arguments": "{ \"location\": \"Boston, MA\"}"}},
    {"role": "function", "name": "get_current_weather", "content": "{\"temperature\": \"22\", \"unit\": \"celsius\", \"description\": \"Sunny\"}"}
  ],
  "functions": [
    {
      "name": "get_current_weather",
      "description": "Get the current weather in a given location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA"
          },
          "unit": {
            "type": "string",
            "enum": ["celsius", "fahrenheit"]
          }
        },
        "required": ["location"]
      }
    }
  ]
}

response = requests.post(url, auth=auth, headers=headers, data=json.dumps(data))

print(response.text)

最后拿到的就是正确的资料,同时又是自然语言

代码语言:javascript
复制
{
  "id": "chatcmpl-123",
  ...
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "The weather in Boston is currently sunny with a temperature of 22 degrees Celsius.",
    },
    "finish_reason": "stop"
  }]
}

在了解完函式呼叫功能后,大概可以更理解OpenAI 的未来策略,如同执行长Sam Altman 先前提到的,不会是再去做ChatGPT 这种应用;而是会以平台的角度出发。函式呼叫能让外部工具更好与ChatGPT API 整合,这会让ChatGPT 这个平台变得更有价值。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-01-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 一个正经的测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 例子
  • API调用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档