As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.
Amazon Titan Embeddings G1 - Text
Titan Embeddings G1 - Textnão suporta o uso de parâmetros de inferência. As seções a seguir detalham os formatos de solicitação e de resposta e fornecem um exemplo de código.
Solicitação e reposta
O corpo da solicitação é passado no body
campo de uma InvokeModelsolicitação.
- V2 Request
-
O parâmetro inputText é necessário. Os parâmetros de normalização e de dimensões são opcionais.
-
InputText — Insira o texto a ser convertido em uma incorporação.
-
normalize — (opcional) Sinalizador que indica se a incorporação de saída deve ou não ser normalizada. O valor padrão é verdadeiro.
-
dimensões — (opcional) O número de dimensões que a incorporação de saída deve ter. Os seguintes valores são aceitos: 1.024 (padrão), 512, 256.
-
embeddingTypes: (opcional) aceita uma lista contendo “float”, “binary” ou ambos. O padrão é
float
.
{ "inputText": string, "dimensions": int, "normalize": boolean, "embeddingTypes": list }
-
- V2 Response
-
Os campos são descritos abaixo.
-
incorporação — Uma matriz que representa o vetor de incorporação da entrada que você forneceu. O tipo sempre será
float
. -
inputTextTokenContagem — O número de tokens na entrada.
-
embeddingsByType — Um dicionário ou mapa da lista de incorporação. Depende da entrada, listas “float”, “binary” ou ambas.
-
Example:
"embeddingsByType": {"binary": [int,..], "float": [float,...]}
-
Esse campo sempre aparecerá. Mesmo que você não especifique
embeddingTypes
na entrada, ainda haverá “float”. Example:"embeddingsByType": {"float": [float,...]}
-
{ "embedding": [float, float, ...], "inputTextTokenCount": int, "embeddingsByType": {"binary": [int,..], "float": [float,...]} }
-
- G1 Request
-
O único campo disponível é
inputText
, no qual você pode incluir texto para converter em uma incorporação.{ "inputText": string }
- G1 Response
-
O
body
da resposta contém os campos a seguir.{ "embedding": [float, float, ...], "inputTextTokenCount": int }
Os campos são descritos abaixo.
-
incorporação — Uma matriz que representa o vetor de incorporação da entrada que você forneceu.
-
inputTextTokenContagem — O número de tokens na entrada.
-
Código de exemplo
Os exemplos a seguir mostram como chamar os modelos Amazon Titan Embeddings para gerar incorporação. Selecione a guia que corresponde ao modelo que você está usando:
- Amazon Incorporador do Titan 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 Incorporador de Texto do Titan v2
-
Ao usar o Titan Text Embeddings V2, o campo
embedding
não estará na resposta se oembeddingTypes
contiver apenasbinary
.# 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()