

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

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

Titan Embeddings G1 - Text non supporta l’utilizzo di parametri di inferenza. Le sezioni seguenti descrivono in dettaglio i formati di richiesta e risposta e forniscono un esempio di codice.

**Topics**
+ [Richiesta e risposta](#model-parameters-titan-embed-text-request-response)
+ [Codice di esempio](#api-inference-examples-titan-embed-text)

## Richiesta e risposta
<a name="model-parameters-titan-embed-text-request-response"></a>

Il corpo della richiesta viene passato nel campo `body` di una richiesta [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html). 

------
#### [ V2 Request ]

Il parametro inputText è obbligatorio. I parametri normalize e dimensions sono facoltativi.
+ inputText: inserire il testo da convertire in un embedding.
+ normalize (opzionale): flag che indica se normalizzare o meno l’embedding dell’output. Il valore predefinito è true.
+ dimensions (opzionale): numero di dimensioni che deve avere l’embedding dell’output. Sono accettati i seguenti valori: 1.024 (impostazione predefinita), 512, 256.
+ embeddingTypes (opzionale): accetta un elenco contenente “float”, “binary” o entrambi. L'impostazione predefinita è `float`. 

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

------
#### [ V2 Response ]

I campi sono descritti di seguito.
+ embedding: array che rappresenta il vettore di embedding dell’input fornito. È sempre del tipo `float`.
+ inputTextTokenCount: numero di token nell’input.
+ embeddingsByType: dizionario o una mappa dell’elenco di embedding. Dipende dall’input, contiene “float”, “binary” o entrambi.
  + Ad esempio: `"embeddingsByType": {"binary": [int,..], "float": [float,...]}`
  + Questo campo è sempre visualizzato. Anche se non specifichi `embeddingTypes` nell’input, è comunque presente “float”. Ad esempio: `"embeddingsByType": {"float": [float,...]}`

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

------
#### [ G1 Request ]

L’unico campo disponibile è `inputText`, in cui puoi includere testo da convertire in un embedding.

```
{
    "inputText": string
}
```

------
#### [ G1 Response ]

L’elemento `body` della risposta contiene i seguenti campi.

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

I campi sono descritti di seguito.
+ **embedding**: array che rappresenta il vettore di embedding dell’input fornito.
+ **inputTextTokenCount**: numero di token nell’input.

------

## Codice di esempio
<a name="api-inference-examples-titan-embed-text"></a>

Gli esempi seguenti mostrano come chiamare i modelli Amazon Titan Embeddings per generare embedding. Seleziona la scheda corrispondente al modello in uso:

------
#### [ 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 ]

Durante l’utilizzo di Titan Text Embeddings V2, il campo `embedding` non è presente nella risposta se `embeddingTypes` contiene solo `binary`. 

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

------