Skip to main content

Async Python client library for the OpenAI API

Project description

async-openai

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

Features


Installation

pip install async-openai

Quick Usage

import asyncio
from async_openai import OpenAI, settings

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

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.1.tar.gz (34.6 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.1-py3-none-any.whl (44.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for async_openai-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9aef80cde8b2323fe0255de79b42977513bac234b39150b4ecea3b3241ee678a
MD5 593a1d46a51bca565a8af518030633a9
BLAKE2b-256 66015fbebfd3fce770fb895c2a32487e5a67dfe0da540b5ef1353b03e521a3bd

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for async_openai-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0b7e1edaf90346913c89d0a8326181f75ceafefa40cee99eb7fa259b1bd748fa
MD5 16381cfe69470a2113440532f9dc732c
BLAKE2b-256 a38d15b85cc8d445370ad58c1d95efd30513ab8370b481404ad7efc6053a6ccb

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