chatgpt python
1.0.0
允许开发人员轻松将Chatgpt集成到其Python项目中的库。
pip install -U chatgpt
在您的工作目录中创建文件config.json
:
{
"email" : " [email protected] " ,
"password" : " xxx "
}
{
"email" : " [email protected] " ,
"password" : " xxx " ,
"proxy" : " socks5://user:pass@host:port "
}
{
"email" : " [email protected] " ,
"password" : " xxx " ,
"timeout" : 300 ,
"cache_file_path" : " /path/filename " ,
"access_token_seconds_to_expire" : 1800
}
您可以通过将CHATGPT_HOME
环境变量设置为所需目录路径来指定chatgpt的默认配置文件夹。
export CHATGPT_HOME= " /home/ $USER /.config/chatgpt "
您可以使用以下方式启动CLI
chatgpt
或者
python -m chatgpt
这些是可用命令:
reset
:忘记当前对话的上下文。clear
:清除终端。exit
:退出CLI。 #!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from chatgpt import Conversation
conversation = 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?" ))
建议使用流而不是聊天。
from chatgpt import ChatgptError , ChatgptErrorCodes
try :
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 . code
if code == ChatgptErrorCodes . INVALID_ACCESS_TOKEN :
print ( "Invalid token" )
INVALID_ACCESS_TOKEN
:此错误代码指示提供给聊天机器人API的访问令牌无效或已过期。CHATGPT_API_ERROR
:此错误代码表明在向聊天机器人的API请求时发生了错误。CONFIG_FILE_ERROR
:此错误代码指示聊天机器人的配置文件存在问题。UNKNOWN_ERROR
:当错误原因未知或无法确定时,使用此错误代码。LOGIN_ERROR
:此错误代码表示登录过程存在问题,例如不正确的用户名或密码。TIMEOUT_ERROR
:此错误代码指示聊天机器人API的请求已超时。CONNECTION_ERROR
:此错误代码表示与聊天机器人API的连接存在问题。