1
1
<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 >
4
7
<p align =" center " >
5
8
<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>
6
9
<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
16
pip install -U chatgpt
14
17
```
15
18
16
-
17
- ## Create a file with your credentials
18
- > :warning :   ; Please, update the library. Note the change in the config file.
19
+ ## Config
20
+ ### Create a file with your credentials
19
21
20
22
Create the file ` config.json ` in your working directory:
21
23
``` json
@@ -40,14 +42,24 @@ Create the file `config.json` in your working directory:
40
42
41
43
"password" : " xxx" ,
42
44
43
- //Timeout for requests made .
45
+ // Timeout per request .
44
46
"timeout" :300 ,
45
47
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
48
53
}
49
54
```
50
55
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
+
51
63
52
64
## Usage
53
65
### CLI
@@ -65,6 +77,15 @@ These are the available commands:
65
77
- ` clear ` : clear the terminal.
66
78
- ` exit ` : exit the CLI.
67
79
80
+ <br />
81
+
82
+ <div style =" padding :0.2em ;padding-top :0 " >
83
+
84
+ ![ ] ( img/cli_example.gif )
85
+
86
+ </div >
87
+ <br >
88
+
68
89
### SDK
69
90
``` python
70
91
# !/usr/bin/env python
@@ -74,19 +95,48 @@ from chatgpt import Conversation
74
95
75
96
conversation = Conversation()
76
97
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.
79
99
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
81
100
print (chunk, end = " " )
82
- # flush terminal output
83
101
sys.stdout.flush()
84
102
85
- # this conversation chat instead is going to wait until the response is completely received.
103
+ # Wait until the message is fully received.
86
104
print (conversation.chat(" What's the color of the sky?" ))
87
105
88
106
# The AI will forget it was speaking Portuguese
89
107
conversation.reset()
90
108
print (conversation.chat(" What's the color of the sun?" ))
91
109
92
110
```
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.
0 commit comments