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_streamwhen 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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52c11bb5c8d2372eda09398df26efefecc4d6442daf916ccb6b20d313614c7ad
|
|
| MD5 |
7e0e0df0ce2fdcedd6081e655a531f52
|
|
| BLAKE2b-256 |
d80a4fd14e9ace6060e8c48420b0c448be3cd552997004b123d62c14f426e00f
|
Provenance
The following attestation bundles were made for bedrockhelper-1.1.0.tar.gz:
Publisher:
publish.yml on losboss/bedrockhelper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bedrockhelper-1.1.0.tar.gz -
Subject digest:
52c11bb5c8d2372eda09398df26efefecc4d6442daf916ccb6b20d313614c7ad - Sigstore transparency entry: 919281598
- Sigstore integration time:
-
Permalink:
losboss/bedrockhelper@3809121f7d71747963776a2e17d3a170d391f28c -
Branch / Tag:
refs/tags/bedrockhelper-v1.1.0 - Owner: https://github.com/losboss
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3809121f7d71747963776a2e17d3a170d391f28c -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd6c9fcc58f95f3e53654797b252fc0d26541ed14f453ac217f7c3185787fcd6
|
|
| MD5 |
bffa719f83103cac8689b2ed90af99e9
|
|
| BLAKE2b-256 |
fc72ea7402c456d4fe7792f9f54f66809e416329a63e036ba8c821e062dc5112
|
Provenance
The following attestation bundles were made for bedrockhelper-1.1.0-py3-none-any.whl:
Publisher:
publish.yml on losboss/bedrockhelper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bedrockhelper-1.1.0-py3-none-any.whl -
Subject digest:
fd6c9fcc58f95f3e53654797b252fc0d26541ed14f453ac217f7c3185787fcd6 - Sigstore transparency entry: 919281611
- Sigstore integration time:
-
Permalink:
losboss/bedrockhelper@3809121f7d71747963776a2e17d3a170d391f28c -
Branch / Tag:
refs/tags/bedrockhelper-v1.1.0 - Owner: https://github.com/losboss
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3809121f7d71747963776a2e17d3a170d391f28c -
Trigger Event:
release
-
Statement type: