关于chatgpt:ChatGPT-API重大升级-必知必会

4次阅读

共计 4216 个字符,预计需要花费 11 分钟才能阅读完成。

背景

OpenAI 官网在 2023.06.13 公布了 API 层面的重磅降级,次要变动如下:

  • 在 Chat Completions 这个 API 里反对了开发者自定义的函数调用。
  • 更新了 gpt-4gpt-3.5-turbo模型。
  • gpt-3.5-turbo反对的上下文长度扩容到 16K,之前只反对 4K 个 token。
  • embedding model 的应用老本升高 75%。
  • gpt-3.5-turbo模型的 input token 的老本升高 25%,从原来的 0.002 美金 / 1K token 升高为 0.0015 美金 / 1K token。
  • 2023.09.13 会下线 gpt-3.5-turbo-0301gpt-4-0314gpt-4-32k-0314 模型,过了这个工夫点调用这些模型会申请失败。

下面提到的这些模型都严格遵循 2023.03.01 公布的隐衷和平安规定,用户通过 API 发送的数据和 API 返回的数据不会用于 OpenAI 大模型的训练。

函数调用示例

场景:咱们心愿 ChatGPT 通知当初 Boston 的天气状况。

如果只靠 ChatGPT 是无奈实现这个性能的,因为 ChatGPT 的训练数据只截止到 2021 年 9 月,无奈晓得当初的天气。这也是 GPT 目前最大的一个问题,不能很好地反对信息的及时更新。

那应该怎么应用 ChatGPT 来实现这个性能呢?

咱们能够本人定义一个函数来获取当天某个城市的天气状况,ChatGPT 只须要依据用户的发问生成咱们自定义的函数的参数值(也叫实参),那咱们就能够调用自定义函数拿到咱们想要的后果,而后把自定义函数生成的后果和对话记录作为 Prompt 送给 ChatGPT,由 ChatGPT 做一个汇总,最初把汇总的论断返回给用户即可。

用户发问 -> ChatGPT 生成函数的实参 -> 开发者调用自定义函数 -> 把函数执行后果 + 上下文对话记录发送给 ChatGPT 做汇总 -> 返回汇总论断给用户

上面咱们来看一个具体的实现案例:

  • 第一步:发问What’s the weather like in Boston right now?
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"]
      }
    }
  ]
}'

拿到 ChatGPT 返回的后果,返回后果里 content 为 null,function_call 有值,示意须要调用自定义函数get_current_weather,并且返回了自定义函数的参数值。

{
  "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"
  }]
}
  • 第二步:调用自定义函数。
curl https://weatherapi.com/...

拿到自定义函数返回后果

{"temperature": 22, "unit": "celsius", "description": "Sunny"}
  • 第三步:把自定义函数执行后果和上下文对话记录发送给 ChatGPT 做汇总。
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"]
      }
    }
  ]
}'

最初 ChatGPT 返回如下后果:

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

咱们输入后果:

The weather in Boston is currently sunny with a temperature of 22 degrees Celsius.

以上性能实现的外围因素是 ChatGPT 能够智能地依据用户的输出来判断什么时候应该要调用开发者的自定义函数,并且把自定义函数的参数值给返回。开发者就能够间接本人去调用自定义函数拿到想要的后果,最初再把对话记录和自定义函数执行后果发送给大模型去做汇总。

目前这个性能能够在 gpt-4-0613gpt-3.5-turbo-0613这 2 个模型里应用。

等 OpenAI 在 2023.06.27 实现模型降级后,gpt-4gpt-4-32kgpt-3.5-turbo 模型也能够应用这个性能。

新 API 的应用详情能够参考:developer documentation。

新模型

GPT- 4 模型

gpt-4-0613 绝对于gpt-4,新增了函数调用的反对。

gpt-4-32k-0613 绝对于gpt-4-32k,同样是新增了函数调用的反对。

在接下来的几周里,OpenAI 会把 GPT-4 API waiting list 上的申请都尽量审批通过,让开发者能够享受到 GPT- 4 的弱小能力。还没申请的连忙去申请吧。

GPT-3.5 Turbo 模型

gpt-3.5-turbo-0613 绝对于gpt-3.5-turbo,新增了函数调用的反对。

gpt-3.5-turbo-16k 反对的上下文长度扩容到了 16K,是 gpt-3.5-turbo 的 4 倍,费用是 gpt-3.5-turbo 的 2 倍。具体费用是每 1K input token 须要 0.003 美金,每 1K output token 须要 0.004 美金。

旧模型下线工夫

从 2023.06.13 开始,OpenAI 会开始降级生产环境的 gpt-4gpt-4-32kgpt-3.5-turbo模型到最新版本,预计 2023.06.27 开始就能够应用到降级后的模型了。

如果开发者不想降级,能够持续应用旧版本的模型,不过须要在 model 参数里指定用
gpt-3.5-turbo-0301gpt-4-0314gpt-4-32k-0314

这些旧版本的模型在 2023.09.13 会下线,后续持续调用会申请失败。

更低价格

Embedding 模型

text-embedding-ada-002目前是 OpenAI 所有 embedding 模型里最受欢迎的。

当初应用这个 embedding 模型的老本升高为 0.0001 美金 /1K token,老本降落 75%。

GPT-3.5 Turbo 模型

gpt-3.5-turbo 模型在免费的时候,既对用户发送的问题 (input token) 免费,也对 API 返回的后果 (output token) 免费。

当初该模型的 input token 老本升高 25%,每 1K input token 的费用为 0.0015 美金。

output token 的费用放弃不变,还是 0.002 美金 /1K token。

gpt-3.5-turbo-16k 模型的 input token 免费是 0.003 美金 /1K token,output token 免费是 0.004 美金 /1K token。

总结

文章和示例代码开源在 GitHub: GPT 实战教程,能够看到所有支流的开源 LLM。

公众号:coding 进阶。关注公众号能够获取最新 GPT 实战内容。

集体网站:Jincheng’s Blog。

知乎:无忌。

References

  • https://openai.com/blog/function-calling-and-other-api-updates
正文完
 0