-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Bedrock (Titan) embeddings to ai/embed #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michaelmcnees
merged 3 commits into
main
from
claude/office-assistant-mantle-feasibility-n8f4bk
Jul 3, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@mantle/engine": minor | ||
| --- | ||
|
|
||
| Add Bedrock support to the `ai/embed` connector via the Amazon Titan text-embedding models (`amazon.titan-embed-text-v2:0`, `amazon.titan-embed-text-v1`). Set `provider: bedrock` with a `region` and an `aws` credential; the connector uses Bedrock `InvokeModel` (embedding one input per call, reassembled in order) and honors `dimensions` on Titan v2. `serve` wires the AWS region/config into the embeddings connector alongside the chat connector. Cohere Bedrock embedding models are a planned follow-up. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/engine/internal/connector/provider_bedrock_embed_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| "github.com/aws/aws-sdk-go-v2/service/bedrockruntime" | ||
| ) | ||
|
|
||
| // mockBedrockInvokeClient implements BedrockInvokeAPI for testing. | ||
| type mockBedrockInvokeClient struct { | ||
| InvokeFunc func(ctx context.Context, input *bedrockruntime.InvokeModelInput, opts ...func(*bedrockruntime.Options)) (*bedrockruntime.InvokeModelOutput, error) | ||
| } | ||
|
|
||
| func (m *mockBedrockInvokeClient) InvokeModel(ctx context.Context, input *bedrockruntime.InvokeModelInput, opts ...func(*bedrockruntime.Options)) (*bedrockruntime.InvokeModelOutput, error) { | ||
| return m.InvokeFunc(ctx, input, opts...) | ||
| } | ||
|
|
||
| func TestBedrockEmbeddingProvider_Titan(t *testing.T) { | ||
| var calls int | ||
| mock := &mockBedrockInvokeClient{ | ||
| InvokeFunc: func(ctx context.Context, input *bedrockruntime.InvokeModelInput, opts ...func(*bedrockruntime.Options)) (*bedrockruntime.InvokeModelOutput, error) { | ||
| calls++ | ||
| if aws.ToString(input.ModelId) != "amazon.titan-embed-text-v2:0" { | ||
| t.Errorf("ModelId = %q", aws.ToString(input.ModelId)) | ||
| } | ||
| // Verify the request body carries inputText and the requested dimensions. | ||
| var body titanEmbedRequest | ||
| if err := json.Unmarshal(input.Body, &body); err != nil { | ||
| t.Fatalf("unmarshaling request body: %v", err) | ||
| } | ||
| if body.InputText == "" { | ||
| t.Error("inputText is empty") | ||
| } | ||
| if body.Dimensions != 256 { | ||
| t.Errorf("dimensions = %d, want 256", body.Dimensions) | ||
| } | ||
| resp, _ := json.Marshal(titanEmbedResponse{ | ||
| Embedding: []float64{0.5, -0.25}, | ||
| InputTextTokenCount: 4, | ||
| }) | ||
| return &bedrockruntime.InvokeModelOutput{Body: resp}, nil | ||
| }, | ||
| } | ||
|
|
||
| p := &BedrockEmbeddingProvider{Client: mock} | ||
| resp, err := p.Embeddings(context.Background(), &EmbeddingRequest{ | ||
| Model: "amazon.titan-embed-text-v2:0", | ||
| Inputs: []string{"one", "two", "three"}, | ||
| Dimensions: 256, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Embeddings() error: %v", err) | ||
| } | ||
| // Titan embeds one input per call. | ||
| if calls != 3 { | ||
| t.Errorf("InvokeModel calls = %d, want 3", calls) | ||
| } | ||
| if len(resp.Embeddings) != 3 { | ||
| t.Fatalf("embeddings = %d, want 3", len(resp.Embeddings)) | ||
| } | ||
| if resp.Embeddings[0][0] != 0.5 || resp.Embeddings[0][1] != -0.25 { | ||
| t.Errorf("embedding[0] = %v", resp.Embeddings[0]) | ||
| } | ||
| if resp.Usage.TotalTokens != 12 { // 4 per input * 3 | ||
| t.Errorf("TotalTokens = %d, want 12", resp.Usage.TotalTokens) | ||
| } | ||
| } | ||
|
|
||
| func TestBedrockEmbeddingProvider_V1IgnoresDimensions(t *testing.T) { | ||
| mock := &mockBedrockInvokeClient{ | ||
| InvokeFunc: func(ctx context.Context, input *bedrockruntime.InvokeModelInput, opts ...func(*bedrockruntime.Options)) (*bedrockruntime.InvokeModelOutput, error) { | ||
| var body titanEmbedRequest | ||
| if err := json.Unmarshal(input.Body, &body); err != nil { | ||
| t.Fatalf("unmarshaling request body: %v", err) | ||
| } | ||
| // Titan v1 must not receive dimensions even when requested. | ||
| if body.Dimensions != 0 { | ||
| t.Errorf("dimensions = %d, want 0 (omitted) for titan v1", body.Dimensions) | ||
| } | ||
| resp, _ := json.Marshal(titanEmbedResponse{Embedding: []float64{1}, InputTextTokenCount: 1}) | ||
| return &bedrockruntime.InvokeModelOutput{Body: resp}, nil | ||
| }, | ||
| } | ||
| p := &BedrockEmbeddingProvider{Client: mock} | ||
| if _, err := p.Embeddings(context.Background(), &EmbeddingRequest{ | ||
| Model: "amazon.titan-embed-text-v1", | ||
| Inputs: []string{"x"}, | ||
| Dimensions: 512, | ||
| }); err != nil { | ||
| t.Fatalf("Embeddings() error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestBedrockEmbeddingProvider_V2InvalidDimensions(t *testing.T) { | ||
| mock := &mockBedrockInvokeClient{ | ||
| InvokeFunc: func(ctx context.Context, input *bedrockruntime.InvokeModelInput, opts ...func(*bedrockruntime.Options)) (*bedrockruntime.InvokeModelOutput, error) { | ||
| t.Fatal("InvokeModel should not be called for invalid dimensions") | ||
| return nil, nil | ||
| }, | ||
| } | ||
| p := &BedrockEmbeddingProvider{Client: mock} | ||
| _, err := p.Embeddings(context.Background(), &EmbeddingRequest{ | ||
| Model: "amazon.titan-embed-text-v2:0", | ||
| Inputs: []string{"x"}, | ||
| Dimensions: 999, // only 256/512/1024 are valid | ||
| }) | ||
| if err == nil { | ||
| t.Error("expected error for unsupported Titan v2 dimensions") | ||
| } | ||
| } | ||
|
|
||
| func TestBedrockEmbeddingProvider_UnsupportedModel(t *testing.T) { | ||
| p := &BedrockEmbeddingProvider{Client: &mockBedrockInvokeClient{}} | ||
| _, err := p.Embeddings(context.Background(), &EmbeddingRequest{ | ||
| Model: "cohere.embed-english-v3", | ||
| Inputs: []string{"x"}, | ||
| }) | ||
| if err == nil { | ||
| t.Error("expected error for unsupported (non-Titan) Bedrock embedding model") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.