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.2.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.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: openai_async_utils-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 15cb20f9ed7e82b74977b852c7d195f27c1829335cdc0681993bf0d89444622d
MD5 77dd4c3851b42119f75c71e77c09d0d4
BLAKE2b-256 6f0e5a936b75e49b5e341aa88092eb4da6bde8ccb6ef0c1122e31d93be9fa474

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openai_async_utils-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a842060a17d18aa8b0f27b883d879c86bca3d5bd950f555b6a3804868b4e83ef
MD5 a607b19c682a0c832dabfeb25fbb880d
BLAKE2b-256 fae54cb5b821a7c9092344a1efc644b33087db184f032cb3570a3596df33041c

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