Skip to main content

Unofficial Async Python client library for the OpenAI API

Project description

async-openai

Unofficial Async Python client library for the OpenAI API based on Documented Specs

Latest Version: PyPI version

Official Client

Features

  • Asyncio based with Sync and Async Support with httpx

  • Supports all API endpoints

  • Strongly typed validation of requests and responses with Pydantic Models with transparent access to the raw response and object-based results.

  • Handles Retries automatically through backoff

  • Supports Local and Remote Cloud Object Storage File Handling Asyncronously through file-io

    • Supports S3: s3://bucket/path/to/file.txt

    • Supports GCS: gs://bucket/path/to/file.txt

    • Supports Minio: minio://bucket/path/to/file.txt

  • Supports limited cost tracking for Completions and Edits requests (when stream is not enabled)


Installation

# Install from stable
pip install async-openai

# Install from dev/latest
pip install git+https://github.com/GrowthEngineAI/async-openai.git

Quick Usage

import asyncio
from async_openai import OpenAI, settings, CompletionResponse

# Environment variables should pick up the defaults
# however, you can also set them explicitly.

# `api_key` - Your OpenAI API key.                  Env: [`OPENAI_API_KEY`]
# `url` - The URL of the OpenAI API.                Env: [`OPENAI_URL`]
# `api_type` - The OpenAI API type.                 Env: [`OPENAI_API_TYPE`]
# `api_version` - The OpenAI API version.           Env: [`OPENAI_API_VERSION`]
# `organization` - The OpenAI organization.         Env: [`OPENAI_ORGANIZATION`]
# `proxies` - A dictionary of proxies to be used.   Env: [`OPENAI_PROXIES`]
# `timeout` - The timeout in seconds to be used.    Env: [`OPENAI_TIMEOUT`]
# `max_retries` - The number of retries to be used. Env: [`OPENAI_MAX_RETRIES`]

OpenAI.configure(
    api_key = "sk-XXXX",
    organization = "org-XXXX",
    debug_enabled = False,
)

# Alternatively you can configure the settings through environment variables
# settings.configure(
#    api_key = "sk-XXXX",
#     organization = "org-XXXX",
# )


# [Sync] create a completion
# Results return a CompletionResult object
result: CompletionResponse = OpenAI.completions.create(
    prompt = 'say this is a test',
    max_tokens = 4,
    stream = True
)

# print the completion text
# which are concatenated together from the result['choices'][n]['text']

print(result.text)

# print the number of choices returned
print(len(result))

# get the cost consumption for the request
print(result.consumption)

# [Async] create a completion
# All async methods are generally prefixed with `async_`

result: CompletionResponse = asyncio.run(
    OpenAI.completions.async_create(
        prompt = 'say this is a test',
        max_tokens = 4,
        stream = True
    )
)

Dependencies

The aim of this library is to be as lightweight as possible. It is built on top of the following libraries:

  • aiohttpx: Unified Async / Sync HTTP Client that wraps around httpx

    • httpx: Async / Sync HTTP Requests

    • lazyops: Provides numerous utility functions for working with Async / Sync code and data structures

  • pydantic: Type Support

  • file-io: Async Cloud-based File Storage I/O

  • backoff: Retries with Exponential Backoff

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

async_openai-0.0.15.tar.gz (28.5 kB view details)

Uploaded Source

Built Distribution

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

async_openai-0.0.15-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file async_openai-0.0.15.tar.gz.

File metadata

  • Download URL: async_openai-0.0.15.tar.gz
  • Upload date:
  • Size: 28.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for async_openai-0.0.15.tar.gz
Algorithm Hash digest
SHA256 49b89cddf81c282ee4d1761d51ea7e91ba34e2736f76aa67b0bad78ddb1bf029
MD5 d6e8a72cf89cff3656f2a06aff2a2fcd
BLAKE2b-256 340ca4a902dac6e3498449f0b427527869db73ad7309457e792c0a9d092d879d

See more details on using hashes here.

File details

Details for the file async_openai-0.0.15-py3-none-any.whl.

File metadata

  • Download URL: async_openai-0.0.15-py3-none-any.whl
  • Upload date:
  • Size: 37.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for async_openai-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 d1c4c4aea8c509023e3d8f7e9e5459d8335eec5b80c983032dc67eb470d2ba0b
MD5 6c53d2c0b2b770d2556346e6432ff028
BLAKE2b-256 1f234541b8141f141a240db9bf98d5176f81cca0db6a53bbee8082143acca9c3

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