https://github.com/wojtekb30/easyapiopenai
Python classes which allow using OpenAI API easily. Based fully on web requests.
https://github.com/wojtekb30/easyapiopenai
ai library openai-api python3 requests
Last synced: 21 days ago
JSON representation
Python classes which allow using OpenAI API easily. Based fully on web requests.
- Host: GitHub
- URL: https://github.com/wojtekb30/easyapiopenai
- Owner: Wojtekb30
- License: mit
- Created: 2024-09-30T20:08:17.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-11-13T13:34:43.000Z (6 months ago)
- Last Synced: 2025-04-09T23:18:13.201Z (23 days ago)
- Topics: ai, library, openai-api, python3, requests
- Language: Python
- Homepage: https://pypi.org/project/EasyAPIOpenAI/
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
```bash
pip install EasyAPIOpenAI
```Then, use it as follows:
```python
import easyapiopenai as oaiagent = 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:
```python
from APIWojOpenAI import *
```Make sure to adjust the filename as necessary.
Then, you can use it like this:
```python
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:
```python
import urequests as requests
```## Example Code
```python
import easyapiopenai as oaiAPI_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.