Skip to main content

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

import easyapiopenai as oai

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

bot = oai.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.

Release files for EasyAPIOpenAI 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for EasyAPIOpenAI 0.1.2
File Size Uploaded
easyapiopenai-0.1.2.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for EasyAPIOpenAI 0.1.2
File Interpreter ABI Platform
EasyAPIOpenAI-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / easyapiopenai-0.1.2.tar.gz

Download URL easyapiopenai-0.1.2.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7d98edbfd595238d7d9dd83c51be174962eef74d074605bc9b0cbbab09aeea9d
BLAKE2b-256 checksum
How to use checksums
6a73f7462ea70e55ee5f52ac17484a3b5094654458a5369ad76a1111e3663aa6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release files / EasyAPIOpenAI-0.1.2-py3-none-any.whl

Download URL EasyAPIOpenAI-0.1.2-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4db5919d3e44612d4728b6fd9fccf9a3083288b0cc6c63e80c4f434f74b7763a
BLAKE2b-256 checksum
How to use checksums
a37d4291bbf2ab94f753130f47ed5941815153080c9ac2e759dbe871ef7c4c9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page