Amazon Titan 嵌入文本 - Amazon Bedrock

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

Amazon Titan 嵌入文本

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

请求和回应

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

V2 Request

inputText 参数为必填项。归一化和尺寸参数是可选的。

  • inputText — 输入要转换为嵌入的文本。

  • normalize —(可选)指示是否对输出嵌入进行标准化的标志。默认值为 true。

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

  • embeddingTypes —(可选)接受包含 “浮点数”、“二进制” 或两者的列表。默认值为 float

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

字段如下所述。

  • embedding — 一个数组,它表示您提供的输入的嵌入向量。这将永远是类型float

  • inputTextToken计数-输入中的令牌数量。

  • embeddingsByType — 嵌入列表的字典或地图。根据输入,列出 “浮点数”、“二进制” 或两者。

    • 例如:"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 — 一个数组,它表示您提供的输入的嵌入向量。

  • inputTextToken计数-输入中的令牌数量。

代码示例

以下示例说明如何调用 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仅包含该embedding字段,则该字段不在响应中binary

# 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()