Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/labteral/chatgpt-python
Unofficial Python SDK for OpenAI's ChatGPT
https://github.com/labteral/chatgpt-python
chatgpt davinci gpt-3 gpt3-library openai
Last synced: 2 months ago
JSON representation
Unofficial Python SDK for OpenAI's ChatGPT
- Host: GitHub
- URL: https://github.com/labteral/chatgpt-python
- Owner: labteral
- License: gpl-3.0
- Archived: true
- Created: 2022-12-03T12:23:41.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2022-12-22T12:25:27.000Z (about 2 years ago)
- Last Synced: 2024-09-17T03:53:40.220Z (4 months ago)
- Topics: chatgpt, davinci, gpt-3, gpt3-library, openai
- Language: Python
- Homepage: https://labteral.github.io/chatgpt-python/
- Size: 3.56 MB
- Stars: 523
- Watchers: 19
- Forks: 76
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- Awesome-ChatGPT - chatgpt-python
- awesome-ChatGPT-repositories - chatgpt-python - Unofficial Python SDK for OpenAI's ChatGPT (Openai)
- chatgpt-awesome - chatgpt-python
- awesome-chatgpt - Wrapper
- awesome-chatgpt - chatgpt-python
README
ChatGPT Python SDK
Library that allows developers to easily integrate the ChatGPT into their Python projects.
## Install or update
```bash
pip install -U chatgpt
```## Config
### Create a file with your credentialsCreate the file `config.json` in your working directory:
```json
{
"email": "[email protected]",
"password": "xxx"
}
```### With proxy
```json
{
"email": "[email protected]",
"password": "xxx",
"proxy": "socks5://user:pass@host:port"
}
```### With other parameters
```json
{
"email": "[email protected]",
"password": "xxx",
"timeout":300,
"cache_file_path":"/path/filename",
"access_token_seconds_to_expire":1800
}
```## Environment variable
You can specify the default configuration folder for chatgpt by setting the `CHATGPT_HOME` environment variable to the desired directory path.```bash
export CHATGPT_HOME="/home/$USER/.config/chatgpt"
```## Usage
### CLI
You can launch the CLI with:
```bash
chatgpt
```
or
```bash
python -m chatgpt
```These are the available commands:
- `reset`: forget the context of the current conversation.
- `clear`: clear the terminal.
- `exit`: exit the CLI.
![](img/cli_example.gif)
### SDK
```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from chatgpt import Conversationconversation = Conversation()
# Stream the message as it arrives.
for chunk in conversation.stream("We are going to start a conversation. I will speak English and you will speak Portuguese."):
print(chunk, end="")
sys.stdout.flush()# Wait until the message is fully received.
print(conversation.chat("What's the color of the sky?"))# The AI will forget it was speaking Portuguese
conversation.reset()
print(conversation.chat("What's the color of the sun?"))```
> it is recommended to use *stream* instead of *chat*.
#### **Exceptions**:```python
from chatgpt import ChatgptError, ChatgptErrorCodestry:
for chunk in conversation.stream("Hello, world!"):
print(chunk, end="")
sys.stdout.flush()except ChatgptError as chatgpt_error:
message = chatgpt_error.message
code = chatgpt_error.codeif code == ChatgptErrorCodes.INVALID_ACCESS_TOKEN:
print("Invalid token")```
#### **Chatgpt Error codes:**
- `INVALID_ACCESS_TOKEN`: This error code indicates that the access token provided to the chatbot's API is not valid or has expired.
- `CHATGPT_API_ERROR`: This error code indicates that an error has occurred while making a request to the chatbot's API.
- `CONFIG_FILE_ERROR`: This error code indicates that there is a problem with the configuration file for the chatbot.
- `UNKNOWN_ERROR`: This error code is used when the cause of the error is unknown or cannot be determined.
- `LOGIN_ERROR`: This error code indicates that there was a problem with the login process, such as an incorrect username or password.
- `TIMEOUT_ERROR`: This error code indicates that a request to the chatbot's API has timed out.
- `CONNECTION_ERROR`: This error code indicates that there is a problem with the connection to the chatbot's API.