If you’ve applied for GPT-4 API access and are still waiting for a reply from OpenAI, you’re not alone. Thankfully, there’s a way to get unofficial access via chat.openai.com by leveraging Selenium WebDriver. In this article, you’ll learn how to set up and use this unofficial API efficiently.
Understanding the Unofficial API
This unofficial API package utilizes Selenium to emulate user interactions, allowing you to access the capabilities of GPT-4 (and DALL-E 3) if you have a ChatGPT Plus subscription. However, please keep in mind that this is not an official implementation, and its use may violate OpenAI’s Terms of Service.
Installation Steps
To get started, you’ll need to install the necessary Python package. Just follow these simple steps:
- Open your terminal or command prompt.
- Run the following command:
pip install gpt4-openai-api
Setting Up the API
Here’s a step-by-step guide on how to set up your API access:
- Retrieve your session token.
- Use the Python code below to interact with GPT-4:
from gpt4_openai import GPT4OpenAI
# Token is the __Secure-next-auth.session-token from chat.openai.com
llm = GPT4OpenAI(token=my_session_token, headless=False, model="gpt-4")
# Sample interaction
response = llm("If there are 10 books in a room and I read 2, how many books are still in the room?")
print(response)
Explaining the Code: An Analogy
Think of the code snippet you just saw as sending a letter to a knowledge wizard (GPT-4) who resides in a castle (the API). The session token is like a secret password that lets you into the castle. Once inside, you can ask the wizard questions, and he responds with wisdom (answers). However, if you don’t have the correct password (session token), you won’t be granted access!
Working with DALL-E 3 and Browsing
To utilize DALL-E 3 or harness browsing capabilities with the API, you can incorporate the following code snippets:
DALL-E 3 Support
llm = GPT4OpenAI(token=my_session_token, headless=False, model="gpt-4")
img_bytes = llm.generate_image("Generate an isometric image of a cute doggo inside a house.", image_path="img_save_path.png")
Browsing Support
llm = GPT4OpenAI(token=my_session_token, headless=False, model="gpt-4-browsing")
response = llm("What is the age difference between Dua Lipa and her boyfriend?")
print(response)
Langchain Support
This library also integrates with Langchain, allowing for seamless functionality within that ecosystem. Here’s an example:
from langchain import LLMChain
from langchain.prompts.chat import (ChatPromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate)
template = "You are a helpful assistant that translates English to pirate."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_message_prompt = HumanMessagePromptTemplate.from_template(text)
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, example_human, example_ai, human_message_prompt])
llm = GPT4OpenAI(token=my_session_token)
chain = LLMChain(llm=llm, prompt=chat_prompt)
print(chain.run("My name is John and I like to eat pizza."))
Getting Your Session Token
Follow these steps to find your session token:
- Go to chat.openai.com.
- Open developer tools by pressing F12.
- Locate the
__Secure-next-auth.session-tokenunder Application > Storage > Cookies. - Copy the value from the Cookie Value field.
Troubleshooting
If you encounter any issues while using the unofficial API, here are some troubleshooting tips:
- Ensure your session token is up-to-date. Tokens can expire or change.
- Check browser compatibility with the Selenium version you are using.
- Make sure you comply with OpenAI’s Terms of Service to avoid any misuse.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
While waiting for your official GPT-4 access, this unofficial API provides you with a potent tool to harness the power of chat and generate creative outputs with AI. 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.

