Skip to content

feat: multimodal embeddings for bedrock titan and cohere#1130

Merged
VisargD merged 5 commits into
Portkey-AI:mainfrom
narengogi:feat/bedrock-multimodal-embeddings
Jul 2, 2025
Merged

feat: multimodal embeddings for bedrock titan and cohere#1130
VisargD merged 5 commits into
Portkey-AI:mainfrom
narengogi:feat/bedrock-multimodal-embeddings

Conversation

@narengogi

Copy link
Copy Markdown
Member

This PR adds support for multimodal embeddings in compliance with the portkey signature:

example requests:

  1. Titan text embedding:
curl --location 'http://localhost:8787/v1/embeddings' \
--header 'x-portkey-provider: bedrock' \
--header 'Content-Type: application/json' \
--header 'x-portkey-aws-access-key-id: ID' \
--header 'x-portkey-aws-secret-access-key: KEY' \
--header 'x-portkey-aws-region: us-east-1' \
--data '{
    "model": "amazon.titan-embed-text-v2:0",
    "input": "Hello this is a test",
    "normalize": false
}'
  1. Titan image embedding:
curl --location 'http://localhost:8787/v1/embeddings' \
--header 'x-portkey-provider: bedrock' \
--header 'Content-Type: application/json' \
--header 'x-portkey-aws-access-key-id: ID' \
--header 'x-portkey-aws-secret-access-key: KEY' \
--header 'x-portkey-aws-region: us-east-1' \
--data '{
    "model": "amazon.titan-embed-image-v1",
    "dimensions": 256,
    "input": [
        {
            "text": "this is the caption of the image",
            "image": {
                "base64": "UklGRkac....."
            }
        }
    ]
}'
  1. Cohere text embedding:
curl --location 'http://localhost:8787/v1/embeddings' \
--header 'x-portkey-provider: bedrock' \
--header 'Content-Type: application/json' \
--header 'x-portkey-aws-access-key-id: ID' \
--header 'x-portkey-aws-secret-access-key: KEY' \
--header 'x-portkey-aws-region: us-east-1' \
--data '{
    "model": "cohere.embed-english-v3",
    "input": ["Hello this is a test", "skibidi"],
    "input_type": "classification"
}'
  1. Cohere Multimodal embedding:
curl --location 'http://localhost:8787/v1/embeddings' \
--header 'x-portkey-provider: bedrock' \
--header 'Content-Type: application/json' \
--header 'x-portkey-aws-access-key-id: ID' \
--header 'x-portkey-aws-secret-access-key: KEY' \
--header 'x-portkey-aws-region: us-east-1' \
--data '{
    "model": "cohere.embed-english-v3",
    "input_type": "image",
    "dimensions": 256,
    "input": [
        {
            
            "image": {
                "base64": "UklGRkac....."
            }
        }
    ]
}'

@narengogi narengogi requested review from VisargD and sk-portkey June 10, 2025 12:15

@matterai-app matterai-app Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds multimodal embedding support for Bedrock Titan and Cohere providers, allowing both text and image inputs. The implementation is well-structured, but I've identified a few improvements that could enhance error handling and type safety.

@Portkey-AI Portkey-AI deleted a comment from matterai-app Bot Jun 10, 2025
@Portkey-AI Portkey-AI deleted a comment from matterai-app Bot Jun 10, 2025
@matterai-app

matterai-app Bot commented Jun 10, 2025

Copy link
Copy Markdown
Contributor

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Comment thread src/providers/cohere/embed.ts
@narengogi narengogi requested a review from VisargD July 1, 2025 08:17
@matterai-app

matterai-app Bot commented Jul 1, 2025

Copy link
Copy Markdown
Contributor

Code Quality new feature

Description

# Summary By MatterAI MatterAI logo

🔄 What Changed

This Pull Request introduces support for multimodal embeddings for Bedrock Titan and Cohere models. The core changes involve updating the provider configuration (BedrockCohereEmbedConfig, BedrockTitanEmbedConfig, CohereEmbedConfig) to handle diverse input types, including text and base64 encoded images. The input parameter now accepts a more flexible structure, allowing for single strings, arrays of strings, or arrays of objects containing text or image (base64) properties. Additionally, new parameters like dimensions and normalize are introduced for Bedrock Titan, and encoding_format is standardized across providers.

🔍 Impact of the Change

This change significantly enhances the capabilities of the Portkey API by enabling users to generate embeddings from both text and image inputs using Bedrock Titan and Cohere models. It aligns the API with multimodal embedding standards, providing greater flexibility and broader model support. The updated input parsing logic ensures compatibility with various client request formats.

📁 Total Files Changed

2 files were changed in this pull request.

🧪 Test Added

