Skip to main content

openai-async-utils

Lightweight async helpers for OpenAI chat completions:

  • call_openai_api: one-shot request with retries.
  • stream_openai_api: yields response chunks with retries.

Install

pip install openai-async-utils
export OPENAI_API_KEY="sk-…"

You can also pass api_key="sk-…" directly to functions.


Quick Example

import asyncio
from openai_utils import call_openai_api, stream_openai_api

async def main():
    sys = "You are a helpful assistant."
    prev = [{"role": "user", "content": "Hello!"}]

    # Full response
    text, usage = await call_openai_api(sys, prev)
    print(text, usage)

    # Streaming response
    async for chunk in stream_openai_api(sys, prev):
        print(chunk, end="")

asyncio.run(main())

API

  • call_openai_api(system_prompt, previous_prompts, api_key=None, validation_schema=None, model="gpt-3.5-turbo", max_attempts=3) Returns (response_str, {"prompt_tokens":…, "completion_tokens":…, "total_tokens":…}).

  • stream_openai_api(system_prompt, previous_prompts, api_key=None, model="gpt-3.5-turbo", max_attempts=3) Async generator yielding text chunks.


License

MIT

Metadata

Release files for openai-async-utils 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-async-utils 0.1.2
File Size Uploaded
openai_async_utils-0.1.2.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openai-async-utils 0.1.2
File Interpreter ABI Platform
openai_async_utils-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.3 kB

Release files / openai_async_utils-0.1.2.tar.gz

Download URL openai_async_utils-0.1.2.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
15cb20f9ed7e82b74977b852c7d195f27c1829335cdc0681993bf0d89444622d
BLAKE2b-256 checksum
How to use checksums
6f0e5a936b75e49b5e341aa88092eb4da6bde8ccb6ef0c1122e31d93be9fa474
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.0

Release files / openai_async_utils-0.1.2-py3-none-any.whl

Download URL openai_async_utils-0.1.2-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a842060a17d18aa8b0f27b883d879c86bca3d5bd950f555b6a3804868b4e83ef
BLAKE2b-256 checksum
How to use checksums
fae54cb5b821a7c9092344a1efc644b33087db184f032cb3570a3596df33041c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.0

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page