ChatGPT API major upgrade- must know will know

background

OpenAI officially released a major upgrade at the API level on 2023.06.13. The main changes are as follows:

  • The Chat Completions API supports developer-defined function calls.
  • Updated gpt-4and gpt-3.5-turbomodels.
  • gpt-3.5-turboThe supported context length is expanded to 16K, and only 4K tokens were supported before.
  • The cost of using the embedding model is reduced by 75%.
  • gpt-3.5-turboThe cost of the input token of the model is reduced by 25%, from the original $0.002/1K token to $0.0015/1K token.
  • 2023.09.13 will be offline gpt-3.5-turbo-0301, gpt-4-0314and gpt-4-32k-0314models, after this point in time, calling these models will fail the request.

The models mentioned above strictly follow the privacy and security regulations released on March 1, 2023. The data sent by users through the API and the data returned by the API will not be used for the training of OpenAI large models.

function call example

Scenario: We want ChatGPT to tell the current weather conditions in Boston.

This function cannot be realized by relying on ChatGPT alone, because the training data of ChatGPT is only up to September 2021, and the current weather cannot be known. This is also the biggest problem of GPT at present, and it cannot well support the timely update of information.

So how should ChatGPT be used to achieve this function?

We can define a function by ourselves to obtain the weather conditions of a certain city on that day. ChatGPT only needs to generate the parameter value (also called the actual parameter) of our custom function according to the user's question, then we can call the custom function to get our The desired result, and then send the result generated by the custom function and the dialogue record as a prompt to ChatGPT, and ChatGPT will make a summary, and finally return the summary conclusion to the user.

User asks a question -> ChatGPT generates the actual parameters of the function -> the developer calls the custom function -> sends the function execution result + context dialogue record to ChatGPT for summary -> returns the summary conclusion to the user

Let's look at a specific implementation case:

  • Step One: Ask QuestionsWhat’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"]
      }
    }
  ]
}'

Get the result returned by ChatGPT. In the returned result, the content is null, and function_call has a value, which means that the custom function needs to be called get_current_weather, and the parameter value of the custom function is returned.

{
    
    
  "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"
  }]
}
  • Step 2: Call the custom function.
curl https://weatherapi.com/...

Get the custom function to return the result

{
    
     "temperature": 22, "unit": "celsius", "description": "Sunny" }
  • Step 3: Send the custom function execution results and context dialogue records to ChatGPT for summary.
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"]
      }
    }
  ]
}'

Finally, ChatGPT returns the following results:

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

We output the result:

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

The core element of the realization of the above functions is that ChatGPT can intelligently judge when to call the developer's custom function according to the user's input, and return the parameter value of the custom function. The developer can directly call the custom function to get the desired result, and finally send the dialogue record and the execution result of the custom function to the large model for summary.

Currently this function can be used in gpt-4-0613and gpt-3.5-turbo-0613these two models.

After OpenAI completes the model upgrade on 2023.06.27, the gpt-4, gpt-4-32kand gpt-3.5-turbomodels can also use this function.

For details about the use of the new API, please refer to: developer documentation .

new model

GPT-4 model

gpt-4-0613In contrast gpt-4, support for function calls has been added.

gpt-4-32k-0613In contrast gpt-4-32k, support for function calls is also added.

In the next few weeks, OpenAI will try its best to approve the applications on the GPT-4 API waiting list, so that developers can enjoy the powerful capabilities of GPT-4. If you haven't applied yet, apply now.

GPT-3.5 Turbo model

gpt-3.5-turbo-0613In contrast gpt-3.5-turbo, support for function calls has been added.

gpt-3.5-turbo-16kThe supported context length has been expanded to 16K, which is gpt-3.5-turbo4 times the cost and gpt-3.5-turbo2 times the cost. The specific fee is $0.003 per 1K input token and $0.004 per 1K output token.

Old model offline time

Starting from 2023.06.13, OpenAI will start to upgrade the production environment gpt-4and models to the latest version. It is expected that the upgraded model will be available starting from 2023.06.27.gpt-4-32kgpt-3.5-turbo

If the developer does not want to upgrade, he can continue to use the old version of the model, but it needs to be specified in the model parameter
gpt-3.5-turbo-0301, gpt-4-0314or gpt-4-32k-0314.

These old versions of the models will be offline on 2023.09.13, and subsequent calls will fail.

lower price

Embedding model

text-embedding-ada-002It is currently the most popular of all embedding models in OpenAI.

Now the cost of using this embedding model is reduced to $0.0001/1K token, and the cost is reduced by 75%.

GPT-3.5 Turbo model

gpt-3.5-turboWhen the model charges, it not only charges for the questions sent by the user (input token), but also charges for the results returned by the API (output token).

Now the model’s input token cost is reduced by 25%, and the cost per 1K input token is $0.0015.

The fee for the output token remains unchanged at $0.002/1K token.

gpt-3.5-turbo-16kThe input token fee of the model is 0.003 USD/1K token, and the output token fee is 0.004 USD/1K token.

Summarize

Articles and sample codes are open source on GitHub: GPT Practical Tutorial , where you can see all mainstream open source LLMs.

Official account: advanced coding. Follow the official account to get the latest GPT combat content.

Personal website: Jincheng's Blog .

Zhihu: Wuji .

References

  • https://openai.com/blog/function-calling-and-other-api-updates

Guess you like

Origin blog.csdn.net/perfumekristy/article/details/131445027