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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openai_async_utils-0.1.1.tar.gz.
File metadata
- Download URL: openai_async_utils-0.1.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28fc203fe4791ccb855a7a933b5f1a753fb3dfdee08eb74f6e3f0ec289201142
|
|
| MD5 |
0f6cc872ab72ef3dbb278efbdc287e25
|
|
| BLAKE2b-256 |
08bcb41d5fe519c815973c032c227d42e896c36a2ade60651c77ea8b81ad3f4e
|
File details
Details for the file openai_async_utils-0.1.1-py3-none-any.whl.
File metadata
- Download URL: openai_async_utils-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5387da9e5b6527b8ac9ca87cdeb04953b239645246e249740aab9686e396a46b
|
|
| MD5 |
8175017feff60655de97fe1e5808d137
|
|
| BLAKE2b-256 |
797cd1812cd6bf5c96a8dac62a2e76a00eff8309729fa047870cd9e22d87de95
|