Amazon 泰坦嵌入文本 - Amazon Bedrock

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon 泰坦嵌入文本

Titan Embeddings G1 - Text不支援使用推論參數。以下各節詳細說明請求和回應格式,並提供程式碼範例。

請求和回應

請求主體在請InvokeModel求的body字段中傳遞。

V2 Request

輸入文字參數是必需的。標準化和標註參數是可選的。

  • 輸入文字 — 輸入要轉換為嵌入的文字。

  • 規範化-指示是否規範化輸出嵌入的標誌。預設為 true。

  • 維度-輸出嵌入應具有的維度數。接受下列值:1024 (預設值)、512、256。

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

這些 欄位如下所述。

  • 嵌入-array,代表您提供的輸入的嵌入向量。

  • inputTextToken計數-輸入中令牌的數量。

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

唯一可用的欄位是inputText,您可以在其中包含要轉換為嵌入的文字。

{ "inputText": string }
G1 Response

body的回應包含下列欄位。

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

這些 欄位如下所述。

  • 嵌入-array,代表您提供的輸入的入向量。

  • inputTextToken計數-輸入中令牌的數量。

範例程式碼

此範例顯示如何呼叫 Amazon Titan 嵌入模型來產生嵌入。

# 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-dim 索引嵌入,將其存放在 S3 等低成本儲存選項中,並隨時將其 1024、512 或 256 維度版本載入他們喜愛的向量資料庫中。

若要將指定的內嵌從 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))