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 ได้โดยการตั้งค่าตัวแปรสภาพแวดล้อม CHATGPT_HOME
เป็นพา ธ ไดเรกทอรีที่ต้องการ
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 ของ ChatBot นั้นไม่ถูกต้องหรือหมดอายุCHATGPT_API_ERROR
: รหัสข้อผิดพลาดนี้ระบุว่าเกิดข้อผิดพลาดในขณะที่ทำการร้องขอไปยัง API ของ ChatbotCONFIG_FILE_ERROR
: รหัสข้อผิดพลาดนี้ระบุว่ามีปัญหากับไฟล์การกำหนดค่าสำหรับ chatbotUNKNOWN_ERROR
: รหัสข้อผิดพลาดนี้ใช้เมื่อไม่ทราบสาเหตุของข้อผิดพลาดหรือไม่สามารถกำหนดได้LOGIN_ERROR
: รหัสข้อผิดพลาดนี้ระบุว่ามีปัญหากับกระบวนการเข้าสู่ระบบเช่นชื่อผู้ใช้หรือรหัสผ่านที่ไม่ถูกต้องTIMEOUT_ERROR
: รหัสข้อผิดพลาดนี้ระบุว่าคำขอไปยัง API ของ ChatBot ได้หมดเวลาแล้วCONNECTION_ERROR
: รหัสข้อผิดพลาดนี้ระบุว่ามีปัญหากับการเชื่อมต่อกับ API ของ Chatbot