本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Titan Embeddings G1 - Text 不支持使用推理参数。下文详细介绍了请求和响应格式,并提供了一个代码示例。
请求和响应
请求正文在 InvokeModel 请求的 body
字段中传递。
inputText 为必需参数。normalize 和 dimensions 为可选参数。
-
inputText – 输入要转换为嵌入向量的文本。
-
normalize –(可选)标记,用于表示是否对输出嵌入进行规范化。默认值为 true。
-
dimensions –(可选)输出嵌入应具有的维度数。接受以下值:1024(默认)、512、256。
-
embeddingTypes –(可选)接受包含“float”、“binary”或同时包含两者的列表。默认值为
float
。
{
"inputText": string,
"dimensions": int,
"normalize": boolean,
"embeddingTypes": list
}
代码示例
以下示例展示了如何调用 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 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()