How to Use the ChatGPT API

With the release of its API, OpenAI has opened up the capabilities of ChatGPT to everyone. you may now seamlessly integrate ChatGPT’s features into your application.

Follow these steps to get started, whether you’re looking to integrate ChatGPT into your existing application or develop new applications with it.

4

1. Getting an OpenAI API Key

To start using the ChatGPT API, you need to obtain an API key.

The code used in this project is available in aGitHub repositoryand is free for you to use under the MIT license.

chatgpt openai logo on green background feature

2. Setting Up the Development Environment

You can use the API endpoint directly or take advantage of theopenaiPython/JavaScript library to start building ChatGPT API-powered applications. This guide uses Python and theopenai-pythonlibrary.

To get started:

OpenAI API Key Page

Make sure you do not accidentally share your API key via version control.Add a .gitignore fileto your project’s root directory and add “.env” to it to ignore the dotenv file.

3. Making ChatGPT API Requests

The OpenAI API’s GPT-3.5 Turbo, GPT-4, and GPT-4 Turbo are the same models that ChatGPT uses. These powerful models are capable of understanding and generating natural language text and code. GPT-4 Turbo can even process image inputs which opens the gates for several uses including analyzing images, parsing documents with figures, and transcribing text from images.

Please note that the ChatGPT API is a general term that refers to OpenAI APIs that use GPT-based models, including thegpt-3.5-turbo,gpt-4, andgpt-4-turbomodels.

ChatGPT API output response format

The ChatGPT API is primarily optimized for chat but it also works well for text completion tasks. Whether you want to generate code, translate languages, or draft documents, this API can handle it all.

To get access to the GPT-4 API, you need to make a successful payment of $1 or more. Otherwise, you might get an error similar to “The model gpt-4 does not exist or you do not have access to it.”

Programming joke output generated via Chat Completions API

Using the API for Chat Completion

You need to configure the chat model to get it ready for an API call. Here’s an example:

The ChatGPT API sends a response in the following format:

You can extract the content from the response, as a JSON string, with this code:

Running this code produces the following output:

The code demonstrates a ChatGPT API call using Python. Note that the model understood the context (“dad joke”) and the type of response (Q&A form) that we were expecting, based on the prompts fed to it.

The most significant part of the configuration is themessagesparameter which accepts an array of message objects. Each message object contains aroleandcontent. You can use three types of roles:

you could further customize thetemperatureandmax_tokensparameters of the model to get the output according to your requirements.

The higher the temperature, the higher the randomness of the output, and vice-versa. If you want your responses to be more focused and deterministic, go for the lower temperature value. And if you want it to be more creative, use a higher value. The temperature value ranges between 0 and 2.

Like ChatGPT, the API also has a word limit. Use themax_tokensparameter to limit the length of responses. Be aware that setting a lowermax_tokensvalue can cause issues as it may cut off the output mid-way.

At the time of writing, thegpt-3.5-turbomodel has a token limit of 4,096, whilegpt-4’s is 8,192. The latestgpt-3.5-turbo-0125andgpt-4-turbo-previewmodels have limits of 16,385 and 128,000 respectively.

After high demand from developers, OpenAI has introduced JSON mode which instructs the model to always return a JSON object. it’s possible to enable JSON mode by settingresponse_formatto{ “type”: “json_object” }. Currently, this feature is only available to the latest models: gpt-3.5-turbo-0125 and gpt-4-turbo-preview.

You can further configure the model using the other parameters provided byOpenAI.

Using the ChatGPT API for Text Completion

In addition to multi-turn conversation tasks, the Chat Completions API (ChatGPT API) does a good job with text completion. The following example demonstrates how you can configure the ChatGPT API for text completion:

You don’t even need to provide the system role and its content. Providing just the user prompt will do the work for you.

Running the above code will generate a poem for programmers, for example:

ChatGPT API Pricing

The ChatGPT API pricing is based on the “price per 1,000 tokens” model. For chat completion requests, the cost is calculated based on the number of input tokens plus the number of output tokens returned by the API. In layman’s terms, tokens are equivalent to pieces of words, where 1,000 tokens are approximately equal to 750 words.

gpt-4-0125-preview

$0.01 / 1K tokens

$0.03 / 1K tokens

gpt-4-1106-preview

gpt-4-1106-vision-preview

$0.06 / 1K tokens

$0.12 / 1K tokens

gpt-3.5-turbo-0125

$0.0005 / 1K tokens

$0.0015 / 1K tokens

gpt-3.5-turbo-instruct

$0.0020 / 1K tokens

Note that the pricing may change over time with improvements in the model.

Build Next Generation Apps Using the ChatGPT API

The ChatGPT API has opened gates for developers around the world to build innovative products with the power of AI.

you could use this tool to develop applications like story writers, code translators, marketing copy generators, and text summarizers. Your imagination is the limit to building applications using this technology.

Discover the power of OpenAI’s API and all the possibilities it enables.

These plugins will make you wonder why you used Photoshop in the first place.

So much time invested, and for what?

Anyone with more than a passing interest in motorsports must see these films.

These are the best free movies I found on Tubi, but there are heaps more for you to search through.

You’ve been quoting these famous films wrong all along!

Technology Explained

PC & Mobile