How to Use the Unofficial ChatGPT API Client

Jul 6, 2023 | Educational

In the ever-evolving world of API integrations, encountering issues can be common, particularly for developers relying on services like the ChatGPT API. The official ChatGPT API, which has faced interruptions due to changes implemented by OpenAI (specifically, Cloudflare), requires a workaround. Fortunately, there’s an unofficial API client available that can help you get back on track. This guide will walk you through the process of using this client efficiently.

Prerequisites

Before diving into the installation and usage steps, ensure you have the following:

  • Python installed on your system
  • A working internet connection

Installation Steps

To get started with the unofficial ChatGPT API client, follow these installation steps:

1. Install via pip

pip install chatgpt-api

2. Install from Source Code

If you prefer to install from source, execute the following commands:

pip install -r requirements.txt
pip install .

Usage

The functionality of the ChatGPT API can be accessed both through a command-line interface (CLI) and as an API in Python.

Using the Command-Line Interface

To set up the CLI, you need to be authenticated. Here’s how:

sh chatgpt setup

Use a file named .session_key in the top directory; this file will remain ignored by Git.

Once the setup is complete, you can start chatting using:

sh chatgpt start

Using as an API

To utilize the ChatGPT API in your Python code, you need to follow these steps:

from chatgpt.api import ChatGPT

with ChatGPT(session_token='your-session-token') as chat:
    response = chat.send_message('Hello!')
    print(response.content)

If you prefer not to use a context manager, remember to authenticate explicitly:

from chatgpt.api import ChatGPT

chat = ChatGPT(session_token='your-session-token')
chat.authenticate()

response = chat.send_message('Hello!')
print(response.content)
chat.close()

How to Acquire Your Session Key

After logging into ChatGPT, follow these steps to retrieve your session key:

  1. Open Chrome and activate DevTools (F12).
  2. Select the Application tab.
  3. Navigate to Cookies on the left sidebar.
  4. Copy the value of the __Secure-next-auth.session-token cookie.

This value will be your session key, which you will use within your CLI or Python code.

Cookie value example

Troubleshooting

If you encounter any issues while setting up or using the ChatGPT API client, consider the following troubleshooting tips:

  • Ensure your session token is valid and correctly copied from your browser.
  • Check that you have all necessary Python packages installed. Use the command pip list to verify.
  • If the command-line interface is not responding, confirm that you executed the setup command correctly.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

The unofficial ChatGPT API client offers a versatile way to integrate ChatGPT’s capabilities into your projects, especially in light of ongoing changes by OpenAI. With proper installation and use of session tokens, you can communicate effortlessly with the API.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Understanding the Code Through Analogy

Think of working with the unofficial ChatGPT API client like entering a special club. The club entrance represents the session token, which is your unique key. Without this key, you cannot enter and enjoy the conversations within. Once inside, you can choose how you want to communicate—either through direct face-to-face interactions (Command-Line Interface) or by sending messages through an intermediary (API). Every interaction is recorded so you can look back and see what has been discussed, like a journal of your conversations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox