Amazon Titan bettet Text ein - Amazon Bedrock

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Amazon Titan bettet Text ein

Titan Embeddings G1 - Textunterstützt die Verwendung von Inferenzparametern nicht. In den folgenden Abschnitten werden die Anfrage- und Antwortformate detailliert beschrieben und ein Codebeispiel bereitgestellt.

Anfrage und Antwort

Der Anfragetext wird im body Feld einer InvokeModelAnfrage übergeben.

V2 Request

Der InputText-Parameter ist erforderlich. Die Parameter normalisieren und dimensionieren sind optional.

  • InputText — Geben Sie Text ein, der in Einbettungen umgewandelt werden soll.

  • normalize — Markierung, die angibt, ob die Ausgabeeinbettungen normalisiert werden sollen oder nicht. Standardwert ist „true“.

  • dimensions — Die Anzahl der Dimensionen, die die Ausgabe-Einbettungen haben sollten. Die folgenden Werte werden akzeptiert: 1024 (Standard), 512, 256.

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

Die -Felder werden im Folgenden beschrieben.

  • embedding — Ein Array, das den Einbettungsvektor der von Ihnen angegebenen Eingabe darstellt.

  • input TextTokenCount — Die Anzahl der Token in der Eingabe.

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

Das einzige verfügbare Feld istinputText, in das Sie Text einfügen können, der in Einbettungen umgewandelt werden soll.

{ "inputText": string }
G1 Response

Die body Antwort enthält die folgenden Felder.

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

Die -Felder werden im Folgenden beschrieben.

  • Einbettung — Ein Array, das den Einbettungsvektor der von Ihnen angegebenen Eingabe darstellt.

  • TextTokenAnzahl der Eingaben — Die Anzahl der Token in der Eingabe.

Beispiel-Code

Die folgenden Beispiele zeigen, wie das Amazon Titan Embeddings-Modell aufgerufen wird, um Einbettungen zu generieren. Wählen Sie die Registerkarte aus, die dem Modell entspricht, das Sie verwenden:

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 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 embedding created by the model and the number of input tokens. """ 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()
Amazon Titan-Texteinbettungen V2
# 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 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 embedding created by the model and the number of input tokens. """ 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() </programlisting> <para><emphasis role="bold">Configure your accuracy-cost tradeoff as you go</emphasis></para> <para>While normalization is available via API customers can also reduce the embedding dimension after generating the embeddings allowing them to tradeoff between accuracy and cost as their need evolve. This empower customers to generate 1024-dim index embeddings, store them in low-cost storage options such as S3 and load their 1024, 512 or 256 dimension version in their favorite vector DB as they go. </para> <para>To reduce a given embedding from 1024 to 256 dimensions you can use the following example logic:</para> <programlisting language="json">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))