chatgpt python
1.0.0
Bibliothek, die es Entwicklern ermöglicht, das ChatGPT einfach in ihre Python -Projekte zu integrieren.
pip install -U chatgpt
Erstellen Sie die Datei config.json
in Ihrem Arbeitsverzeichnis:
{
"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
}
Sie können den Standardkonfigurationsordner für ChatGPT angeben, indem Sie die Umgebungsvariable CHATGPT_HOME
auf den gewünschten Verzeichnispfad einstellen.
export CHATGPT_HOME= " /home/ $USER /.config/chatgpt "
Sie können die CLI mit:
chatgpt
oder
python -m chatgpt
Dies sind die verfügbaren Befehle:
reset
: Vergessen Sie den Kontext des aktuellen Gesprächs.clear
: Löschen Sie das Terminal.exit
: Beenden Sie die 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?" ))
Es wird empfohlen, Stream anstelle von Chat zu verwenden.
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
: Dieser Fehlercode zeigt an, dass das der API des Chatbot bereitgestellte Zugriffstoken nicht gültig ist oder abgelaufen ist.CHATGPT_API_ERROR
: Dieser Fehlercode zeigt an, dass ein Fehler aufgetreten ist, während eine Anfrage an die API des Chatbot gestellt wird.CONFIG_FILE_ERROR
: Dieser Fehlercode gibt an, dass ein Problem mit der Konfigurationsdatei für den Chatbot vorhanden ist.UNKNOWN_ERROR
: Dieser Fehlercode wird verwendet, wenn die Ursache des Fehlers unbekannt ist oder nicht bestimmt werden kann.LOGIN_ERROR
: Dieser Fehlercode gibt an, dass ein Problem mit dem Anmeldungsprozess wie einem falschen Benutzernamen oder Kennwort vorliegt.TIMEOUT_ERROR
: Dieser Fehlercode zeigt an, dass eine Anforderung an die API des Chatbots abgelaufen ist.CONNECTION_ERROR
: Dieser Fehlercode gibt an, dass mit der Verbindung zur API des Chatbot ein Problem besteht.