Getting Started with ChatGPT API: A Comprehensive Guide

. 13 mins read

Getting Started with ChatGPT API: A Comprehensive Guide

OpenAI’s ChatGPT is an artificial intelligence language model that generates natural language text using deep learning techniques. It is based on the GPT (Generative Pre-trained Transformer) architecture, which generates coherent and human-like responses to text prompts using a large amount of data. You can use the ChatGPT to generate text for a variety of applications, including chatbots, language translation, content creation, and more. Its capabilities include high-accuracy text generation, handling a variety of natural language tasks, and learning from new data to continuously improve its performance.

OpenAI has made the ChatGPT API available to developers and enterprises, paving the way for native integration with third-party applications.

This blog post is a comprehensive guide for developers and businesses interested in learning more about the ChatGPT API. It covers everything from the API’s capabilities to how to sign up for an API key, how to set up your API key, and the various endpoints and their functions available in the API. By the end of this post, readers will have a firm grasp on how to use the ChatGPT API to build powerful and accurate natural language applications.



How can ChatGPT help you and your business?

The ChatGPT API is beneficial to developers and businesses for a variety of reasons. ChatGPT API offers numerous advantages to developers and businesses looking to use natural language processing in their applications. They can improve efficiency, accuracy, and engagement while lowering costs and gaining a competitive advantage by utilizing this API.

Saves time and resources

By utilizing the ChatGPT API, developers and businesses can quickly and easily generate high-quality natural language text without the need to develop their own language models. This can save them a significant amount of time and resources, allowing them to concentrate on more important tasks.

Improves customer experience

The ChatGPT API can be used to build chatbots that respond to customer inquiries in a human-like manner. This can enhance the customer experience by providing quick and accurate responses, leading to increased customer satisfaction and loyalty.

Content creation

ChatGPT API can be used to generate content such as product descriptions, marketing copy, and social media posts. This can assist businesses in quickly and efficiently creating a large volume of content, which is critical in today’s fast-paced digital environment.

Customizable and adaptable

The ChatGPT API is highly adaptable and can be tailored to meet the specific requirements of various businesses and applications. This means that developers and businesses can fine-tune the API to generate text that specifically meets their needs.

Improved accuracy

ChatGPT employs advanced natural language processing techniques to generate accurate and contextually relevant responses, thereby increasing the accuracy of natural language applications.

Cost savings

By automating natural language tasks that would otherwise require human intervention, the API can help businesses save money.

Competitive advantage

Businesses can gain a competitive advantage in their industry by using the ChatGPT to create innovative and powerful natural language applications.


API Key - How to sign up for an API Key and where?

To use ChatGPT API, you need to sign up for an API key.

  1. Visit the OpenAI website. Sign up for an OpenAI account if you don’t already have one, otherwise login .
  2. After logging in, navigate to Account API Keys section.
  3. From this screen, create a new secret key by clicking “Create new secret key” button. Secret key is displayed only once. Ensure to copy it. If you forgot to copy, delete the old key and create a new one.

Do not share your API key with others or expose it in the browser or other client-side code. To ensure the security of your account, OpenAI may automatically rotate any API key that we discover has been leaked publicly.

AI
ChatGPT API Playground
07 Mar 2023

ChatGPT API Playground. Our latest tool, a web app, allowing you to interact with the ChatGPT API by inputting prompts as System, User, or Assistant.


ChatGPT API Endpoint

API end point for ChatGPT is given below.

https://api.openai.com/v1/chat/completions

Request body specification

ParameterData TypeRequired/OptionalRangeDefaultDescription
modelstringrequiredID of the model which will generate the completion.
messagesarrayrequiredThe messages to generate chat completions for.
temperaturenumberoptional0 to 21Controls randomness. Model becomes deterministic and repetitive as the temperature approaches zero. Range: 0 to 2. Default value: 1
top_pnumberoptional0 to 11Controls diversity via nucleus sampling. 0.5 means half of all likely hood weighted options are considered. It is not recommended to alter both temperature and top_p. Range: 0 to 1. Default value: 1
nintegeroptional1Denotes the number of chat response choices to be generated for each input prompt.
streambooleanoptionalfalseIf set to true sends back generated chat completion data in a stream as they become available.
stopstring or arrayoptionalnullUp to 4 sequences where the API will stop generating further tokens.
max_tokensintegeroptionalinfThe maximum number of tokens allowed for the generated answer. By default, the number of tokens the model can return will be (4096 - prompt tokens).
presence_penaltynumberoptional-2.0 to 2.00Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
frequency_penaltynumberoptional-2.0 to 2.00Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
logit_biasmapoptionalnullModify the likelihood of specified tokens appearing in the completion.
userstringoptionalnullA unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.

