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

Uploaded Python 3

File details

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

File metadata

  • Download URL: batch_router-0.2.6.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.6.tar.gz
Algorithm Hash digest
SHA256 50aa4ec09c5c25c1c028690515095f6e9ae9a57a23a88f14cb8191059ee5a195
MD5 f7c2933cbb01df46c51d63919e13c464
BLAKE2b-256 463fd7082e82ced4b5d6ce000a8e6dc9826026b4ba7d91034d21fb9788902ca4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: batch_router-0.2.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 443eb3c762fbc329483f3372df658d2dede7994afb80f2060c214258d541aa75
MD5 2553dd9fe87240ee42a28d16d11abc3f
BLAKE2b-256 7b566cec5f65138bc5f9111aa0abada153da36dba22e99907a5377263954a31c

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