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 which allow using OpenAI API easily. Based fully on web requests.

The classes are well documented within the code file.

Currently there are classes:

ChatGPTAgent - allows you to create instance (object) of ChatGPT, saves conversation history and supports tweaking all parameters like model, system prompt, temperature etc. Use GetResponse to send a new message to the chatbot, ChangeSystemMessage to change system message without affecting conversation history, and use ClearHistory to reset message history.

ImgChatGPTAgent - same as ChatGPTAgent, but introduces additional method GetResponseWithImg, which allows you to send a message containing a image file. Please mind that as of now only ChatGPT 4o and 4o-mini support processing images.

DallEAgent - Allows to generate an image with DALL-E AI image generator. Use GetImage to get the image as PIL Image variable, or use GetImageURL to get link to generated image.

Usage

To use it in your Python project, download my code into same folder and write:

from APIWojOpenAI import *

Of course that depends on how you name the file.

Class ChatGPTAgent 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 .

I may make it a pip package someday.

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 about securing it propely.

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 mind

Incorrect API key, nonexistent AI model or other impossible parameter will result in 404 error.

ImgChatGPTAgent may not remember the image for longer than one or two next questions.

If chat history is longer than 100 messages, the AI might stop working. It depends on the AI model, for sure. I noticed that for 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.0.tar.gz (5.7 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.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: easyapiopenai-0.1.0.tar.gz
  • Upload date:
  • Size: 5.7 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.0.tar.gz
Algorithm Hash digest
SHA256 42b5f50eddbc2778561ac9019bd3f4c397164a12affb38e87e8405959793e679
MD5 448f819fd46b119ece4cac4fd8443ddc
BLAKE2b-256 9ec3ea640813bc77697e0ddeee064b4065d87e53887658640bdc899e0db9948b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: EasyAPIOpenAI-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24076f102ca25eb8d4051a3bd0ff96647376c695ebb6a7b25e92a2ec7d30c348
MD5 0548fa7af59f5aec232b3a83fc2160e2
BLAKE2b-256 7ddb7538adcb0124e5b2ba605b11d638d64ed3220df7cb5314c13e07fbd80d7d

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