Skip to content
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

support model embedding types in bedrock and cohere post process function #3262

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

zane-neo
Copy link
Collaborator

@zane-neo zane-neo commented Dec 9, 2024

Description

BedRock titan model V2 and Cohere V2 model now supports multiple embedding types in their API, which means you can specify different embedding types as a list in the request, and model generates different results based on embedding types in response, e.g.:

import cohere
import requests
import base64

co = cohere.ClientV2()

image = requests.get("https://cohere.com/favicon-32x32.png")
stringified_buffer = base64.b64encode(image.content).decode("utf-8")
content_type = image.headers["Content-Type"]
image_base64 = f"data:{content_type};base64,{stringified_buffer}"

response = co.embed(
    model="embed-english-v3.0",
    input_type="image",
    embedding_types=["float", "binary", "int8"],
    images=[image_base64],
)

print(response)

Response:

{
  "id": "5807ee2e-0cda-445a-9ec8-864c60a06606",
  "embeddings": {
    "float": [
      [
        -0.007247925,
        -0.041229248,
        -0.023223877,
       ......
      ]
    ],
    "binary": [
        [
            12,
            -13,
            14
        ]
    ],
    "int8": [
        [
            19,
            20,
            21
        ]
    ]
  },
  "texts": [],
  "images": [
    {
      "width": 400,
      "height": 400,
      "format": "jpeg",
      "bit_depth": 24
    }
  ],
  "meta": {
    "api_version": {
      "version": "2"
    },
    "billed_units": {
      "images": 1
    }
  }
}

The current bedrock and cohere post process function has default response filter to $.embedding and $.embeddings respectively and the extracted results are both List<List<Float>> type, after they support embedding types, the response structure is different and the default response filter extracts List<List<Float>> and Map<String, List<List<Number>> type separately. This PR is to add support to user who wants to extract different types of results in model response, and this can be classified into several categories:

BedRock (The post process function applies for both v1 and v2 model)

  1. The default case(response filter not set) is exactly same as before to maintain BWC.
  2. When user specified the response filter to $.embeddingsByType, the response will be captured into dataAsMap in ModelTensor, and user can get all embedding types result.
  3. When user specified the response filter to $.embeddingsByType.{concrete_embedding_type}, the response will be captured similar to default case to the data field in ModelTensor.

Cohere V2 (A new v2 post process function is added to v2 model)

  1. The default case(response filter not set) is exactly same between v1 and v2.
  2. When user specified the response filter to $.embeddings, the response will be captured into dataAsMap in ModelTensor, and user can get all embedding types result.
  3. When user specified the response filter to $.embeddings.{concrete_embedding_type}, the response will be captured similar to default case to the data field in ModelTensor.

Related Issues

#3093

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: zane-neo <[email protected]>
@zane-neo zane-neo temporarily deployed to ml-commons-cicd-env December 9, 2024 05:13 — with GitHub Actions Inactive
@zane-neo zane-neo temporarily deployed to ml-commons-cicd-env December 9, 2024 05:13 — with GitHub Actions Inactive
@zane-neo zane-neo temporarily deployed to ml-commons-cicd-env December 9, 2024 06:11 — with GitHub Actions Inactive
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.

1 participant