Python – How to use the ChatGPT API in 4 easy steps

Step 1 – Generate a bearer token

Login and then create an application from the My account menu.

Generate a bearer token from your new application.

Step 2 – Start a new thread with ChatGPT

You may select an ai_version_code. For example : gpt-4.

Send an http POST request to create a new thread.

import requests

bearer = '<<your_token_here>>'

url = "https://app.ai-client.com/api/v1/threads"

payload={'ai_version_code': 'gpt-4'}

headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer ' + bearer
}

response = requests.request("POST", url, headers=headers, data=payload)

thread = response.json()['data']

print(thread)

This will return :

{
   "guid":"946a5d77-ab48-4169-be1e-1604a754f848",
   "ai_code":"chatgpt",
   "ai_version_code":"gpt-4",
   "title":"New thread with gpt-4"
}

Step 3 – Create a thread entry to send a message

You may use the thread object to create a new entry with a POST request.

url = "https://app.ai-client.com/api/v1/threads/" + thread['guid'] + "/entry"

payload={'type': 'request', 'message' : 'Hello ChatGPT. Tell me a joke!'}

headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer ' + bearer
}

response = requests.request("POST", url, headers=headers, data=payload)

thread_entry = response.json()['data']

print(thread_entry)

This will return :

{
   "guid":"ec622c31-3b5f-4601-afea-c5275f9ffbfb",
   "thread_guid":"946a5d77-ab48-4169-be1e-1604a754f848",
   "type":"request",
   "error":"None",
   "content":{
      "role":"user",
      "content":"Hello ChatGPT. Tell me a joke!"
   },
   "waiting_response":true,
   "credits":0
}

Step 4 – Wait for a response from ChatGPT

You must now wait for the AI’s response. The “waiting_response” property of your thread entry informs you of ChatGPT’s response.

You may use the thread object to get all the thread entries. Send a GET request. You will see your thread entry of type “request” and the ChatGPT thread entry of type “response“.

url = "https://app.ai-client.com/api/v1/threads/" + thread['guid'] + "/entries"

headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer ' + bearer
}

response = requests.request("GET", url, headers=headers)

thread_entries = response.json()['data']

print(thread_entries)

This will return :

[
   {
      "guid":"ec622c31-3b5f-4601-afea-c5275f9ffbfb",
      "thread_guid":"e045a011-c295-4861-8cd1-8210b55443bd",
      "type":"request",
      "error":"None",
      "content":{
         "role":"user",
         "content":"Hello ChatGPT. Tell me a joke!"
      },
      "waiting_response":false,
      "credits":0
   },
   {
      "guid":"bee1b6af-4966-4a4b-bd68-0cef2ba0da13",
      "thread_guid":"e045a011-c295-4861-8cd1-8210b55443bd",
      "type":"response",
      "error":"None",
      "content":[
         {
            "message":{
               "role":"assistant",
               "content":"Sure, here's a joke for you:\n\nWhy did the tomato turn red?\n\nBecause it saw the salad dressing!"
            },
            "finish_reason":"stop",
            "index":0
         }
      ],
      "waiting_response":false,
      "credits":2
   }
]

Full code example

import requests
import time

bearer = '<<your_token_here>>'

# CREATE THREAD
# -------------
url = "https://app.ai-client.com/api/v1/threads"

payload={'ai_version_code': 'gpt-4'}

headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer ' + bearer
}

response = requests.request("POST", url, headers=headers, data=payload)

thread = response.json()['data']

print(thread)
#  {'guid': '946a5d77-ab48-4169-be1e-1604a754f848', 'ai_code': 'chatgpt', 'ai_version_code': 'gpt-4', 'title': 'New thread with gpt-4'}

# CREATE THREAD ENTRY OF TYPE request
# -----------------------------------
url = "https://app.ai-client.com/api/v1/threads/" + thread['guid'] + "/entry"

payload={'type': 'request', 'message' : 'Hello ChatGPT. Tell me a joke!'}

headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer ' + bearer
}

response = requests.request("POST", url, headers=headers, data=payload)

thread_entry = response.json()['data']

print(thread_entry)
# {'guid': 'ec622c31-3b5f-4601-afea-c5275f9ffbfb', 'thread_guid': 'e045a011-c295-4861-8cd1-8210b55443bd', 'type': 'request', 'error': None, 'content': {'role': 'user', 'content': 'Hello ChatGPT. Tell me a joke!'}, 'waiting_response': True, 'credits': 0}

# WAIT FOR 20 SECONDS
# -------------------
time.sleep(20)

# GET THREAD ENTRIES
# ------------------
url = "https://app.ai-client.com/api/v1/threads/" + thread['guid'] + "/entries"

headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer ' + bearer
}

response = requests.request("GET", url, headers=headers)

thread_entries = response.json()['data']

print(thread_entries)
# [{'guid': 'ec622c31-3b5f-4601-afea-c5275f9ffbfb', 'thread_guid': 'e045a011-c295-4861-8cd1-8210b55443bd', 'type': 'request', 'error': None, 'content': {'role': 'user', 'content': 'Hello ChatGPT. Tell me a joke!'}, 'waiting_response': False, 'credits': 0,}, {'guid': 'bee1b6af-4966-4a4b-bd68-0cef2ba0da13', 'thread_guid': 'e045a011-c295-4861-8cd1-8210b55443bd', 'type': 'response', 'error': None, 'content': [{'message': {'role': 'assistant', 'content': "Sure, here's a joke for you:\n\nWhy did the tomato turn red?\n\nBecause it saw the salad dressing!"}, 'finish_reason': 'stop', 'index': 0}], 'waiting_response': False, 'credits': 2}]

See our Swagger API documentation for more information.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *