-
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 1 commit
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
81 changes: 81 additions & 0 deletions
81
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,81 @@ | ||
| 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_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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When callers use the documented
amazon.titan-embed-text-v1model and passdimensions(for example by reusing a shared embedding config), this branch serializesdimensionsinto the InvokeModel body even though Titan G1/Text v1 only acceptsinputText. That makes the new Bedrock path fail instead of returning the default v1 embedding; either reject dimensions for v1 up front or only set this field foramazon.titan-embed-text-v2:0.Useful? React with 👍 / 👎.