-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add bedrock-titan-embedding skill for AWS Bedrock Titan V2 #69
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
dpomian
merged 2 commits into
AmadeusITGroup:main
from
MarouaneBenabdelkader:feat/bedrock-titan-embedding
Apr 28, 2026
Merged
Changes from all commits
Commits
Show all changes
2 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
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
83 changes: 83 additions & 0 deletions
83
src/docs2vecs/subcommands/indexer/skills/bedrock_titan_embedding_skill.py
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,83 @@ | ||
| import json | ||
| import time | ||
| from typing import List, Optional | ||
|
|
||
| import boto3 | ||
|
|
||
| from docs2vecs.subcommands.indexer.config.config import Config | ||
| from docs2vecs.subcommands.indexer.document.document import Document | ||
| from docs2vecs.subcommands.indexer.skills.skill import IndexerSkill | ||
|
|
||
|
|
||
| class BedrockTitanEmbeddingSkill(IndexerSkill): | ||
| DEFAULT_MODEL_ID = "amazon.titan-embed-text-v2:0" | ||
| DEFAULT_DIMENSIONS = 1024 | ||
| DEFAULT_MAX_RETRIES = 3 | ||
| DEFAULT_RETRY_BACKOFF = 2 | ||
|
|
||
| def __init__(self, config: dict, global_config: Config): | ||
| super().__init__(config, global_config) | ||
| self._model_id = self._config.get("model_id", self.DEFAULT_MODEL_ID) | ||
| self._dimensions = self._config.get("dimensions", self.DEFAULT_DIMENSIONS) | ||
| self._normalize = self._config.get("normalize", True) | ||
| self._max_retries = self._config.get("max_retries", self.DEFAULT_MAX_RETRIES) | ||
| self._retry_backoff = self._config.get("retry_backoff", self.DEFAULT_RETRY_BACKOFF) | ||
| self._client = boto3.client( | ||
| "bedrock-runtime", | ||
| region_name=self._config.get("region"), | ||
| ) | ||
|
|
||
| def _embed_text(self, content: str, chunk_id=None): | ||
| self.logger.debug( | ||
| f"Requesting Bedrock embedding for chunk_id={chunk_id}, content_length={len(content)}" | ||
| ) | ||
| body = json.dumps( | ||
| { | ||
| "inputText": content, | ||
| "dimensions": self._dimensions, | ||
| "normalize": self._normalize, | ||
| } | ||
| ) | ||
| for attempt in range(self._max_retries): | ||
| try: | ||
| resp = self._client.invoke_model( | ||
| modelId=self._model_id, | ||
| body=body, | ||
| contentType="application/json", | ||
| accept="application/json", | ||
| ) | ||
| embedding = json.loads(resp["body"].read())["embedding"] | ||
| self.logger.debug( | ||
| f"Successfully received embedding for chunk_id={chunk_id}, embedding_dim={len(embedding) if embedding else 0}" | ||
| ) | ||
| return embedding | ||
| except Exception as exc: | ||
| if attempt == self._max_retries - 1: | ||
| raise | ||
| wait = self._retry_backoff * (attempt + 1) | ||
| self.logger.warning( | ||
| f"Bedrock call failed (attempt {attempt + 1}/{self._max_retries}): {exc} - retrying in {wait}s" | ||
| ) | ||
| time.sleep(wait) | ||
|
|
||
| def run(self, input: Optional[List[Document]] = None) -> Optional[List[Document]]: | ||
| self.logger.info( | ||
| f"Running Bedrock Titan Embedding Skill with model_id: {self._model_id}..." | ||
| ) | ||
|
|
||
| docs_count = len(input) | ||
| chunks_count = sum(len(doc.chunks) for doc in input) | ||
|
|
||
| self.logger.info( | ||
| f"Processing a total of documents: {docs_count}. Total number of chunks: {chunks_count}" | ||
| ) | ||
|
|
||
| for doc in input: | ||
| self.logger.debug(f"Processing document: {doc.filename}") | ||
| for chunk in doc.chunks: | ||
| self.logger.debug(f"Creating embedding for chunk: {chunk.chunk_id}") | ||
| chunk.embedding = [] if not chunk.content else self._embed_text( | ||
| chunk.content, chunk_id=chunk.chunk_id | ||
| ) | ||
|
|
||
| return input | ||
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.
The retry loop retries on any Exception (including JSON parsing/KeyError, config errors like missing region/credentials, and programmer errors), which can waste time and hide the real failure mode. Consider catching botocore exceptions (e.g., ClientError/BotoCoreError) and only retrying transient failures (throttling, timeouts, 5xx), while surfacing non-retryable errors immediately with a clearer message.