Async OpenAI API wrapper for Jupyter notebooks
Project description
Wurun
Async OpenAI API wrapper optimized for Jupyter notebooks with connection pooling, retry logic, and batch processing.
Features
- HTTP/2 Connection Pooling - Shared client for efficient API calls
- Robust Retry Logic - Exponential backoff for rate limits and errors
- Batch Processing - Concurrent API calls with semaphore control
- Jupyter Optimized - Clean notebook output and error handling
- Zero Configuration - Simple setup, works with OpenAI and Azure OpenAI
Installation
# Production use
pip install .
# Development (includes testing tools)
pip install ".[dev]"
Quick Start
from wurun import Wurun
# Setup once per kernel
await Wurun.setup(
endpoint="https://api.openai.com/v1",
api_key="your-api-key",
deployment_name="gpt-3.5-turbo"
)
# Single question
messages = [{"role": "user", "content": "Explain asyncio"}]
answer = await Wurun.ask(messages)
print(answer)
# Custom parameters
answer = await Wurun.ask(messages, max_tokens=512, temperature=0.7)
# Batch processing
questions = [
[{"role": "user", "content": "What is Python?"}],
[{"role": "user", "content": "What is JavaScript?"}]
]
answers = await Wurun.run_gather(questions, concurrency=2)
# Cleanup
await Wurun.close()
DataFrame Processing
import pandas as pd
df = pd.DataFrame({
"id": [1, 2, 3],
"messages": [
[{"role": "user", "content": "What is Python?"}],
[{"role": "user", "content": "What is JavaScript?"}],
[{"role": "user", "content": "What is Go?"}]
]
})
results = await Wurun.run_dataframe(df, "messages", concurrency=2)
🧪 Practical Demo on Kaggle
API Reference
Setup
Wurun.setup()- Initialize client (call once per kernel)Wurun.close()- Clean up resources
Single Calls
Wurun.ask()- Single API call with retry logicreturn_meta=True- Include latency and retry countmax_tokens=1024- Maximum tokens in response (default: 1024)temperature=0- Response randomness (default: 0)
Batch Processing
Wurun.run_gather()- Preserve input orderWurun.run_as_completed()- Process as results finishconcurrencyparameter controls parallel requestsmax_tokensandtemperatureavailable on all batch methods
DataFrame Processing
Wurun.run_dataframe()- Process messages from DataFrame column
Notebook Helpers
Wurun.print_qna_ordered()- Pretty print Q&A formatWurun.print_as_ready()- Print results as they complete
Configuration
await Wurun.setup(
endpoint="https://api.openai.com/v1",
api_key="your-key",
deployment_name="gpt-3.5-turbo",
timeout=30.0,
max_connections=32,
max_keepalive=16,
http2=True,
max_retries=2
)
Error Handling
# Custom retry settings
result = await Wurun.ask(
messages,
attempts=3,
initial_backoff=1.0,
max_backoff=10.0,
max_tokens=512,
temperature=0.7
)
# Get metadata
answer, meta = await Wurun.ask(messages, return_meta=True)
print(f"Latency: {meta['latency']:.2f}s, Retries: {meta['retries']}")
Development
# Install dev dependencies
pip install ".[dev]"
# Run tests
pytest test_wurun.py -v
Release Process
- Create PR: Make changes and create pull request to
main - Auto Draft: Release Drafter automatically creates/updates draft release
- Publish Release: Go to GitHub Releases, edit draft, and publish
- Auto Deploy: Publishing triggers automatic PyPI deployment
Manual Version Update
# Update version in pyproject.toml
python scripts/update_version.py 1.2.3
License
Apache License 2.0
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 wurun-0.2.1.tar.gz.
File metadata
- Download URL: wurun-0.2.1.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c29d45cfd25fb5df94dd1e838fb5802abee139f885f0f695e2fab0780b127bdc
|
|
| MD5 |
c1bc1cae66f80309d71356bdb1f176e5
|
|
| BLAKE2b-256 |
a8ebcff713384e09bc46a9e40dc961268e2c72e13b6287d86d1a848d0b7f6bbd
|
Provenance
The following attestation bundles were made for wurun-0.2.1.tar.gz:
Publisher:
publish.yml on Aisuko/wurun
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wurun-0.2.1.tar.gz -
Subject digest:
c29d45cfd25fb5df94dd1e838fb5802abee139f885f0f695e2fab0780b127bdc - Sigstore transparency entry: 648040272
- Sigstore integration time:
-
Permalink:
Aisuko/wurun@4da63a6ffebb9f9a7051185c0d7ff8878cf9ecea -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/Aisuko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4da63a6ffebb9f9a7051185c0d7ff8878cf9ecea -
Trigger Event:
release
-
Statement type:
File details
Details for the file wurun-0.2.1-py3-none-any.whl.
File metadata
- Download URL: wurun-0.2.1-py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d8f65635a94a85ff51ef0c461e698eac57736a29ce34bc9ab6c46ce123a3c7f
|
|
| MD5 |
9721241743da9c506a7bfdb3f42ec783
|
|
| BLAKE2b-256 |
1230e0e2928f2870c1c986298313588037b4de9ff3e52dbe3bc2d232b21516f6
|
Provenance
The following attestation bundles were made for wurun-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on Aisuko/wurun
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wurun-0.2.1-py3-none-any.whl -
Subject digest:
2d8f65635a94a85ff51ef0c461e698eac57736a29ce34bc9ab6c46ce123a3c7f - Sigstore transparency entry: 648040275
- Sigstore integration time:
-
Permalink:
Aisuko/wurun@4da63a6ffebb9f9a7051185c0d7ff8878cf9ecea -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/Aisuko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4da63a6ffebb9f9a7051185c0d7ff8878cf9ecea -
Trigger Event:
release
-
Statement type: