chatgpt python
1.0.0
개발자가 Chatgpt를 파이썬 프로젝트에 쉽게 통합 할 수있는 라이브러리.
pip install -U chatgpt
작업 디렉토리에서 file 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에 연결하는 데 문제가 있음을 나타냅니다.