How to use OpenAI's ChatGPT API

A. What is OpenAI and ChatGPT

OpenAI is an AI research and deployment company founded in December 2015. It is responsible for building the GPT-3 large language model (LLM) which is trained from a huge text dataset from internet. This model may generate "untruthful, toxic, or reflect harmful sentiments" - OpenAI. A new model called InstructGPT was created to address these shortcomings. This model is fine-tuned from GPT-3, to follow instructions using human feedback.

"ChatGPT is similar to InstructGPT but with slight differences in the data collection setup" - OpenAI. ChatGPT can generate text responses based from the text input from the users. It is optimized for chatbots application and communication interfaces. The model was trained using Reinforcement Learning with Human Feedback (RLHF). Based on ChatGPT release notes as of January 30 2023 from https://help.openai.com/en/articles/6825453-chatgpt-release-notes, the model has been improved on factuality and mathematical capabilities. Historically, ChatGPT is fined-tuned from OpenAI's GPT-3.5 language model. GPT stands for Generative Pre-trained Transformer.

1. Structure

image

2. Usage of GPT-3

According to OpenAI, you can build your own applications with GPT-3.5-turbo (the latest model so far) to do things such as:

  • Draft an email or other piece of writing

  • Write Python code

  • Answer questions about a set of documents

  • Create conversational agents

  • Give your software a natural language interface

  • Tutor in a range of subjects

  • Translate languages

  • Simulate characters for video games and much more

B. User Registration

The ChatGPT can only be used by a registered user. You can register at https://platform.openai.com/signup. After the registration you will receive 18.0 USD which you can use to explore OpenAI's services and model use. You are now in a category as Free Trial Users.

1. Login / signup

image

2. API Key Access

After the log in, press at the top right menu to access other menu items including the View API keys to generate secret api keys.

image

C. Create new key

The keys are used by OpenAI to authenticate their users.

image

D. ChatGPT Research Preview

It is a service provided by OpenAI that is free of charge. It has some limitations such as number of requests per minute and number of tokens per minute. This will allow users to explore the ChatGPT capabilties such as asking questions and get back responses. In return, OpenAI can further improve ChatGPT based from these questions and responses.

You can access the ChatGPT Research Preview from https://chat.openai.com/chat. You will be required to log in with your credentials.

1. Example dialogue

image

E. ChatGPT API

OpenAI had recently released ChatGPT API. API is an acronym for Application Programming Interface. It provides a means for the developers to integrate ChatGPT on their applications or even create a new application solely based from ChatGPT.

There are 2 ways to use the API in Python, first is by using the OpenAI python library and second is by using the OpenAI Endpoint.

1. OpenAI Python Library

Openai has a python package at https://pypi.org/project/openai/ for openai API, ChatGPT API and DALL.E API. I will create sample code on how to use it for ChatGPT.

main.py

import openai

openai.api_key = "your-api-key"
messages = [{"role": "user", "content": "Hello, how are you?"}]

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    temperature=1.0,
    top_p=1.0,
    n=1,
    presence_penalty=0.0,
    frequency_penalty=0.0,
    max_tokens=150,
    messages=messages
)
print(response)

Run the script with:

python main.py

output

{
  "choices": [
    {
      "finish_reason": null,
      "index": 0,
      "message": {
        "content": "\n\nAs an AI language model, I don't have feelings, but I'm functioning perfectly fine. Thank you for asking. How can I assist you?",
        "role": "assistant"
      }
    }
  ],
  "created": 1678283150,
  "id": "chatcmpl-6roP0H4URtFW8G4MQgYa7UunULCvT",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 32,
    "prompt_tokens": 11,
    "total_tokens": 43
  }
}

The output is like a python dictionary or a json format. We can get the content through the keys.

main.py

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

reply_content

As an AI language model, I do not have emotions in the human sense, but I am functioning well and ready to assist you with any inquiries you may have. How can I assist you today?

full code

import openai  # OpenAI Python v0.27.0

openai.api_key = "your api key"
messages = [{"role": "user", "content": "Hello, how are you?"}]

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    temperature=1.0,
    top_p=1.0,
    n=1,
    presence_penalty=0.0,
    frequency_penalty=0.0,
    max_tokens=150,
    messages=messages
)

print(response)

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

Chat Completion Parameters

model: ID of the model to use.  
message: The messages to generate chat completions for

See also the complete parameter definitions at https://platform.openai.com/docs/api-reference/chat.

2. OpenAI Endpoint

The second method does not need the OpenAI python package. But it needs a requests package to make a post request to the endpoint.

full code

