Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit 3d75da3

Browse files
committed
Documentation improvements and environment variables
1 parent 085b228 commit 3d75da3

File tree

7 files changed

+191
-93
lines changed

7 files changed

+191
-93
lines changed

README.md

+63-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<h1 align="center">ChatGPT Python SDK</h1>
2-
3-
<br />
2+
<p align="center">
3+
Library that allows developers to easily integrate the ChatGPT into their Python projects.
4+
<br />
5+
<br />
6+
</p>
47
<p align="center">
58
<a href="https://github.com/labteral/chatgpt-python/issues"><img alt="PyPi" src="https://img.shields.io/github/issues/labteral/chatgpt-python.svg?style=flat-square"></a>
69
<a href="https://pypi.python.org/pypi/chatgpt/"><img alt="PyPi" src="https://img.shields.io/pypi/v/chatgpt.svg?style=flat-square"></a>
@@ -13,9 +16,8 @@
1316
pip install -U chatgpt
1417
```
1518

16-
17-
## Create a file with your credentials
18-
> :warning: &nbsp; Please, update the library. Note the change in the config file.
19+
## Config
20+
### Create a file with your credentials
1921

2022
Create the file `config.json` in your working directory:
2123
```json
@@ -40,14 +42,24 @@ Create the file `config.json` in your working directory:
4042
"email": "[email protected]",
4143
"password": "xxx",
4244

43-
//Timeout for requests made.
45+
// Timeout per request.
4446
"timeout":300,
4547

46-
//Cache for saving the cookies and the state of the session.
47-
"cache_file_path":"./path_and_filename"
48+
// Cache for saving the cookies and the state of the session.
49+
"cache_file_path":"/path/filename",
50+
51+
// Time for refresh the access token.
52+
"access_token_seconds_to_expire":1800
4853
}
4954
```
5055

56+
## Environment variable
57+
You can specify the default configuration folder for chatgpt by setting the `CHATGPT_HOME` environment variable to the desired directory path.
58+
59+
```bash
60+
export CHATGPT_HOME="/home/$USER/.config/chatgpt"
61+
```
62+
5163

5264
## Usage
5365
### CLI
@@ -65,6 +77,15 @@ These are the available commands:
6577
- `clear`: clear the terminal.
6678
- `exit`: exit the CLI.
6779

80+
<br/>
81+
82+
<div style="padding:0.2em;padding-top:0">
83+
84+
![](img/cli_example.gif)
85+
86+
</div>
87+
<br>
88+
6889
### SDK
6990
```python
7091
#!/usr/bin/env python
@@ -74,19 +95,48 @@ from chatgpt import Conversation
7495

7596
conversation = Conversation()
7697

77-
# Python generator to stream the message in chunks.
78-
# the chunk give you a new part of the final message.
98+
# Stream the message as it arrives.
7999
for chunk in conversation.stream("We are going to start a conversation. I will speak English and you will speak Portuguese."):
80-
# print chunk without line break
81100
print(chunk, end="")
82-
# flush terminal output
83101
sys.stdout.flush()
84102

85-
# this conversation chat instead is going to wait until the response is completely received.
103+
# Wait until the message is fully received.
86104
print(conversation.chat("What's the color of the sky?"))
87105

88106
# The AI will forget it was speaking Portuguese
89107
conversation.reset()
90108
print(conversation.chat("What's the color of the sun?"))
91109

92110
```
111+
> it is recommended to use *stream* instead of *chat*.
112+
#### **Exceptions**:
113+
114+
```python
115+
from chatgpt import ChatgptError, ChatgptErrorCodes
116+
117+
try:
118+
119+
for chunk in conversation.stream("Hello, world!"):
120+
print(chunk, end="")
121+
sys.stdout.flush()
122+
123+
except ChatgptError as chatgpt_error:
124+
125+
message = chatgpt_error.message
126+
code = chatgpt_error.code
127+
128+
if code == ChatgptErrorCodes.INVALID_ACCESS_TOKEN:
129+
print("Invalid token")
130+
131+
```
132+
133+
134+
#### **Chatgpt Error codes:**
135+
136+
- `INVALID_ACCESS_TOKEN`: This error code indicates that the access token provided to the chatbot's API is not valid or has expired.
137+
- `CHATGPT_API_ERROR`: This error code indicates that an error has occurred while making a request to the chatbot's API.
138+
- `CONFIG_FILE_ERROR`: This error code indicates that there is a problem with the configuration file for the chatbot.
139+
- `UNKNOWN_ERROR`: This error code is used when the cause of the error is unknown or cannot be determined.
140+
- `LOGIN_ERROR`: This error code indicates that there was a problem with the login process, such as an incorrect username or password.
141+
- `TIMEOUT_ERROR`: This error code indicates that a request to the chatbot's API has timed out.
142+
- `CONNECTION_ERROR`: This error code indicates that there is a problem with the connection to the chatbot's API.

chatgpt/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
from .chatgpt import *
55

6-
__version__ = '2.2212.0'
6+
__version__ = '2.2214.0'

0 commit comments

Comments
 (0)