Skip to main content

Async OpenAI API wrapper for Jupyter notebooks

Project description

Wurun

CI PyPI version Python versions License Release

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

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 logic
  • return_meta=True - Include latency and retry count
  • max_tokens=1024 - Maximum tokens in response (default: 1024)
  • temperature=0 - Response randomness (default: 0)

Batch Processing

  • Wurun.run_gather() - Preserve input order
  • Wurun.run_as_completed() - Process as results finish
  • concurrency parameter controls parallel requests
  • max_tokens and temperature available on all batch methods

DataFrame Processing

  • Wurun.run_dataframe() - Process messages from DataFrame column

Notebook Helpers

  • Wurun.print_qna_ordered() - Pretty print Q&A format
  • Wurun.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

  1. Create PR: Make changes and create pull request to main
  2. Auto Draft: Release Drafter automatically creates/updates draft release
  3. Publish Release: Go to GitHub Releases, edit draft, and publish
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wurun-0.2.2.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

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

wurun-0.2.2-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file wurun-0.2.2.tar.gz.

File metadata

  • Download URL: wurun-0.2.2.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

Hashes for wurun-0.2.2.tar.gz
Algorithm Hash digest
SHA256 50f4057942326cf256c554c31fbf9c06027381074849f03387de3b55800bbf94
MD5 43c5378a646f09cafbd3d774ab8be08b
BLAKE2b-256 f1fe9716f5459ea905dc49334db9a3139b973f24f698ba645dc6dfe5b986b782

See more details on using hashes here.

Provenance

The following attestation bundles were made for wurun-0.2.2.tar.gz:

Publisher: publish.yml on Aisuko/wurun

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wurun-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: wurun-0.2.2-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

Hashes for wurun-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d38185f1ad6b9d0a027e1f6ccc5225e1a27ea2388d70f49ef99b30674fbf3ede
MD5 75367b481a7f4493af2cb0f905b76765
BLAKE2b-256 c17f7481081245f7690dce1d9dc4436684cbd7502b6dba3da5f8933275d0a23f

See more details on using hashes here.

Provenance

The following attestation bundles were made for wurun-0.2.2-py3-none-any.whl:

Publisher: publish.yml on Aisuko/wurun

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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