Giter VIP home page Giter VIP logo

chatgpt-python's Introduction

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.

chatgpt-python's People

Contributors

alexsua avatar brunneis avatar reyoung avatar mefengl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.