"""
requirements.txt
    requests
"""

import requests
import json

# Set the API endpoint URL
url = "https://api.openai.com/v1/chat/completions"

# Set your API key
api_key = "your-api-key"

# Set the headers for the HTTP request
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
}

# Our message is a list of dictionaries.
messages = []

system_message = {"role": "system", "content": "Be creative and expressive."}
messages.append(system_message)

user_message = {"role": "user",
                "content": "Could you suggest a best approach to study react programming."}
messages.append(user_message)

# Set the payload data for the HTTP request
model = "gpt-3.5-turbo-0301"
data = {
    "model": model,
    "messages": messages,
    "temperature": 1.0,
    "top_p": 1.0,
    "n": 1,
    "stream": False,
    "presence_penalty": 0,
    "frequency_penalty": 0,
    "max_tokens": 256
}

# Send the HTTP request using the requests library
response = requests.post(url, headers=headers, data=json.dumps(data))

# Read request response.
if response.status_code == 200:
    json_response = response.json()
    reply_content = json_response["choices"][0]["message"]["content"]
    reply_usage = json_response["usage"]["total_tokens"]

    print("messages: " + str(messages))
    print("response: " + json.dumps(json_response, indent=4))
    print("tokens: " + str(reply_usage))
    print("content: " + reply_content)
else:
    print(f"Error: {response.json()['error']['message']}")

output

messages: [{'role': 'system', 'content': 'Be creative and expressive.'}, {'role': 'user', 'content': 'Could you suggest a best approach to study react programming.'}]
response: {
    "id": "chatcmpl-6uOlfMlkY7jfcce7XpqM4EpQG7gTS",
    "object": "chat.completion",
    "created": 1678899595,
    "model": "gpt-3.5-turbo-0301",
    "usage": {
        "prompt_tokens": 28,
        "completion_tokens": 256,
        "total_tokens": 284
    },
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "\n\nSure, I would suggest the following approach for studying React programming:\n\n1. Learn the fundamentals of JavaScript - React is built on top of JavaScript, so having a solid understanding of JavaScript is essential. You can refer to online tutorials, courses, and books to learn JavaScript.\n\n2. Learn the basics of React - Start with the official website of React which provides high-quality documentation, tutorials, and example code to learn the basics of React. You can also consult reliable online resources such as tutorialspoint, reactjs.org to learn React in-depth.\n\n3. Build simple projects - After learning the fundamentals of React, start building simple projects to experiment and implement your learnings. This helps in mastering the basics of React and gives hands-on experience in solving real-world problems.\n\n4. Follow best practices - As you start building complex projects, it's important to follow best practices such as using reusable components, modularizing code, designing a clean and scalable architecture, and writing clean and readable code.\n\n5. Keep practicing - Keep building projects and experimenting with different React features and concepts to hone your skills. Participate in online React communities, attend meetups, and ask questions to learn from experts in the field.\n\nRemember, 
the key to mastering React programming is to keep practicing and experimenting with"
            },
            "finish_reason": "length",
            "index": 0
        }
    ]
}
tokens: 284
content:

Sure, I would suggest the following approach for studying React programming:

1. Learn the fundamentals of JavaScript - React is built on top of JavaScript, so having a solid understanding of JavaScript is essential. You can refer to online tutorials, courses, and books to learn JavaScript.

2. Learn the basics of React - Start with the official website of React which provides high-quality documentation, tutorials, and example code to learn the basics of React. You can also consult reliable online resources such as tutorialspoint, reactjs.org to learn React in-depth.

3. Build simple projects - After learning the fundamentals of React, start building simple projects to experiment and implement your learnings. This helps in mastering the basics of React and gives hands-on experience in solving real-world problems.

4. Follow best practices - As you start building complex projects, it's important to follow best practices such as using reusable components, modularizing code, designing a clean and scalable architecture, and writing clean and readable code.

5. Keep practicing - Keep building projects and experimenting with different React features and concepts to hone your skills. Participate in online React communities, attend meetups, and ask questions to learn from experts in the field.

Remember, the key to mastering React programming is to keep practicing and experimenting

F. Summary

We got a basic knowledge of what OpenAI is and what the company has been developing. We also knew how to use the ChatGPT API to send our message or prompt and get back the model's response with the use of OpenAI's library package and Chat endpoint. This service is not free but OpenAI gives 18 usd so that users can explore ChatGPT API. This amount can also be used in exploring other models.

As of March 14, 2023, GPT-4 was released with enhanced capabilities such as Advanced reasoning, Complex instructions, and More creativity according to OpenAI.