Skip to main content

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

Install using pip:

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():
    # Initialise a storage location
    storage_location = Path("/path/to/storage")

    # Create a system message
    system_message = "You are a helpful chatbot. You are very friendly and helpful. You are a good friend to have."

    # Create the client
    client = SimpleOpenai(api_key, system_message, storage_location)

    # Create tasks for the chat response and the image response
    result = client.get_chat_response("Hello, how are you?", name="Bob", chat_id="Group 1")

    # 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():
    # Initialise a storage location
    storage_location = Path("/path/to/storage")

    # Create a system message
    system_message = "You are a helpful chatbot. You are very friendly and helpful. You are a good friend to have."

    # Create the client
    client = AsyncSimpleOpenai(api_key, system_message, storage_location)

    # Create tasks for the chat response and the image response
    tasks = [
        client.get_chat_response("Hello, how are you?", name="Bob", chat_id="Group 1"),
        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.

Functions

Functions can be added to the client using the add_function method. This method takes a function name and a function as arguments. The function should take an OpenAIFunction object as its first argument, and the Python function itself as the second argument.

The Python function should return a string, which will be passed to the API using the function role

Documentation

The documentation is available on GitHub

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

simple_openai-5.6.0.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_openai-5.6.0-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

Details for the file simple_openai-5.6.0.tar.gz.

File metadata

  • Download URL: simple_openai-5.6.0.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for simple_openai-5.6.0.tar.gz
Algorithm Hash digest
SHA256 3c846429feea4620df0eb6c7d5c082a4546db217cde0f57e121099fab062656c
MD5 912cc90f3840bc8bd48f96677539eafa
BLAKE2b-256 fc39aeba5dc1a65012a2c1df53c52ee2866d896f64d6b8b818ca4dc93c4fedec

See more details on using hashes here.

File details

Details for the file simple_openai-5.6.0-py3-none-any.whl.

File metadata

  • Download URL: simple_openai-5.6.0-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for simple_openai-5.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 233b19024c3a8160bed9755a01afed7e592c0ddafb6f59aeccb1ba0d1e29b4f4
MD5 8fa013243d6950f34e05ebd422030e4b
BLAKE2b-256 6e1cf8a493337160335f3ded1203d2696a594a70736897b5c18593e0277549fe

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