4 Ways to Use ChatGPT API in Python

Deepanshu Bhalla Add Comment ,

In this tutorial, we will explain how to use ChatGPT API in Python, along with examples.

Steps to Access ChatGPT API

Please follow the steps below to access the ChatGPT API.

  1. Visit the OpenAI Platform and sign up using your Google, Microsoft or Apple account.
  2. After creating your account, the next step is to generate a secret API key to access the API. The API key looks like this -sk-xxxxxxxxxxxxxxxxxxxx
  3. If your phone number has not been associated with any other OpenAI account previously, you may get free credits to test the API. Otherwise you have to add atleast 5 dollars into your account and charges will be based on the usage and the type of model you use. Check out the pricing details in the OpenAI website.
  4. Now you can call the API using the code below.
Python Code to Access ChatGPT API

Step 1 : To install the OpenAI Python library, run this command : pip install openai

Step 2 : Enter your API key in os.environ["OPENAI_API_KEY"] = and input the question you would like to ask in the prompt argument in the code below.


import os
import openai

# Set API Key
os.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxx"
openai.api_key = os.getenv("OPENAI_API_KEY")

def chatGPT(prompt, 
            model="gpt-3.5-turbo",
            temperature = 0.7,
            top_p=1):
    error_message = ""
    try:    
        response = openai.ChatCompletion.create(
          model = model,
          messages = [{'role': 'user', 'content': prompt}],
          temperature = temperature,
          top_p = top_p
        )
        
    except Exception as e:
        error_message = str(e)

    if error_message:
        return "An error occurred: {}".format(error_message)
    else:
        return response['choices'][0]['message']['content']

# Run Function    
print(chatGPT("efficient way to remove duplicates in python. Less verbose response."))

In the model argument, you can enter either gpt-3.5-turbo, gpt-4-turbo or gpt-4, pointing to their respective latest model version.

Using ChatGPT API in Python
Custom Instructions for ChatGPT

Sometimes you want ChatGPT to consider some high-level instructions while providing responses. In simple words, what do you want ChatGPT to know about you to provide responses. For example, you want the model to "act like a linguistic expert and provide responses in layman's terms". You can input instructions in the system_instruction argument in the code below.


# System Message
def chatGPT(prompt, 
            system_instruction=None,
            model="gpt-3.5-turbo",
            temperature = 0.7,
            top_p=1):
    error_message = ""
    try:
        messages = [{'role': 'user', 'content': prompt}]
        if system_instruction:
            messages.insert(0, {'role': 'system', 'content': system_instruction})

        response = openai.ChatCompletion.create(
          model = model,
          messages = messages,
          temperature = temperature,
          top_p = top_p
        )
        
    except Exception as e:
        error_message = str(e)

    if error_message:
        return "An error occurred: {}".format(error_message)
    else:
        return response['choices'][0]['message']['content']

# Run Function    
print(chatGPT(prompt = "who are you and what do you do?",
              system_instruction = "You are Janie Jones, the CEO of Jones Auto."))

Using ChatGPT API with Custom Instructions in Python
How to Converse Like ChatGPT Website

The ChatGPT website remembers prior conversations while providing responses of the current question. For example : You ask '2+2', it answers '4'. Then you ask follow-up questions like "square of it", it returns '16'. So it remembers the prior response of '2+2'.

By default, the ChatGPT API does not remember prior conversations. However, we can make the ChatGPT API remember prior conversations by feeding it the previous questions and responses in each API request.


# ChatGPT Chat
import os   
import openai
os.environ['OPENAI_API_KEY'] = "sk-xxxxxxxxxxxxxxxxxxxxxxx"
openai.api_key = os.getenv("OPENAI_API_KEY")


chatHistory = []
def chatGPT_chat(prompt, modelName="gpt-3.5-turbo", temperature=0.7, top_p=1):
    params = {
        "model": modelName,
        "temperature": temperature,
        "top_p": top_p
    }

    chatHistory.append({"role": "user", "content": prompt})

    response = openai.ChatCompletion.create(
        **params,
        messages=chatHistory
    )

    answer = response["choices"][0]["message"]["content"].strip()
    chatHistory.append({"role": "assistant", "content": answer})

    return answer

chatGPT_chat("2*3")
# 6

chatGPT_chat("square of it")
# The square of 6 is 36.

chatGPT_chat("add 3 to it")
# Adding 3 to 36, we get 39.

How to Build Chatbot using ChatGPT API

In the following code, we are building an application using tkinter python library that creates a graphical user interface (GUI). It includes several widgets such as text boxes, buttons, dropdowns etc.
To install the tkinter library, run this command : pip install tkinter.


import os
import openai
import tkinter as tk

# Set API Key
os.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxx"
openai.api_key = os.getenv("OPENAI_API_KEY")

def chatGPT(prompt, 
            model="gpt-3.5-turbo",
            temperature = 0.7,
            top_p=1):
    error_message = ""
    try:    
        response = openai.ChatCompletion.create(
          model = model,
          messages = [{'role': 'user', 'content': prompt}],
          temperature = temperature,
          top_p = top_p
        )
        
    except Exception as e:
        error_message = str(e)

    if error_message:
        return "An error occurred: {}".format(error_message)
    else:
        return response['choices'][0]['message']['content']

# tikinter GUI Application
def chatgpt_output():
    input_text = input_field.get()
    model_name = dropdown_var.get()
    response = chatGPT(input_text, model_name)
    output_field.config(state='normal')
    output_field.delete(1.0, tk.END)
    output_field.insert(tk.END, response)
    output_field.config(state='disabled')

# Define the function to get the selected option
def on_select(value):
    print("Selected model:", value)

# Create the main window
root = tk.Tk()
root.title("ChatGPT")
root.geometry("600x700")

options = ["gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"]

# Create a label
label = tk.Label(root, text="Select Model:", font=("Arial", 12, "italic"))
label.pack()

# Create dropdown menu
dropdown_var = tk.StringVar(root)
dropdown_var.set(options[0])
dropdown_font = ('Arial', 12)
dropdown = tk.OptionMenu(root, dropdown_var, *options, command=on_select)
dropdown.config(font=dropdown_font)
dropdown.pack()

# Create a label for the input field
input_label = tk.Label(root, text="Enter Your Prompt:", font=("Arial", 14, "italic"))
input_label.pack(pady=5)

# Create input field
input_field = tk.Entry(root, font=("Arial", 14), width=50, bd=2, relief="groove", justify="left")
input_field.pack(pady=5, ipady=15)

# Create submit button
submit_button = tk.Button(root, text="Submit", font=("Arial", 14), command=chatgpt_output)
submit_button.pack(pady=10)

# Create output box
output_field = tk.Text(root, font=("Arial", 14), state='disabled')
output_field.pack(pady=10)

# Start
root.mainloop()

ChatGPT Bot in Python
How to Improve Chatbot's look?

The GUI developed using the tkinter library lacks fancy visuals. To build a more modern and stylish interface, use the Gradio package and refer to this article for the same - Building ChatGPT Clone in Python Using Gradio

Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "4 Ways to Use ChatGPT API in Python"
Next → ← Prev