Amazon Titan 文本嵌入 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Amazon Titan 文本嵌入

Titan Embeddings G1 - Text 不支持使用推理参数。下文详细介绍了请求和响应格式,并提供了一个代码示例。

请求和响应

请求正文在 InvokeModel 请求的 body 字段中传递。

V2 Request

inputText 为必需参数。normalize 和 dimensions 为可选参数。

  • inputText – 输入要转换为嵌入向量的文本。

  • normalize –(可选)标记,用于表示是否对输出嵌入进行规范化。默认值为 true。

  • dimensions –(可选)输出嵌入应具有的维度数。接受以下值:1024(默认)、512、256。

  • embeddingTypes –(可选)接受包含“float”、“binary”或同时包含两者的列表。默认值为 float

{ "inputText": string, "dimensions": int, "normalize": boolean, "embeddingTypes": list }
V2 Response

字段如下所述。

  • embedding – 一个数组,表示您提供的输入的嵌入向量。其类型始终为 float

  • inputTextTokenCount – 输入中的词元数量。

  • embeddingsByType – 嵌入列表的字典或映射。根据输入的不同会列出“float”、“binary”或同时列出两者。

    • 例如:"embeddingsByType": {"binary": [int,..], "float": [float,...]}

    • 该字段将始终显示。即使您没有在输入中指定 embeddingTypes,也会显示“float”。例如:"embeddingsByType": {"float": [float,...]}

{ "embedding": [float, float, ...], "inputTextTokenCount": int, "embeddingsByType": {"binary": [int,..], "float": [float,...]} }
G1 Request

唯一可用的字段是 inputText,您可以在其中输入要转换为嵌入的文本。

{ "inputText": string }
G1 Response

响应的 body 包含以下字段。

{ "embedding": [float, float, ...], "inputTextTokenCount": int }

字段如下所述。

  • embedding – 一个数组,表示您提供的输入的嵌入向量。

  • inputTextTokenCount – 输入中的词元数量。

代码示例

以下示例展示了如何调用 Amazon Titan 嵌入模型来生成嵌入。选择与您要使用的模型相对应的选项卡:

Amazon Titan Embeddings G1 - Text
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate embeddings with the Amazon Titan Embeddings G1 - Text model (on demand). """ import json import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_embeddings(model_id, body): """ Generate a vector of embeddings for a text input using Amazon Titan Embeddings G1 - Text on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: response (JSON): The embedding created by the model and the number of input tokens. """ logger.info("Generating embeddings with Amazon Titan Embeddings G1 - Text model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get('body').read()) return response_body def main(): """ Entrypoint for Amazon Titan Embeddings G1 - Text example. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = "amazon.titan-embed-text-v1" input_text = "What are the different services that you offer?" # Create request body. body = json.dumps({ "inputText": input_text, }) try: response = generate_embeddings(model_id, body) print(f"Generated embeddings: {response['embedding']}") print(f"Input Token count: {response['inputTextTokenCount']}") except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) else: print(f"Finished generating embeddings with Amazon Titan Embeddings G1 - Text model {model_id}.") if __name__ == "__main__": main()
Amazon Titan Text Embeddings V2

使用 Titan Text Embeddings V2 时,如果 embeddingTypes 仅包含 binary,则 embedding 字段不会出现在响应中。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate embeddings with the Amazon Titan Text Embeddings V2 Model """ import json import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_embeddings(model_id, body): """ Generate a vector of embeddings for a text input using Amazon Titan Text Embeddings G1 on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: response (JSON): The embedding created by the model and the number of input tokens. """ logger.info("Generating embeddings with Amazon Titan Text Embeddings V2 model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get('body').read()) return response_body def main(): """ Entrypoint for Amazon Titan Embeddings V2 - Text example. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = "amazon.titan-embed-text-v2:0" input_text = "What are the different services that you offer?" # Create request body. body = json.dumps({ "inputText": input_text, "embeddingTypes": ["binary"] }) try: response = generate_embeddings(model_id, body) print(f"Generated embeddings: {response['embeddingByTypes']['binary']}") # returns binary embedding # print(f"Generated embeddings: {response['embedding']}") NOTE:"embedding" field is not in "response". print(f"Input Token count: {response['inputTextTokenCount']}") except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) else: print(f"Finished generating embeddings with Amazon Titan Text Embeddings V2 model {model_id}.") if __name__ == "__main__": main()