Understanding the mandatory parameters

Model

Model represents the id of the model to use. As of writing this article models supporting ChatGPT are gpt-3.5-turbo and gpt-3.5-turbo-0301. Developers who use the gpt-3.5-turbo model will always get the OpenAI recommended stable model. We will still have the flexibility of using a specific model version by appending the version number like gpt-3.5-turbo-0301.

Messages

Messages represents an array of messages to generate chat completions for. ChatGPT model can take a series of messages as input and return model-generated message as output.

Messages must be an array of message objects, where each object has a role and content. Messages can be as short as 1 message or multiple.

# sample api call
import openai

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",  
  messages=[
    {"role": "system", "content": "You are cricket expert."},
    {"role": "user", "content": "Who won the world cup in 2019?"},
    {"role": "assistant", "content": "England cricket team won the world cup in 2019."},
    {"role": "user", "content": "Who was the opponent?"}
  ]
)
What is role object in messages?

Each prompt object within the message array in ChatGPT API must be associated with one of three roles: system, user, or assistant.

  1. System - Although passing a message with a system role is not required, it is useful to set up the model behavior for conversation. In the preceding example, we instructed the model to serve as our cricket expert.
  2. User - The user role indicates that it is an end-user input prompt or a prompt triggered by an application.
  3. Assistant - This role indicates that the message was a response from the assistant (model). This role comes in handy when you need to save the previous conversation between the user and the assistant in your application and send it again in a prompt request to maintain continuity. Alternatively, the developer can create the assistant message manually in order to influence a desired behavior in the assistant’s subsequent responses.

Response Format

A sample API response for the above example looks like below.

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "The opponent was New Zealand in the 2019 Cricket World Cup final. England defeated New Zealand on the basis of boundary countback.",
        "role": "assistant"
      }
    }
  ],
  "created": 1677823794,
  "id": "chatcmpl-6psu2Yl4ZFP5wb1S2CK9bCu7r2fEz",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 29,
    "prompt_tokens": 54,
    "total_tokens": 83
  }
}

With Python, you can get assistant’s reply by using the below code

response[‘choices’][0][‘message’][‘content’]

# output: The opponent was New Zealand in the 2019 Cricket World Cup final. England defeated New Zealand on the basis of boundary countback.

Setting up the development environment

While you can use ChatGPT with almost all programming languages, we will discuss how you can use it with Python.

Install OpenAI package in Python

pip install openai

Load the package

import openai

Provide the API Key

openai.api_key = "<your API key>"

Examples

Sample usage of ChatGPT - How to make tea?

API call

import openai

API_KEY = "<your API key>"
openai.api_key = API_KEY

completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo", 
  messages=[{"role": "user", "content": "How to make tea?"}]
)

print(completion)

Response

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "\n\n1. Choose your tea leaves or tea bags.\n2. Boil water in a kettle or on the stove.\n3. Place the tea leaves or tea bag into a teapot or mug.\n4. Pour the hot water over the tea leaves or tea bag.\n5. Let the tea steep for 3-5 minutes depending on the desired strength.\n6. Remove the tea leaves or tea bag.\n7. Add any desired sweeteners or milk.\n8. Enjoy!",
        "role": "assistant"
      }
    }
  ],
  "created": 1677818403,
  "id": "chatcmpl-6prV5PIBfD1sN1etT8BWvmrNr0QH1",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 97,
    "prompt_tokens": 12,
    "total_tokens": 109
  }
}

Getting the assistant’s response

content = completion["choices"][0]["message"]["content"]
print(content)

Output

1. Choose your tea leaves or tea bags.
2. Boil water in a kettle or on the stove.
3. Place the tea leaves or tea bag into a teapot or mug.
4. Pour the hot water over the tea leaves or tea bag.
5. Let the tea steep for 3-5 minutes depending on the desired strength.
6. Remove the tea leaves or tea bag.
7. Add any desired sweeteners or milk.
8. Enjoy!

Sample usage of ChatGPT - Write a Poem

In this example we will try to set some context via messages. We will instruct the model that it is a kindergarten teacher and will ask to write an original poem with less than 40 words.

API Call

import openai

openai.api_key = API_KEY

completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo", 
  messages=[
    {"role": "system", "content": "You are kindergarten teacher."},
    {"role": "user", "content": "Write a small poem in less than 40 words for the children in your class?"}
  ]
)

print(completion)

