Skip to main content

A Python package designed to facilitate batch LLM requests efficiently across multiple providers with a unified interface.

Project description

Batch Router

A Python package designed to facilitate batch LLM requests efficiently across multiple providers with a unified interface.

Installation

You can install the package using pip:

pip install batch-router

Basic Usage

Here is a simple example of how to use batch-router with vLLM:

import time
from batch_router.core.base import BatchConfig, BatchStatus, ProviderId
from batch_router.providers import vLLMProvider
from batch_router.core.input import InputBatch, InputRequest, InputMessage, InputMessageRole
from batch_router.core.base import TextContent, InferenceParams

# Initialize the provider
provider = vLLMProvider(model_path="./gemma_3_270m")

# Create requests
requests = [
    InputRequest(
        custom_id=f"request_{i+1}",
        messages=[
            InputMessage(
                role=InputMessageRole.USER,
                contents=[
                    TextContent(text="Hello, how are you?")
                ]
            )
        ],
        params=InferenceParams(
            max_output_tokens=100
        )
    )
    for i in range(5)
]

# Create an input batch
input_batch = InputBatch(
    requests=requests
)

# Configure the batch for vLLM
vllm_batch = input_batch.with_config(
    config=BatchConfig(
        provider_id=ProviderId.VLLM,
        model_id="./gemma_3_270m"
    )
)

# Send the batch
batch_id = provider.send_batch(vllm_batch)

# Poll for completion
while provider.poll_status(batch_id) != BatchStatus.COMPLETED:
    time.sleep(5)
    print(f"Batch {batch_id} is {provider.poll_status(batch_id)}")

# Retrieve results
results = provider.get_results(batch_id)
for request in results.requests:
    print(f"Request {request.custom_id} response: {request.messages[0].contents[0].text}")

Available Providers

batch-router supports multiple providers. You can import them from batch_router.providers (or their specific submodules if not exposed directly).

OpenAI

from batch_router.providers import OpenAIChatProvider

provider = OpenAIChatProvider(api_key="your-api-key")

Anthropic

from batch_router.providers import AnthropicProvider

provider = AnthropicProvider(api_key="your-api-key")

Google GenAI

from batch_router.providers.google.google_genai_provider import GoogleGenAIProvider

provider = GoogleGenAIProvider(api_key="your-api-key")

vLLM

from batch_router.providers import vLLMProvider

provider = vLLMProvider(model_path="/path/to/model")

Advanced Usage

Multi-Modal Inputs

You can send images and audio (depending on provider support) using ImageContent and AudioContent.

from batch_router.core.base import ImageContent, Modality

image_content = ImageContent(image_base64="base64_encoded_image_string")
# Add to InputMessage contents...

Reusing Batches across Providers

You can define a generic InputBatch and configure it for different providers using .with_config().

# Define generic batch
input_batch = InputBatch(requests=...)

# Configure for OpenAI
openai_batch = input_batch.with_config(
    config=BatchConfig(
        provider_id=ProviderId.OPENAI,
        model_id="gpt-4o"
    )
)

# Configure for Anthropic
anthropic_batch = input_batch.with_config(
    config=BatchConfig(
        provider_id=ProviderId.ANTHROPIC,
        model_id="claude-3-5-sonnet-20241022"
    )
)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

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

batch_router-0.2.5.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

batch_router-0.2.5-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file batch_router-0.2.5.tar.gz.

File metadata

  • Download URL: batch_router-0.2.5.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for batch_router-0.2.5.tar.gz
Algorithm Hash digest
SHA256 b53a1beff2ce66d5dd17cccc16d474232aed7d295162fb79a07e107ff0fd1fb1
MD5 0569bdfce7595ae8bf084a25428134f5
BLAKE2b-256 58825bb0443c9f9b18dd3eb64466f0bc99a0cadb44bfde1a7c59ef87a289c687

See more details on using hashes here.

File details

Details for the file batch_router-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: batch_router-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for batch_router-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9f3ce5e89dd9fc3805da7d76c0359124d9526c82144b5580d52c9caeff19afb9
MD5 8121184c73d32c605d13f73f155b05d4
BLAKE2b-256 828221df22c793c6f2c712208af86ae7ac1b5bbec83249cb21412fbfeb4d3be5

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