

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Amazon Titan Embeddings G1 - Text
<a name="model-parameters-titan-embed-text"></a>

Titan Embeddings G1 - Text 不支援使用推論參數。以下各節詳細說明請求和回應格式，並提供程式碼範例。

**Topics**
+ [請求與回應](#model-parameters-titan-embed-text-request-response)
+ [範例程式碼](#api-inference-examples-titan-embed-text)

## 請求與回應
<a name="model-parameters-titan-embed-text-request-response"></a>

請求內文在 [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) 請求的 `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`。
+ inputTextTokenCount – 輸入中的字符數量。
+ embeddingsByType – 嵌入清單的字典或映射。根據輸入，列出「浮點數」、「二進位」或兩者。
  + 範例：`"embeddingsByType": {"binary": [int,..], "float": [float,...]}`
  + 此欄位一律會出現。即使您未在輸入中指定 `embeddingTypes`，仍然會有「浮點數」。範例：`"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** – 輸入中的字符數量。

------

## 範例程式碼
<a name="api-inference-examples-titan-embed-text"></a>

下列範例示範如何呼叫 Amazon Titan Embeddings 模型來產生嵌入。選取對應至您正在使用之模型的索引標籤：

------
#### [ 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 an embedding 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_embedding(model_id, body):
    """
    Generate an embedding with the vector representation of 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 an embedding 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_embedding(model_id, body)

        print(f"Generated an embedding: {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 an embedding with Amazon Titan Embeddings G1 - Text model {model_id}.")


if __name__ == "__main__":
    main()
```

------
#### [ Amazon Titan 文本嵌入 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 an embedding 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_embedding(model_id, body):
    """
    Generate an embedding with the vector representation of 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 an embedding 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_embedding(model_id, body)

        print(f"Generated an embedding: {response['embeddingsByType']['binary']}") # returns binary embedding
        print(f"Input text: {input_text}")
        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 an embedding with Amazon Titan Text Embeddings V2 model {model_id}.")


if __name__ == "__main__":
    main()
```

------