Skip to main content

Async helpers for OpenAI chat

Project description

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

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

openai_async_utils-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

openai_async_utils-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file openai_async_utils-0.1.0.tar.gz.

File metadata

  • Download URL: openai_async_utils-0.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.0

File hashes

Hashes for openai_async_utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 451ade8e8bb43fdc680761569ccfef69d8e45e1d5f9d7c18eb23dfe9069e12c9
MD5 a5e1030d75b66a4a7a51456cada1366a
BLAKE2b-256 d43f72775d9afbe133c4972dc62cd27b2250393bc241696961097c9c9fa854d4

See more details on using hashes here.

File details

Details for the file openai_async_utils-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_async_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c084321ec49ce08b63c230127749d155f1cd252a1b683689ba234f4493636ed0
MD5 49cea1f51817779913ca6db02137157f
BLAKE2b-256 20a0238c0c3271918b006031924ee57954d31736e5485ca7f2d6daeda6fdf9e9

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