Skip to main content

Helper utilities for Amazon Bedrock (RAG, embeddings, batch jobs)

Project description

BedrockHelper

A small, batteries-included helper for Amazon Bedrock:

  • RAG Q&A using Bedrock Runtime converse / converse_stream when available, with automatic fallback.
  • Embeddings using Titan embed text v2:
    • Small batches inline
    • Large batches via Bedrock batch inference (S3 JSONL)
  • Works in sync and async codebases.
  • Includes a pluggable JobStore abstraction for tracking batch jobs (in-memory, Redis, Postgres, etc.).

Structure

├── .github
│      └── workflows
│          └── mypy.yml
├── .gitignore
├── poetry.lock
├── pyproject.toml
├── README.md
├── src
│    └── bedrockhelper
│          ├── __init__.py
│          ├── batch
│          ├── main.py
│          ├── models.py
│          ├── types.py
│          └── utils.py
└── tests
    ├── test_batch_models.py
    ├── test_batch_store.py
    ├── test_main.py
    ├── test_models.py
    ├── test_reconcile.py
    └── test_utils.py

Installation

This module is intended to be vendored or installed directly from source.

Requirements:

  • Python 3.10+
  • boto3 / botocore
  • AWS credentials configured for Bedrock + S3

Quick Start

from bedrockhelper import BedrockHelper

helper = BedrockHelper(
	region_name="ca-central-1",
	rag_model_id="global.anthropic.claude-sonnet-4-5-20250929-v1:0",
	embedding_model_id="amazon.titan-embed-text-v2:0",
)

RAG Usage

Non-streaming

resp = helper.generate_with_rag(
    system_prompt="You are a helpful assistant.",
    context=["Doc 1", "Doc 2"],
    question="Summarize this",
)

print(resp.text)

Streaming

resp = helper.generate_with_rag(
    system_prompt="You are concise.",
    context="Some context",
    question="Stream this",
    stream=True,
)

for chunk in resp.stream:
    print(chunk, end="")

Async Usage

resp = await helper.async_generate_with_rag(
    system_prompt="You are helpful",
    context="Context",
    question="Async answer?",
)

print(resp.text)

Streaming:

async for chunk in await helper.async_stream_generate_with_rag(
    system_prompt="You are helpful",
    context="Context",
    question="Async stream",
):
    print(chunk, end="")

Embeddings

Inline embeddings

records = {
    "a": "Hello world",
    "b": "Another document",
}

resp = helper.embed_texts(records)
print(resp.embeddings["a"])

Batch embeddings

job = helper.embed_texts(
    big_records,
    s3_bucket="my-bucket",
    s3_prefix="bedrock_batch",
    role_arn="arn:aws:iam::123456789012:role/BedrockBatchRole",
)

print(job.job_id)

Wait for completion:

info = helper.wait_for_batch_job(job.job_id)
print(info["status"])

Parse results:

embeddings = helper.parse_batch_embeddings(job.output_s3_uri)

Batch Job Reconciliation

The helper includes a small framework for tracking and reconciling batch jobs.

from bedrockhelper import InMemoryJobStore
from bedrockhelper.batch.reconcile import reconcile_batch_embedding_jobs

store = InMemoryJobStore()
store.add(job)


def process_embeddings(job, embeddings):
	print("Processed", len(embeddings))


reconcile_batch_embedding_jobs(
	helper=helper,
	store=store,
	process_embeddings=process_embeddings,
)

Implementing Your Own JobStore

To persist batch jobs across restarts or multiple workers, implement the JobStore interface.

Redis Example

Use Redis keys per job plus a sorted set for ordering.

Key ideas:

  • Store jobs as JSON
  • Use TTL for terminal jobs
  • Use a sorted set by created_at

This is ideal for horizontally scaled workers.

Postgres Example

Suggested schema:

CREATE TABLE bedrock_batch_jobs (
  job_id TEXT PRIMARY KEY,
  job_name TEXT NOT NULL,
  model_id TEXT NOT NULL,
  input_s3_uri TEXT NOT NULL,
  output_s3_uri TEXT NOT NULL,
  state TEXT NOT NULL,
  created_at TIMESTAMPTZ NOT NULL,
  last_checked_at TIMESTAMPTZ,
  attempts INTEGER NOT NULL DEFAULT 0,
  last_error TEXT,
  bedrock_status TEXT,
  processed_at TIMESTAMPTZ,
  embeddings_count INTEGER,
  debug JSONB NOT NULL DEFAULT '{}'::jsonb
);

This approach is boring, reliable, and production-friendly.


Recommended Patterns

  • Submit batch jobs quickly and return job IDs
  • Reconcile in a background worker or cron
  • Make process_embeddings() idempotent
  • Prefer Postgres if you need observability

License

Internal / project-specific. Adapt as needed.

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

bedrockhelper-1.1.0.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

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

bedrockhelper-1.1.0-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file bedrockhelper-1.1.0.tar.gz.

File metadata

  • Download URL: bedrockhelper-1.1.0.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bedrockhelper-1.1.0.tar.gz
Algorithm Hash digest
SHA256 52c11bb5c8d2372eda09398df26efefecc4d6442daf916ccb6b20d313614c7ad
MD5 7e0e0df0ce2fdcedd6081e655a531f52
BLAKE2b-256 d80a4fd14e9ace6060e8c48420b0c448be3cd552997004b123d62c14f426e00f

See more details on using hashes here.

Provenance

The following attestation bundles were made for bedrockhelper-1.1.0.tar.gz:

Publisher: publish.yml on losboss/bedrockhelper

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

File details

Details for the file bedrockhelper-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: bedrockhelper-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bedrockhelper-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd6c9fcc58f95f3e53654797b252fc0d26541ed14f453ac217f7c3185787fcd6
MD5 bffa719f83103cac8689b2ed90af99e9
BLAKE2b-256 fc72ea7402c456d4fe7792f9f54f66809e416329a63e036ba8c821e062dc5112

See more details on using hashes here.

Provenance

The following attestation bundles were made for bedrockhelper-1.1.0-py3-none-any.whl:

Publisher: publish.yml on losboss/bedrockhelper

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