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
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.
ImgChatGPTAgentmay 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file easyapiopenai-0.1.2.tar.gz.
File metadata
- Download URL: easyapiopenai-0.1.2.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d98edbfd595238d7d9dd83c51be174962eef74d074605bc9b0cbbab09aeea9d
|
|
| MD5 |
bb8473da4f9f3b4b5eb4ff57cc45633a
|
|
| BLAKE2b-256 |
6a73f7462ea70e55ee5f52ac17484a3b5094654458a5369ad76a1111e3663aa6
|
File details
Details for the file EasyAPIOpenAI-0.1.2-py3-none-any.whl.
File metadata
- Download URL: EasyAPIOpenAI-0.1.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4db5919d3e44612d4728b6fd9fccf9a3083288b0cc6c63e80c4f434f74b7763a
|
|
| MD5 |
4cc566f2406aaad1f16e3760bc408340
|
|
| BLAKE2b-256 |
a37d4291bbf2ab94f753130f47ed5941815153080c9ac2e759dbe871ef7c4c9b
|