Skip to main content

Python classes for interacting with OpenAI's API to use ChatGPT or DALL-E very easily.

Project description

OpenAI-API-requests

Python classes that allow for easy interaction with the OpenAI API, based fully on web requests.

The classes are well documented within the code file.

Currently Available Classes

ChatGPTAgent

  • Allows you to create an instance (object) of ChatGPT.
  • Saves conversation history and supports tweaking all parameters like model, system prompt, temperature, etc.
  • Methods:
    • GetResponse: Send a new message to the chatbot.
    • ChangeSystemMessage: Change the system message without affecting conversation history.
    • ClearHistory: Reset message history.

ImgChatGPTAgent

  • Similar to ChatGPTAgent, but introduces an additional method:
    • GetResponseWithImg: Send a message containing an image file.
  • Note: As of now, only ChatGPT 4o and 4o-mini support processing images.

DallEAgent

  • Allows you to generate an image with the DALL-E AI image generator.
  • Methods:
    • GetImage: Get the image as a PIL Image variable.
    • GetImageURL: Get a link to the generated image.

Usage

You can download the package using PIP:

pip install EasyAPIOpenAI

Then, use it as follows:

import easyapiopenai as oai

agent = oai.ChatGPTAgent("api-key", 'gpt-3.5-turbo')

Alternatively, if you prefer not to use PIP or are working with MicroPython, download the source code into the same folder and write:

from APIWojOpenAI import *

Make sure to adjust the filename as necessary.

Then, you can use it like this:

agent = ChatGPTAgent("api-key", 'gpt-3.5-turbo')

The ChatGPTAgent class should work well on embedded MicroPython systems, such as Raspberry Pi Pico. In that case, remove other classes from the file and replace all import statements with:

import urequests as requests

Example Code

from APIWojOpenAI import *  # Assuming you have my code in the same directory saved to file APIWojOpenAI.py

API_KEY = 'KEY'  # OpenAI API key; always remember to secure it properly.

bot = ChatGPTAgent(API_KEY, model="gpt-3.5-turbo", max_tokens=1000, role="Your name is Bob")

message = "Hello, who are you?"  # Sample message
answer = bot.GetResponse(message)  # Get answer
print(answer)

message = "Remember that my favourite animal is a lizard."  # Second sample message
answer = bot.GetResponse(message)  # Get next answer, continuing conversation
print(answer)

message = "What is my favourite animal?"  # Third sample message, to prove it remembers
answer = bot.GetResponse(message)  # Get next answer, continuing conversation
print(answer)

Output:

Hi there! My name is Bob. How can I assist you today?
Got it! Your favorite animal is a lizard. Would you like to talk about lizards or anything else related to them?
Your favorite animal is a lizard.

Known Errors and Things to Keep in Mind

  • An incorrect API key, nonexistent AI model, or other impossible parameters will result in a 404 error.
  • ImgChatGPTAgent may not remember the image for longer than one or two subsequent questions.
  • If chat history exceeds 100 messages, the AI might stop working. This behavior depends on the AI model; it has been observed with GPT-3.5-turbo.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easyapiopenai-0.1.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

EasyAPIOpenAI-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file easyapiopenai-0.1.1.tar.gz.

File metadata

  • Download URL: easyapiopenai-0.1.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for easyapiopenai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a5a31ec59bdb171f8ac0eedc2fd7ebcc49f29668e371b021ecc5876847a56a50
MD5 852b81aca4366f90af0fbbbeb35c1bce
BLAKE2b-256 b46f654f415348d84f5c51fcd77a64fdcc594c8cea5f45d099caecae358a35db

See more details on using hashes here.

File details

Details for the file EasyAPIOpenAI-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: EasyAPIOpenAI-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for EasyAPIOpenAI-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a9501b874d1eb2b65f82c9deeeb585437c19aa3b5243aadfba80f9b7449666f
MD5 0b9fe550e93015e67f6895143cbfc6fc
BLAKE2b-256 a093be45989b41fc2bb34b518b61ffc5215374def67a7a839206d7023f82e600

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page