Amazon Titan Embeddings テキスト - Amazon Bedrock

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Amazon Titan Embeddings テキスト

Titan Embeddings G1 - Text は推論パラメータの使用をサポートしていません。以下のセクションでは、リクエストとレスポンスの形式の詳細とコード例を示します。

リクエストとレスポンス

リクエスト本文は、InvokeModelリクエストの bodyフィールドに渡されます。

V2 Request

T inputText TThe パラメータは必須です。正規化パラメータとディメンションパラメータはオプションです。

  • inputText – 埋め込みに変換するテキストを入力します。

  • normalize - 出力埋め込みを正規化するかどうかを示すフラグ。デフォルトは true です。

  • dimensions - 出力埋め込みに含めるディメンションの数。次の値を使用できます: 1024 (デフォルト)、512、256。

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

フィールドについて以下に説明します。

  • embedding – 指定した入力の埋め込みベクトルを表す配列。

  • inputTextTokenCount – 入力内のトークンの数。

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

使用可能なフィールドは のみでinputText、埋め込みに変換するテキストを含めることができます。

{ "inputText": string }
G1 Response

レスポンスbodyの には、次のフィールドが含まれます。

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

フィールドについて以下に説明します。

  • 埋め込み — 指定した入力の埋め込みベクトルを表す配列。

  • inputTextTokenCount – 入力内のトークンの数。

サンプルのコード

この例では、Amazon Titan Embeddings モデルを呼び出して埋め込みを生成する方法を示します。

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

精度とコストのトレードオフを移行に合わせて設定する

正規化は API 経由で利用できますが、埋め込みを生成した後、埋め込みのディメンションを減らすこともできます。これにより、ニーズの変化に応じて精度とコストをトレードオフできます。これにより、お客様は 1024 次元インデックス埋め込みを生成し、S3 などの低コストのストレージオプションに保存し、1024、512、256 ディメンションバージョンをお気に入りのベクトル DB にロードできます。

特定の埋め込みを 1024 から 256 のディメンションに減らすには、次のサンプルロジックを使用できます。

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))