Manual testing was performed using curl commands, demonstrating successful text and image embeddings for Bedrock Titan and Cohere models. Specific examples include:

  1. Titan text embedding: Request with model: "amazon.titan-embed-text-v2:0" and input: "Hello this is a test".
  2. Titan image embedding: Request with model: "amazon.titan-embed-image-v1", dimensions: 256, and base64 image input.
  3. Cohere text embedding: Request with model: "cohere.embed-english-v3", text input, and input_type: "classification".
  4. Cohere Multimodal embedding: Request with model: "cohere.embed-english-v3", input_type: "image", dimensions: 256, and base64 image input.

🔒Security Vulnerabilities

No new security vulnerabilities were detected in the provided code changes.

Motivation

This PR adds support for multimodal embeddings in compliance with the Portkey signature, expanding the functionality of the embeddings API to handle both text and image inputs for Bedrock Titan and Cohere models.

Type of Change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Manual Testing

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

N/A

Tip

Quality Recommendations

  1. Implement more robust input validation within the transform functions for texts and images in BedrockCohereEmbedConfig and CohereEmbedConfig. Currently, if params.input is an empty array or contains only unsupported types, both texts and images could become undefined due to required: false, potentially leading to an invalid request to the underlying provider. Ensure at least one valid input type (text or image) is present if input is provided.

  2. Consider creating a reusable utility function for the encoding_format transformation, as the logic is identical across BedrockCohereEmbedConfig and BedrockTitanEmbedConfig (and CohereEmbedConfig). This would improve maintainability and reduce code duplication.

  3. Add comprehensive unit tests for the new transformation logic, covering various input scenarios including single string, array of strings, array of objects with text, array of objects with image, mixed types, empty arrays, and invalid input formats to ensure correct parameter mapping and error handling.

Sequence Diagram

sequenceDiagram
    actor User
    participant PortkeyAPI as Portkey API
    participant BedrockCohereConfig as BedrockCohereEmbedConfig
    participant BedrockTitanConfig as BedrockTitanEmbedConfig
    participant CohereConfig as CohereEmbedConfig
    participant BedrockAPI as Bedrock API
    participant CohereAPI as Cohere API

    User->>PortkeyAPI: POST /v1/embeddings (model, input, dimensions, input_type, encoding_format, normalize)
    PortkeyAPI->>PortkeyAPI: Determine Provider (Bedrock/Cohere)

    alt Bedrock Cohere Model
        PortkeyAPI->>BedrockCohereConfig: Apply config transformations (EmbedParams)
        BedrockCohereConfig->>BedrockCohereConfig: Transform input: (string | object[]) -> texts (string[]) | images (base64 string[])
        BedrockCohereConfig->>BedrockCohereConfig: Transform encoding_format: (string | string[]) -> embedding_types (string[])
        BedrockCohereConfig-->>PortkeyAPI: Transformed Bedrock Cohere Params (texts, images, input_type, truncate, embedding_types)
        PortkeyAPI->>BedrockAPI: Call Bedrock Cohere Embed API
    else Bedrock Titan Model
        PortkeyAPI->>BedrockTitanConfig: Apply config transformations (EmbedParams)
        BedrockTitanConfig->>BedrockTitanConfig: Transform input: (string | object[]) -> inputText (string) | inputImage (base64 string)
        BedrockTitanConfig->>BedrockTitanConfig: Transform dimensions: (number) -> dimensions (number) | embeddingConfig (object)
        BedrockTitanConfig->>BedrockTitanConfig: Transform encoding_format: (string | string[]) -> embeddingTypes (string[])
        BedrockTitanConfig-->>PortkeyAPI: Transformed Bedrock Titan Params (inputText, inputImage, dimensions, embeddingConfig, encoding_format, normalize)
        PortkeyAPI->>BedrockAPI: Call Bedrock Titan Embed API
    else Cohere Model
        PortkeyAPI->>CohereConfig: Apply config transformations (EmbedParams)
        CohereConfig->>CohereConfig: Transform input: (string | object[]) -> texts (string[]) | images (base64 string[])
        CohereConfig->>CohereConfig: Transform encoding_format: (string | string[]) -> embedding_types (string[])
        CohereConfig-->>PortkeyAPI: Transformed Cohere Params (texts, images, input_type, truncate, embedding_types)
        PortkeyAPI->>CohereAPI: Call Cohere Embed API
    end

    BedrockAPI-->>PortkeyAPI: Embed Response
    CohereAPI-->>PortkeyAPI: Embed Response
    PortkeyAPI-->>User: EmbedResponse
Loading

@matterai-app

matterai-app Bot commented Jul 1, 2025

Copy link
Copy Markdown
Contributor

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@VisargD VisargD merged commit 72ee387 into Portkey-AI:main Jul 2, 2025
2 checks passed
@matterai-app

matterai-app Bot commented Jul 2, 2025

Copy link
Copy Markdown
Contributor

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants