Simple OpenAI API wrapper
Project description
simple-openai
This is a simple wrapper around the OpenAI API. It's not meant to be a full-featured library, but rather a simple way to get started with the API.
The library provides both synchronous and asynchronous methods for interacting with the API.
Installation
pip install simple-openai
Usage
Calling the API
For the synchronous version, you can use the following code:
from simple_openai import SimpleOpenai
def main():
# Create the client
client = SimpleOpenai(api_key)
# Create tasks for the chat response and the image response
result = client.get_chat_response("Hello, how are you?")
# Print the result
if result.success:
# Print the message
print(f'Success: {result.message}')
else:
# Print the error
print(f'Error: {result.message}')
result = client.get_image_url("A cat")
# Print the result
if result.success:
# Print the message
print(f'Success: {result.message}')
else:
# Print the error
print(f'Error: {result.message}')
if __name__ == "__main__":
# Run the main function
main()
For the asynchronous version, you can use the following code:
from simple_openai import AsyncSimpleOpenai
import asyncio
async def main():
# Create the client
client = AsyncSimpleOpenai(api_key)
# Create tasks for the chat response and the image response
tasks = [
client.get_chat_response("Hello, how are you?"),
client.get_image_url("A cat"),
]
# Wait for the tasks to complete
for task in asyncio.as_completed(tasks):
# Get the result
result = await task
# Print the result
if result.success:
# Print the message
print(f'Success: {result.message}')
else:
# Print the error
print(f'Error: {result.message}')
if __name__ == "__main__":
# Run the main function
asyncio.run(main())
Output
The output of the functions is a SimpleOpenaiResponse object, which contains the following properties:
success
- A boolean indicating whether the request was successful or not.message
- The message returned by the API.
Documentation
The documentation is available on GitHub
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
simple-openai-1.0.3.tar.gz
(14.9 kB
view hashes)
Built Distribution
Close
Hashes for simple_openai-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d27a1bee2e5770c6a4f1b153127d4e54874f419a13a0c21ccac318c9c78bf12 |
|
MD5 | 87ddf10d06d010b24b78c4cbedd054d4 |
|
BLAKE2b-256 | 69699f0640a0f0fd8798c2a6e733864619032b10aa6a83944c4d24597f3ad0d9 |