OpenAI 发布 ChatGPT 函数调用和 API 更新

329 天前
 Ansen678

2023 年 6 月 13 日,OpenAI 针对开发者调用的 API 做了重大更新,包括更易操控的 API 模型、函数调用功能、更长的上下文和更低的价格。

在今年早些时候发布 gpt-3.5-turbo ,gpt-4 在短短几个月内,已经看到开发人员在这些模型之上构建了令人难以置信的应用程序。 今天,我们将跟进一些令人兴奋的更新:

所有这些模型都具有我们在 3 月 1 日推出的相同的数据隐私和安全保证——客户拥有根据他们的请求生成的所有输出,他们的 API 数据不会用于训练。

模型更新相关

GPT-4

GPT-3.5 Turbo

模型弃用

今天,我们将开始对我们在三月份宣布的 gpt-4 和 gpt-3.5-turbo 的初始版本进行升级和弃用。使用稳定模型名称( gpt-3.5-turbo 、gpt-4 和 gpt-4-32k )的应用将在 6 月 27 日自动升级到上述新模型。为了比较版本之间的模型性能,我们的 Evals 库支持公开和私有评估,以显示模型变化对您的用例的影响。

需要更多时间进行过渡的开发者可以通过在 API 请求的 'model' 参数中指定 gpt-3.5-turbo-0301 、gpt-4-0314 或 gpt-4-32k-0314 ,继续使用旧模型。这些旧模型将一直可以使用到 9 月 13 日,之后指定这些模型名称的请求将会失败。您可以通过我们的模型弃用页面来跟踪模型弃用的最新信息。这是对这些模型的第一次更新,因此,我们非常欢迎开发者提供反馈,以帮助我们确保平稳过渡。

函数调用

gpt-4-0613 跟 gpt-3.5-turbo-0613 模型支持函数调用,让模型智能地选择输出包含参数的 JSON 对象来调用这些函数。这是一种更可靠地将 GPT 功能与外部工具和 API 连接的新方法。

这些模型已经过微调,可以检测何时需要调用函数(取决于用户的输入)并使用符合函数签名的 JSON 进行响应。函数调用允许开发人员更可靠地从模型中获取结构化数据。例如,开发人员可以:

创建通过调用外部工具(例如 ChatGPT 插件)来回答问题的聊天机器人

send_email(to: string, body: string)
get_current_weather(location: string, unit: 'celsius' | 'fahrenheit')

从文本中提取结构化数据

定义一个名为 的函数 extract_people_data(people: [{name: string, birthday: string, location: string}]),以提取维基百科文章中提到的所有人。

函数调用示例

调用 OpenAI 的 API 时,需要增加 functions 参数,这个参数是 json 格式字符串。

curl https://api.openai.com/v1/chat/completions -u :$OPENAI_API_KEY -H 'Content-Type: application/json' -d '{
  "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"]
      }
    }
  ]
}'

OpenAI 会返回一个 Json 字符串:

{
  "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

curl https://weatherapi.com/...

响应:

{ "temperature": 22, "unit": "celsius", "description": "Sunny" }

将响应结果发送给 OpenAI 进行总结

这个时候我们看到 messages 中的数组元素变多了,增加了上下文,并且其中有 role=function 的对象,在 content 中带上了上一步 api 返回的结果。

curl https://api.openai.com/v1/chat/completions -u :$OPENAI_API_KEY -H 'Content-Type: application/json' -d '{
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {"role": "user", "content": "What is the weather like in Boston?"},
    {"role": "assistant", "content": null, "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"]
      }
    }
  ]
}'

OpenAI 将响应发送回模型进行总结,返回了一句完整的话:波士顿目前天气晴朗,气温为 22 摄氏度。

{
  "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"
  }]
}

函数调用相关开发文档:

函数如何调用

https://platform.openai.com/docs/guides/gpt/function-calling

了解如何在简单和高级用例中通过 API 使用函数调用

https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb

函数调用总结

自 ChatGPT 插件的 alpha 版本发布以来,我们学到了很多关于如何让工具和语言模型安全地协同工作的知识。然而,仍然存在开放的研究问题。例如,概念验证利用说明了来自工具输出的不受信任的数据如何指示模型执行意外操作。我们正在努力减轻这些和其他风险。开发人员可以通过仅使用来自可信工具的信息并在执行具有现实世界影响的操作(例如发送电子邮件、在线发布或进行购买)之前包括用户确认步骤来保护他们的应用程序。

函数调用其实跟网页版的插件功能差不多,就是让 api 调用有更多的扩展性,但是目前感觉调用还是比较麻烦的,查询一个天气功能要请求三次。

更低的价格

我们将继续提高我们的系统效率,并将节省下来的资金转嫁给开发人员,即日起生效。

嵌入

text-embedding-ada-002 是我们最受欢迎的嵌入模型。今天,我们将成本降低 75% 至每 1K 代币 0.0001 美元。

GPT-3.5 Turbo

开发者反馈是我们平台发展的基石,我们将继续根据我们听到的建议进行改进。我们很高兴看到开发人员如何在他们的应用程序中使用这些最新模型和新功能。

总结

以上就是 OpenAI 2023 年 6 月 13 日发布的更新内容,大家也可以去看原文:

https://openai.com/blog/function-calling-and-other-api-updates?ref=upstract.com

公众号:楚少 AI

ChatGPT 手机版: https://ai004.com/

1738 次点击
所在节点    OpenAI
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/948628

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX