ChatGPT API Beginner’s Guide – Mantium

ChatGPT API Beginner’s Guide

March 3, 2023   ·   7 min read

This article will discuss important concepts on using the new ChatGPT API to build applications and how you can quickly get started.

Introduction

OpenAI released the ChatGPT model and chat playground in November 2022, taking the world by storm. People from different backgrounds – both technical and non-technical – experimented with the model to complete tasks such as content generation, summarization, question answering, conversational AI, language translation, etc. The model was developed by fine-tuning GPT-3.5 using supervised learning and Reinforcement Learning from Human Feedback (RLHF).

Now, OpenAI just announced the release of ChatGPT’s API, allowing developers easy access to building demos and applications with the model. They also released the Whisper API for speech-to-text capabilities; we will cover this in our next article. This article will discuss important concepts on using the new ChatGPT API to build applications and how you can quickly get started.

GPT – 3.5 Models

Let’s understand the models within the GPT-3.5 family which powers the ChatGPT API. The Turbo models consist of the gpt-3.5-turbo and gpt-3.5-turbo-0301. They are designed to excel in conversational chat input and output. Compared to the Davinci model family, they perform equally well in completions.

The advantage of the Turbo model family is that, like ChatGPT, it receives regular model updates.

It means that the Turbo model family is always up-to-date, ensuring its performance continually improves, making it an even more reliable and valuable tool. Also, the price per token is 10% of the price per token of the Davinci models.

OpenAI recommends using gpt-3.5-turbo, as it will yield the best results, and the gpt-3.5-turbo-0301, will be supported for three months ending on June 1st, 2023. In this tutorial, we will explain how to work with both models.

Prompting – Interacting with the model

In using ChatGPT API, instead of prompts, we interact using chat messages. The primary input is the “messages” parameter, which is required to be an array of message objects. Each message object must contain two properties, namely role, either system, user, or assistant, and content, which includes the actual message content. It’s essential to note that the conversation can range from a single message to multiple pages, so the number of messages in the array is not limited. As a result, the model can easily handle short and lengthy conversations, making it an ideal choice for various use cases.

There are two methods to make API calls to the ChatGPT API;

  1. Python Client Library
  2. Making HTTP requests.

We are going to use the Python client library.

Example

Let’s start with this example, where we ask the model to generate text.

Installation & Setup

%pip install --upgrade openai

Interacting with ChatGPT API using the Python client library

import openai
openai_api_key = "sk-key"

Messages

MODEL = "gpt-3.5-turbo"
system_content = "You are a kind helpful AI Knowledge assistant."
user_content = "Tell me about ChatGPT"
completion = openai.ChatCompletion.create(
  model=MODEL,
  messages=[
        {"role": "system", "content": system_content},
        {"role": "user", "content": user_content}
    ],
  temperature=0
)
print(completion['choices'][0]['message']['content'])

"""
ChatGPT is an AI-powered chatbot that provides 
information and assistance on a wide range of topics. 
It is designed to help users find answers to their questions and 
provide guidance on various subjects. ChatGPT uses natural language 
processing and machine learning algorithms to understand user queries 
and provide relevant responses. It can assist with tasks such as finding 
information, making recommendations, and providing support. 
ChatGPT is available 24/7 and can be accessed through various platforms, 
including websites, messaging apps, and social media platforms.
"""

From the code above, we have the system and user messages, let’s examine them.

The conversation above started with the system message to set the behavior of the assistant, and we instructed the assistant to be a helpful “AI Knowledge assistant”. The user message serves as instructions for the assistant, which is the user-provided content, and in this case, we asked the model to us about “ChatGPT”.

OpenAI recommends that conversations should begin with a system message, followed by user and assistant messages that alternate with each other. You are not required to follow this format. You can test the example above without using the system message.

Few-Shot Prompting

Sometimes, it is best to provide multiple examples of the intended task, a way to show the model what you want rather than telling the model what want. At the moment, OpenAI’s team recommends using few-shot prompting when using the gpt-3.5-turbo-0301 model.

