

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 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', '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** - 입력의 토큰 수입니다.

------

## 예제 코드
<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 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 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()
```

------