Testo di Amazon Titan Embeddings - Amazon Bedrock

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à.

Testo di Amazon Titan Embeddings

Titan Embeddings G1 - Textnon supporta l'uso di parametri di inferenza. Le sezioni seguenti descrivono in dettaglio i formati di richiesta e risposta e forniscono un esempio di codice.

Richiesta e risposta

Il corpo della richiesta viene passato nel body campo di una InvokeModelrichiesta.

V2 Request

Il parametro InputText è obbligatorio. I parametri normalize e dimensions sono opzionali.

  • InputText: immettete il testo da convertire in incorporamenti.

  • normalize - flag che indica se normalizzare o meno gli incorporamenti di output. Il valore predefinito è true.

  • dimensioni - Il numero di dimensioni che dovrebbero avere gli incorporamenti di output. Sono accettati i seguenti valori: 1024 (impostazione predefinita), 512, 256.

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

I campi sono descritti di seguito.

  • incorporamento: un array che rappresenta il vettore di incorporamento dell'input fornito.

  • inputTextTokenConteggio: il numero di token nell'input.

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

L'unico campo disponibile èinputText, in cui è possibile includere testo da convertire in incorporamenti.

{ "inputText": string }
G1 Response

Il campo body della risposta contiene i seguenti campi.

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

I campi sono descritti di seguito.

  • incorporamento: un array che rappresenta il vettore di incorporamento dell'input fornito.

  • inputTextTokenConteggio: il numero di token nell'input.

Codice di esempio

Questo esempio mostra come chiamare il modello Amazon Titan Embeddings per generare incorporamenti.

# 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 text that the model generated, token information, and the reason the model stopped generating text. """ 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()
""" 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 text that the model generated, token information, and the reason the model stopped generating text. """ 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, "dimensions": 512, "normalize": True }) 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 Text Embeddings V2 model {model_id}.") if __name__ == "__main__": main()

Configura il compromesso tra precisione e costi man mano che procedi

Sebbene la normalizzazione sia disponibile tramite API, i clienti possono anche ridurre la dimensione di incorporamento dopo aver generato gli incorporamenti, in modo da trovare un compromesso tra precisione e costi man mano che le loro esigenze evolvono. Ciò consente ai clienti di generare incorporamenti di indici a 1024 dimensioni, archiviarli in opzioni di storage a basso costo come S3 e caricare la versione a 1024, 512 o 256 dimensioni nel proprio DB vettoriale preferito man mano che procedono.

Per ridurre un determinato incorporamento da 1024 a 256 dimensioni, puoi utilizzare la seguente logica di esempio:

import numpy as np from numpy import linalg def normalize_embedding(embedding: np.Array): ''' Args: embedding: Unnormlized 1D/2D numpy array - 1D: (emb_dim) - 2D: (batch_size, emb_dim) Return: np.array: Normalized 1D/2D numpy array ''' return embedding/linalg.norm(embedding, dim=-1, keep_dim=True) def reduce_emb_dim(embedding: np.Array, target_dim:int, normalize:bool=True) -> np.Array: ''' Args: embedding: Unnormlized 1D/2D numpy array. Expected shape: - 1D: (emb_dim) - 2D: (batch_size, emb_dim) target_dim: target dimension to reduce the embedding to Return: np.array: Normalized 1D numpy array ''' smaller_embedding = embedding[..., :target_dim] if normalize: smaller_embedding = normalize_embedding(smaller_embedding) return smaller_embedding if __name__ == '__main__': embedding = # bedrock client call reduced_embedding = # bedrock client call with dim=256 post_reduction_embeddings = reduce_emb_dim(np.array(embeddings), dim=256) print(linalg.norm(np.array(reduced_embedding) - post_reduction_embeddings))