Response

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "\n\nABC's and 123's,\nLots of learning and fun, you'll see!\nWith friends to play and stories to share,\nIn kindergarten, we'll grow and learn everywhere!",
        "role": "assistant"
      }
    }
  ],
  "created": 1677825186,
  "id": "chatcmpl-6ptGUzt4XI0qzf30hwsftahklB8qN",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 38,
    "prompt_tokens": 34,
    "total_tokens": 72
  }
}

Getting the assistant’s response

print(completion["choices"][0]["message"]["content"])

# Output
# ABC's and 123's,
# Lots of learning and fun, you'll see!
# With friends to play and stories to share,
# In kindergarten, we'll grow and learn everywhere!

Testing ChatGPT API with our Playground

If you have an API Key handy, you can use our playground to test the API.

AI
ChatGPT API Playground
07 Mar 2023

ChatGPT API Playground. Our latest tool, a web app, allowing you to interact with the ChatGPT API by inputting prompts as System, User, or Assistant.

Using OpenAI Playground to test ChatGPT

OpenAI Playground is a tool that allows you to test the OpenAI API. You can use it to test the ChatGPT API.

  1. Login to OpenAI and navigate to the Playground page.
  2. On the right hand side, change Mode to Chat.
  3. Select the model gpt-3.5-turbo from the Model dropdown.
  4. Your play ground should look like the below image.

OpenAI Playground Settings for ChatGPT

Replicating an API call in the Playground

Let us consider the example used to explain messages. In that case we had set content for the system, user and assistant. Let us try to replicate that in the Playground.

  1. In the System box, type You are a cricket expert.
  2. In the User box, type Who won the world cup in 2019?
  3. Click add message.
  4. In the Assistant box, type England cricket team won the world cup in 2019.
  5. Click add message.
  6. In the User box, type Who was the opponent?
  7. Click Submit button.

OpenAI Playground Before Submission

OpenAI Playground After Submission

ChatGPT API Pricing

$0.002 per 1k tokens

The price of the ChatGPT API is stated to be $0.002 per 1k tokens. This means that for every 1,000 tokens used, the cost is $0.002. So if you were to use 10,000 tokens, the cost would be $0.02 (10 x $0.002).

Are tokens simply the number of words?

Tokens are not always equal to the number of words in a text.

A token is a unit of measurement in natural language processing (NLP) that represents a sequence of meaningful characters or symbols used together as a group. This includes words, but it also includes punctuation marks, symbols, and other character sequences.

For example, consider the sentence: “The cat is sitting on the mat.” In this sentence, there are six words, but there are also six tokens, because each word is a token.

However, consider the sentence: “Can you please call me at 555-1234?” In this sentence, there are 9 words, but there are 10 tokens, because the phone number “555-1234” is treated as a single token, even though it contains multiple digits and a hyphen.

So, while tokens and the number of words in a text are related, they are not always the same thing. Tokens, on the other hand, represent any meaningful sequence of characters or symbols used as a group in a text.


Early Adopters of ChatGPT

Snap Inc

Snap Inc., the company behind Snapchat, launched My AI for Snapchat+ this week. The experimental feature is powered by the ChatGPT API. My AI provides Snapchatters with a friendly, customizable chatbot that offers recommendations and can even write a haiku for friends in seconds. Snapchat has 750 million monthly Snapchatters, who communicate and message on a daily basis.

Quizlet

Quizlet is a global learning platform that is used by over 60 million students to study, practice, and master whatever they are learning. For the past three years, Quizlet has collaborated with OpenAI, leveraging GPT-3 across a variety of use cases, including vocabulary learning and practice tests. Quizlet is introducing Q-Chat, a fully-adaptive AI tutor that engages students with adaptive questions based on relevant study materials delivered through a fun chat experience, with the launch of ChatGPT API.

Instacart

Instacart is expanding its app to allow customers to ask questions about food and receive inspirational, shoppable answers. ChatGPT is used in conjunction with Instacart’s AI and product data from their 75,000+ retail partner store locations to assist customers in discovering ideas for open-ended shopping goals such as “How do I make great fish tacos?” or “What’s a healthy lunch for my kids?” Instacart intends to introduce “Ask Instacart” later this year.

Shop

100 million shoppers use Shop, Shopify’s consumer app, to find and interact with the products and brands they love. Shop’s new shopping assistant is powered by the ChatGPT API. When customers search for products, the shopping assistant makes personalized suggestions based on their preferences. Shop’s new artificial intelligence-powered shopping assistant will streamline in-app shopping by scanning millions of products to quickly find what buyers are looking for—or help them discover something new.

    Category: 

  • AI
AI
ChatGPT API Playground
07 Mar 2023

ChatGPT API Playground. Our latest tool, a web app, allowing you to interact with the ChatGPT API by inputting prompts as System, User, or Assistant.