• This repository has been archived on 25/Jan/2023
  • Stars
    star
    524
  • Rank 84,541 (Top 2 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created almost 2 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Unofficial Python SDK for OpenAI's ChatGPT

ChatGPT Python SDK

Library that allows developers to easily integrate the ChatGPT into their Python projects.

PyPi PyPi License

Install or update

pip install -U chatgpt

Config

Create a file with your credentials

Create the file config.json in your working directory:

{
    "email": "[email protected]",
    "password": "xxx"
}

With proxy

{
    "email": "[email protected]",
    "password": "xxx",
    "proxy": "socks5://user:pass@host:port"
}

With other parameters

{
    "email": "[email protected]",
    "password": "xxx",
    "timeout":300,
    "cache_file_path":"/path/filename",
    "access_token_seconds_to_expire":1800
}

Environment variable

You can specify the default configuration folder for chatgpt by setting the CHATGPT_HOME environment variable to the desired directory path.

export CHATGPT_HOME="/home/$USER/.config/chatgpt"

Usage

CLI

You can launch the CLI with:

chatgpt

or

python -m chatgpt

These are the available commands:

  • reset: forget the context of the current conversation.
  • clear: clear the terminal.
  • exit: exit the CLI.


SDK

#!/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?"))

it is recommended to use stream instead of chat.

Exceptions:

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")

Chatgpt Error codes:

  • INVALID_ACCESS_TOKEN: This error code indicates that the access token provided to the chatbot's API is not valid or has expired.
  • CHATGPT_API_ERROR: This error code indicates that an error has occurred while making a request to the chatbot's API.
  • CONFIG_FILE_ERROR: This error code indicates that there is a problem with the configuration file for the chatbot.
  • UNKNOWN_ERROR: This error code is used when the cause of the error is unknown or cannot be determined.
  • LOGIN_ERROR: This error code indicates that there was a problem with the login process, such as an incorrect username or password.
  • TIMEOUT_ERROR: This error code indicates that a request to the chatbot's API has timed out.
  • CONNECTION_ERROR: This error code indicates that there is a problem with the connection to the chatbot's API.

More Repositories

1

ernie

Simple State-of-the-Art BERT-Based Sentence Classification with Keras / TensorFlow 2. Built with HuggingFace's Transformers.
Python
199
star
2

bluebird

Unofficial Python client for Twitter
Python
43
star
3

pygram

Unofficial Python client for Instagram
Python
43
star
4

python-package

Template to create Python packages
Python
20
star
5

https-reverse-proxy

Reverse proxy + auto SSL/TLS certificates (Let's Encrypt) with Docker and YAML
Python
11
star
6

easysolc

Python wrapper for solc (Solidity compiler)
Python
6
star
7

alien

Unofficial Python client for Reddit
Python
5
star
8

easyweb3

Work easier with Web3 in Python
Python
5
star
9

easygraphql

A minimal library to interact with GraphQL from Python
Python
5
star
10

stopover

A simple and robust message broker built on top of RocksDB
Python
4
star
11

easyrocks

A python-rocksdb wrapper for a more comfortable interaction with RocksDB
Python
4
star
12

stopover-python

Python client for Stopover
Python
3
star
13

nostrd

Python
2
star
14

hashport

Python implementation of hashport, a text-to-port hashing function within the dynamic range (49152-65535).
Python
2
star
15

synced

Disk-synced objects for Python (dict, list & primitive types)
Python
2
star
16

kafka-standalone-docker

Kafka 2.6 Docker image with persistence
Shell
1
star
17

easyaerospike

Work easier with Aerospike in Python
Python
1
star
18

cherrybone

CherryPy server made easier
Python
1
star
19

beak

Unofficial Python client for DuckDuckGo
Python
1
star
20

txlog

Crash-resistant Python code made easy
Python
1
star
21

easymongo

Work easier with MongoDB in Python
Python
1
star
22

digsig

Digital Signature Toolkit for Python
Python
1
star