Let’s see how to achieve few-shot prompting with the following example;

new_user_content = "How can I get cheap flights?"

response = openai.ChatCompletion.create(
    model=MODEL,
    messages=[
        {"role": "system", "content": "You are a helpful, travel assistant that answers concisely travel questions"},
        {"role": "system", "name":"example_user", "content": " How should I pay for things during my trip to Europe?"},
        {"role": "system", "name": "example_assistant", "content": "Have some euros on hand when you arrive, use a debit card at bank ATMs to avoid fees, and pay for larger expenses with a credit card. Inform your credit card issuers of travel plans and check if foreign transaction fees apply. Keep cash on hand for small businesses that may not take credit cards. European credit cards use a chip-and-PIN system, so be prepared with cash for transit kiosks without human attendants."},
        {"role": "system", "name":"example_user", "content": "How do I choose the right escorted tour for my first trip to Europe?"},
        {"role": "system", "name": "example_assistant", "content": "Consider your budget, trip length, destination, style, age group, and what's included in the price before choosing. Research organizations like the United States Tour Operators Association and National Tour Association to identify operators. Some popular companies include Collette Vacations, Cosmos, Gate 1 Travel, Globus, Great Value Vacations, Ritz Tours, SmarTours, Trafalgar, and Tauck World Discovery. Special interest tours and university-affiliated trips can also offer value."},
        {"role": "user", "content": new_user_content},
    ],
    temperature=0,
)

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

In the example, we provided system content that sets the behavior of a travel assistant that generates concise answers to travel questions. Also, we provided example_user and example_assistant examples as few-shots examples of questions and travel answers for the model.

The new_user_content is the user input to the model. Below is the response that the model returned;

Here are some tips to get cheap flights:

1. Be flexible with your travel dates and times.
2. Use flight search engines like Google Flights, Skyscanner, and Kayak to compare prices.
3. Sign up for email alerts from airlines and travel websites to get notified of sales and promotions.
4. Book your flight in advance, but not too far in advance.
5. Consider alternative airports or nearby cities.
6. Fly on weekdays instead of weekends.
7. Avoid peak travel seasons and holidays.
8. Use airline miles or credit card rewards to pay for flights.
9. Check for student discounts, military discounts, and senior discounts.
10. Be willing to make layovers or take connecting flights.

Pricing

OpenAI determines pricing based on the total number of tokens used in an API call, including both input and output tokens. The more tokens used, the higher the cost and longer the call takes. The model’s maximum limit for tokens is 4096 for gpt-3.5-turbo and gpt-3.5-turbo-0301, so conversations exceeding this limit must be truncated, shorted, or split into multiple messages. Additionally, more extended conversations are more likely to receive incomplete replies, so keeping them within the token limit is essential.

Let’s check the number of tokens used in the example above. Run the following command ;

response["usage"]

Output

# Output
"""
{
  "completion_tokens": 148,
  "prompt_tokens": 272,
  "total_tokens": 420
} 
"""

The Turbo family model is priced at $0.002 per 1k tokens, which is 10x cheaper than the Davinci models. The cost for the above is represented as shown below;

420/1000 * $0.002  = $0.00084

Conclusion

This article explained how to use the new OpenAI’s ChatGPT API. We should expect some updates on how to interact with the models as time goes on. In the meantime, you can get started building applications following the concepts discussed in this article.

Getting data to build applications is still a big challenge, and manipulating external knowledge is still limited for LLMs such as the ChatGPT model family. It is why at Mantium, we are working on the fastest way to achieve “Step One” in the AI pipeline. To learn how Mantium can help, visit our website.

References

  1. OpenAI Chat Documentation
  2. Introducing ChatGPT Blog
  3. Introducing ChatGPT and Whisper APIs Blog

Enjoy what you're reading?

Subscribe to our blog to keep up on the latest news, releases, thought leadership, and more.