Skip to main content

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

Features


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

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

# `api_key` - Your OpenAI API key.  Env: [`OPENAI_API_KEY`]
# `api_base` - The base URL of the OpenAI API. Env: [`OPENAI_API_BASE`]
# `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_secs` - The timeout in seconds to be used. Env: [`OPENAI_TIMEOUT_SECS`]
# `max_retries` - The number of retries to be used. Env: [`OPENAI_MAX_RETRIES`]

settings.configure(
    api_key = "sk-XXXX",
    organization = "org-XXXX",
)

# [Sync] create a completion
# Results return a CompletionResult object
result = 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.completion_text)

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

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

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

Alternative Usage

Following a similar pattern to the original openai python project, you can additionally use the methods as a drop-in replacement for most functions

import asyncio
import async_openapi as openai

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

openai.settings.configure(
    api_key = "sk-XXXX",
    organization = "org-XXXX",
)

# [Sync] create a completion
# Results return a CompletionResult object
# Note, the only difference in this method is that the `Completion` is capitalized to match the OpenAI API

result = 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.completion_text)

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

# [Async] create a completion
result = 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:

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.2.tar.gz (34.3 kB view hashes)

Uploaded Source

Built Distribution

async_openai-0.0.2-py3-none-any.whl (42.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page