https://github.com/mbroton/chatgpt-api
ChatGPT HTTP API Client and CLI
https://github.com/mbroton/chatgpt-api
ai api chatgpt cli client http python
Last synced: 10 months ago
JSON representation
ChatGPT HTTP API Client and CLI
- Host: GitHub
- URL: https://github.com/mbroton/chatgpt-api
- Owner: mbroton
- License: mit
- Created: 2022-12-04T21:40:09.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-15T11:30:40.000Z (about 3 years ago)
- Last Synced: 2024-10-13T15:44:39.287Z (over 1 year ago)
- Topics: ai, api, chatgpt, cli, client, http, python
- Language: Python
- Homepage:
- Size: 74.2 KB
- Stars: 122
- Watchers: 9
- Forks: 13
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
💻 ChatGPT API
Unofficial API client and CLI for ChatGPT.

This project is based on `httpx` (uses only HTTP) and uses [Typer (with Rich)](https://typer.tiangolo.com/) for CLI, so responses are looking good (markdown is supported). Also, It has almost 100% code coverage unlike other ChatGPT packages.

## Current status
This project was created before an API for GPT models existed, and it became deprecated just as quickly as it was developed. The implemented solution has not been operational since the end of 2022.
## Installation
### From Pypi
```
pip install chatgpt-api
```
### Source code
```sh
pip install -r requirements.txt && pip install .
```
## Usage
### As a Command Line Interface
#### Setup
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.
```sh
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
```python
Path.home() / ".chatgpt_api" / "key.txt"
```
Session messages are logged to
```python
Path.home() / ".chatgpt_api" / "logs"
```
#### Start chatting
```sh
chatgpt start
```
### As an API
`ChatGPT` class inherits from `httpx.Client`
Recommended usage:
```python
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:
```python
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 session key?
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".
### Chrome instruction
1. Open ChromeDevTools (F12).
2. Click on "Application" tab.
3. Click on "Cookies", on the left bar.
4. Copy the value of `__Secure-next-auth.session-token`:

Now, you can use it in CLI or directly from Python code.
## License
Distributed under the MIT License. See `LICENSE` for more information.
## Disclaimer
This is a personal project, not affiliated in any way with OpenAI. If you have any objections, contact @mbroton.