Files
Xuanwo d605943e9b feat(python): add Bedrock image embeddings (#5190)
Continues #2320. Thank you @qingyuan18 for starting this work!

## Summary

Adds the `bedrock-image` (`BedrockImage`) embedding function for AWS
Bedrock image embeddings, exports it from `lancedb.embeddings`, updates
the AWS Bedrock docs, and extracts shared Bedrock runtime client setup
into `_BedrockEmbeddingFunction`.

- **Models**:
- `amazon.titan-embed-image-v1` (default, 1024 dims): PNG and JPEG
source images.
- `cohere.embed-v4:0` (1536 dims): PNG, JPEG, WebP, and GIF source
images (`search_document` for ingestion, `search_query` for queries).
Cohere v3 models remain text-only under `bedrock-text`.
- **Input & search**: Source columns accept encoded image `bytes` or
local file paths (`str`), with format detected from decoded bytes via
Pillow and `None` entries yielding `None` embeddings. Queries accept
text (`str`) or `PIL.Image.Image` (converted to RGB PNG);
`AsyncTable.search` requires `query_type="vector"` for Pillow image
queries.
- **Credentials**: `bedrock-image` uses `boto3.Session()`'s default
credential chain (e.g., `AWS_PROFILE` or the application's IAM role) and
rejects `profile_name`, `assumed_role`, and `role_session_name` on both
`create()` and metadata restore. `bedrock-text` retains its released
`profile_name`, `assumed_role`, and `role_session_name` settings.

## Validation

- Local Python tests cover the public `get_registry()` API, Arrow table
creation and ingestion (`bytes` and local path sources), table reopen
from persisted Arrow metadata, sync text and `PIL.Image` search, async
`query_type="vector"` `PIL.Image` search, credential-option rejection on
`bedrock-image` create/restore, and `bedrock-text` credential-setting
restoration and Cohere v3 text requests.
- SDK requests and payloads were validated at the `boto3`/`botocore`
`bedrock-runtime` client boundary using `botocore.stub.Stubber`. No live
AWS Bedrock service calls were run, so live IAM/STS credential
resolution, region model availability, and Bedrock service-side
validation are not covered.
2026-11-24 20:12:04 +08:00

86 lines
3.3 KiB
Plaintext

---
title: AWS Bedrock
sidebarTitle: AWS
---
import { PyEmbeddingAwsUsage, PyEmbeddingAwsImageUsage } from '/snippets/integrations.mdx';
Use `bedrock-text` to embed text or `bedrock-image` to search images by text or image.
## Prerequisites {#prerequisites}
Install `boto3` for either embedding function. Install `pillow` as well for image embeddings.
```bash
pip install boto3 pillow
```
Configure AWS credentials with permission to invoke your model in the AWS Region where it is available. Use the AWS CLI to configure credentials and set the session token if you use temporary credentials.
<CodeGroup>
```bash Bash icon="terminal"
aws configure
aws configure set aws_session_token "<your_session_token>"
```
</CodeGroup>
Verify the active credentials with the AWS CLI.
<CodeGroup>
```bash Bash icon="terminal"
aws sts get-caller-identity
```
</CodeGroup>
## Text embeddings {#text-embeddings}
The `bedrock-text` registry entry supports these model IDs.
| Model ID | Dimensions |
| --- | --- |
| `amazon.titan-embed-text-v1` (default) | 1536 |
| `amazon.titan-embed-text-v2:0` | 1024 |
| `cohere.embed-english-v3` | 1024 |
| `cohere.embed-multilingual-v3` | 1024 |
For Cohere text models, set `source_input_type` or `query_input_type` in `create()` to override `search_document` or `search_query`. You can also pass `profile_name`, `assumed_role`, and `role_session_name` to `bedrock-text`.
<CodeGroup>
<CodeBlock filename="Python" language="Python" icon="python">
{PyEmbeddingAwsUsage}
</CodeBlock>
</CodeGroup>
## Image embeddings {#image-embeddings}
LanceDB embeds images when you add them to a table. You can search the table with text or a Pillow image. `bedrock-image` uses boto3's default credential chain. Select a profile with `AWS_PROFILE` in the application's environment or run the application with an IAM role.
| Model ID | Dimensions | Source image formats |
| --- | --- | --- |
| `amazon.titan-embed-image-v1` (default) | 1024 | PNG, JPEG |
| `cohere.embed-v4:0` | 1536 | PNG, JPEG, WebP, GIF |
Choose a binary source column (`bytes`) for encoded image bytes or a string source column (`str`) for file paths readable by the process that adds rows. Pillow detects the source format from the file contents. If your table has nullable images and vectors, pass `on_bad_vectors="null"` to keep rows without an image. To use Cohere Embed v4, pass `name="cohere.embed-v4:0"` to `create()`.
<CodeGroup>
<CodeBlock filename="Python" language="Python" icon="python">
{PyEmbeddingAwsImageUsage}
</CodeBlock>
</CodeGroup>
A query string is embedded as text. To search asynchronously with a Pillow image, pass `query_type="vector"`. Run the following code in an async context with the `db_path` and `query_image` from the example.
```python
async_db = await lancedb.connect_async(db_path)
async_table = await async_db.open_table("photos")
query = await async_table.search(query_image, query_type="vector")
results = await query.limit(1).to_list()
```
## AWS settings {#aws-settings}
Pass these options to `create()` with either registry entry.
| Parameter | Default | Description |
| --- | --- | --- |
| `name` | Varies by registry entry | Model ID to invoke. |
| `region` | `"us-east-1"` | AWS Region where the model is available. |
| `max_retries` | `7` | Maximum retries. Image embeddings retry individual Bedrock requests. Set to `0` to disable retries. |