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的連接存在問題。