Currently chatgpt-api
is not working, since OpenAI started to use Cloudflare. Also, it changes almost every day.
See mbroton/chatgpt-api#18
Unofficial API client and CLI for ChatGPT.
This project is based on httpx
(uses only HTTP) and uses Typer (with Rich) for CLI, so responses are looking good (markdown is supported). Also, It has almost 100% code coverage unlike other ChatGPT packages.
pip install chatgpt-api
pip install -r requirements.txt && pip install .
Required to authenticate. In this step you have to provide a path to the file containing the session key. A simple txt file with the key only is enough.
chatgpt setup
Tip: Use a file named .session_key in chatgpt-api top directory. It will be ignored by git - see .gitignore.
The key will be saved to
Path.home() / ".chatgpt_api" / "key.txt"
Session messages are logged to
Path.home() / ".chatgpt_api" / "logs"
chatgpt start
ChatGPT
class inherits from httpx.Client
Recommended usage:
from chatgpt.api import ChatGPT
with ChatGPT(session_token="your-session-token") as chat:
response = chat.send_message("Hello!")
print(response.content)
Without context manager you have to explicitly authenticate:
from chatgpt.api import ChatGPT
chat = ChatGPT(session_token="your-session-token")
chat.authenticate()
response = chat.send_message("Hello!")
print(response.content)
chat.close()
After you log in to ChatGPT in your browser, get value of __Secure-next-auth.session-token
cookie. In this project, this is named as a "session key".
- Open ChromeDevTools (F12).
- Click on "Application" tab.
- Click on "Cookies", on the left bar.
- Copy the value of
__Secure-next-auth.session-token
:
Now, you can use it in CLI or directly from Python code.
Distributed under the MIT License. See LICENSE
for more information.
This is a personal project, not affiliated in any way with OpenAI. If you have any objections, contact @mbroton.