使用以下命令提交单个提示 InvokeModel - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用以下命令提交单个提示 InvokeModel

您可以使用InvokeModelInvokeModelWithResponseStreamAPI 操作并指定模型,在单个提示上运行推理。Amazon Bedrock 模型的不同之处在于它们是接受文本、图像还是视频输入,以及它们能否生成文本、图像或嵌入的输出。某些模型可以在流中返回响应。要检查模型对输入、输出和流媒体支持的支持,请执行以下任一操作:

  • 查看模型的 “输入模式”、“输出模式” 或 “流媒体支持” 列中的值,网址为Amazon Bedrock 中支持的根基模型

  • 发送带有模型 ID 的GetFoundationModel请求并检查inputModalitiesoutputModalities、和responseStreamingSupported字段中的值。

重要

InvokeModelInvokeModelWithResponseStream通过以下方式受到限制:

使用 Amazon Bedrock 运行时终端节点发送InvokeModelInvokeModelWithResponseStream请求,根据提示运行模型推理。

以下字段是必填字段:

字段 应用场景
modelId 指定要使用的模型、推理配置文件或提示管理中的提示。要了解如何查找此值,请参阅 使用 API 提交提示并生成响应
body 为模型指定推理参数。要查看不同模型的推理参数,请参阅 基础模型的推理请求参数和响应字段。如果您在modelId字段中指定提示管理中的提示,请省略此字段(如果包含该字段,则会忽略该字段)。

以下字段是可选字段:

字段 应用场景
accept 为请求正文指定媒体类型。有关更多信息,请参阅 Swagger 网站上的 Media Types
contentType 为响应正文指定媒体类型。有关更多信息,请参阅 Swagger 网站上的 Media Types
performanceConfigLatency 指定是否针对延迟对模型进行优化。有关更多信息,请参阅 针对延迟优化模型推理
guardrailIdentifier 指定要应用于提示和响应的防护机制。有关更多信息,请参阅 测试你的护栏
guardrailVersion 指定要应用于提示和响应的防护机制。有关更多信息,请参阅 测试你的护栏
trace 指定是否返回您指定的防护机制的轨迹。有关更多信息,请参阅 测试你的护栏

调用模型代码示例

本主题提供了一些在 InvokeModelAPI 中使用单个提示运行推理的基本示例。有关不同模型的更多示例,请访问以下资源:

以下示例假设您已设置编程访问权限,以便在运行这些示例时自动对 AWS CLI 和适用于 Python 的 SDK (Boto3) 进行默认 AWS 区域 身份验证。有关设置编程访问权限的信息,请参阅。 API 入门

注意

在尝试示例之前,请查看以下几点:

  • 您应该在美国东部(弗吉尼亚北部)(us-east-1) 中测试这些示例,它支持示例中使用的所有模型。

  • body参数可能很大,因此对于某些 CLI 示例,系统会要求您创建一个 JSON 文件并将该文件提供到--body参数中,而不是在命令行中指定。

  • 对于图像和视频示例,系统会要求您使用自己的图像和视频。这些示例假设您的图像文件已命名image.png,您的视频文件已命名video.mp4

  • 您可能需要将图像或视频转换为 base64 编码的字符串,或者将其上传到 Amazon S3 地点。在示例中,您必须将占位符替换为实际的 base64 编码字符串或 S3 位置。

展开一个部分来尝试一些基本的代码示例。

以下示例使用 Amazon Titan Text Premier 模型生成对文本提示的文本响应。选择您首选方法的选项卡,然后按照以下步骤操作:

CLI

在终端中运行以下命令,并在名为的文件中找到生成的响应invoke-model-output.txt

aws bedrock-runtime invoke-model \ --model-id amazon.titan-text-premier-v1:0 \ --body '{ "inputText": "Describe the purpose of a 'hello world' program in one line.", "textGenerationConfig": { "maxTokenCount": 512, "temperature": 0.5 } }' \ --cli-binary-format raw-in-base64-out \ invoke-model-output.txt
Python

运行以下 Python 代码示例以生成文本响应:

# Use the native inference API to send a text message to Amazon Titan Text. import boto3 import json from botocore.exceptions import ClientError # Create a Bedrock Runtime client in the AWS Region of your choice. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Titan Text Premier. model_id = "amazon.titan-text-premier-v1:0" # Define the prompt for the model. prompt = "Describe the purpose of a 'hello world' program in one line." # Format the request payload using the model's native structure. native_request = { "inputText": prompt, "textGenerationConfig": { "maxTokenCount": 512, "temperature": 0.5, }, } # Convert the native request to JSON. request = json.dumps(native_request) try: # Invoke the model with the request. response = client.invoke_model(modelId=model_id, body=request) except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") exit(1) # Decode the response body. model_response = json.loads(response["body"].read()) # Extract and print the response text. response_text = model_response["results"][0]["outputText"] print(response_text)

以下代码示例在 Stable Diffusion XL 1.0 模型中使用文本提示生成图像。选择您首选方法的选项卡,然后按照以下步骤操作:

CLI

在终端中运行以下命令,并在名为的文件中找到生成的响应invoke-model-output.txt。代表图像的字节可以在响应的base64字段中找到:

aws bedrock-runtime invoke-model \ --model-id stability.stable-diffusion-xl-v1 \ --body '{ "text_prompts": [{"text": "A stylized picture of a cute old steampunk robot."}], "style_preset": "photographic", "seed": 0, "cfg_scale": 10, "steps": 30 }' \ --cli-binary-format raw-in-base64-out \ invoke-model-output.txt
Python

运行以下 Python 代码示例生成图像并在名为的文件夹中找到生成的stability_1.png图像文件output

# Use the native inference API to create an image with Amazon Titan Image Generator import base64 import boto3 import json import os import random # Create a Bedrock Runtime client in the AWS Region of your choice. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Titan Image Generator G1. model_id = "amazon.titan-image-generator-v1" # Define the image generation prompt for the model. prompt = "A stylized picture of a cute old steampunk robot." # Generate a random seed. seed = random.randint(0, 2147483647) # Format the request payload using the model's native structure. native_request = { "taskType": "TEXT_IMAGE", "textToImageParams": {"text": prompt}, "imageGenerationConfig": { "numberOfImages": 1, "quality": "standard", "cfgScale": 8.0, "height": 512, "width": 512, "seed": seed, }, } # Convert the native request to JSON. request = json.dumps(native_request) # Invoke the model with the request. response = client.invoke_model(modelId=model_id, body=request) # Decode the response body. model_response = json.loads(response["body"].read()) # Extract the image data. base64_image_data = model_response["images"][0] # Save the generated image to a local folder. i, output_dir = 1, "output" if not os.path.exists(output_dir): os.makedirs(output_dir) while os.path.exists(os.path.join(output_dir, f"titan_{i}.png")): i += 1 image_data = base64.b64decode(base64_image_data) image_path = os.path.join(output_dir, f"titan_{i}.png") with open(image_path, "wb") as file: file.write(image_data) print(f"The generated image has been saved to {image_path}")

以下示例使用 Amazon Titan Text Embeddings V2 模型为文本输入生成二进制嵌入。选择您首选方法的选项卡,然后按照以下步骤操作:

CLI

在终端中运行以下命令,并在名为的文件中找到生成的响应invoke-model-output.txt。生成的嵌入在binary字段中。

aws bedrock-runtime invoke-model \ --model-id amazon.titan-embed-text-v2:0 \ --body '{ "inputText": "What are the different services that you offer?", "embeddingTypes": ["binary"] }' \ --cli-binary-format raw-in-base64-out \ invoke-model-output.txt
Python

运行以下 Python 代码示例,为提供的文本生成嵌入内容:

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate an embedding 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_embedding(model_id, body): """ Generate an embedding with the vector representation of 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 an embedding 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, "embeddingTypes": ["binary"] }) try: response = generate_embedding(model_id, body) print(f"Generated an embedding: {response['embeddingsByType']['binary']}") # returns binary embedding print(f"Input text: {input_text}") 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 an embedding with Amazon Titan Text Embeddings V2 model {model_id}.") if __name__ == "__main__": main()

以下示例使用 Amazon Titan Multimodal Embeddings G1 模型为图像输入生成嵌入内容。选择您首选方法的选项卡,然后按照以下步骤操作:

CLI

打开终端并执行以下操作:

  1. 将当前文件夹image.png中标题的图像转换为 base64 编码的字符串,然后image.txt通过运行以下命令将其写入标题为的文件中:

    base64 -i image.png -o image.txt
  2. 创建一个名为的 JSON 文件image-input-embeddings-output.json并粘贴${image-base64}以下 JSON,替换为该image.txt文件的内容(确保字符串末尾没有新行):

    { "inputImage": "${image-base64}", "embeddingConfig": { "outputEmbeddingLength": 256 } }
  3. 运行以下命令,将image-input-embeddings-output.json文件指定为正文。

    aws bedrock-runtime invoke-model \ --model-id amazon.titan-embed-image-v1 \ --body file://image-input-embeddings-output.json \ --cli-binary-format raw-in-base64-out \ invoke-model-output.txt
  4. invoke-model-output.txt文件中找到生成的嵌入内容。

Python

在以下 Python 脚本中,/path/to/image替换为实际图像的路径。然后运行脚本生成嵌入内容:

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate embeddings from an image with the Amazon Titan Multimodal Embeddings G1 model (on demand). """ import base64 import json import logging import boto3 from botocore.exceptions import ClientError class EmbedError(Exception): "Custom exception for errors returned by Amazon Titan Multimodal Embeddings G1" def __init__(self, message): self.message = message logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_embeddings(model_id, body): """ Generate a vector of embeddings for an image input using Amazon Titan Multimodal Embeddings G1 on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: response (JSON): The embeddings that the model generated, token information, and the reason the model stopped generating embeddings. """ logger.info("Generating embeddings with Amazon Titan Multimodal Embeddings G1 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()) finish_reason = response_body.get("message") if finish_reason is not None: raise EmbedError(f"Embeddings generation error: {finish_reason}") return response_body def main(): """ Entrypoint for Amazon Titan Multimodal Embeddings G1 example. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") # Read image from file and encode it as base64 string. with open("/path/to/image", "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') model_id = 'amazon.titan-embed-image-v1' output_embedding_length = 256 # Create request body. body = json.dumps({ "inputImage": input_image, "embeddingConfig": { "outputEmbeddingLength": output_embedding_length } }) try: response = generate_embeddings(model_id, body) print(f"Generated image embeddings of length {output_embedding_length}: {response['embedding']}") except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) except EmbedError as err: logger.error(err.message) print(err.message) else: print(f"Finished generating image embeddings with Amazon Titan Multimodal Embeddings G1 model {model_id}.") if __name__ == "__main__": main()

选择您首选方法的选项卡,然后按照以下步骤操作:

CLI

以下示例使用AnthropicClaude 3 Haiku模型生成响应,给定图像和询问图像内容的文本提示。打开终端并执行以下操作:

  1. 将当前文件夹image.png中标题的图像转换为 base64 编码的字符串,然后image.txt通过运行以下命令将其写入标题为的文件中:

    base64 -i image.png -o image.txt
  2. 创建一个名为的 JSON 文件image-text-input.json并粘贴${image-base64}以下 JSON,替换为该image.txt文件的内容(确保字符串末尾没有新行):

    { "anthropic_version": "bedrock-2023-05-31", "max_tokens": 1000, "messages": [ { "role": "user", "content": [ { "type": "image", "source": { "type": "base64", "media_type": "image/png", "data": "${image-base64}" } }, { "type": "text", "text": "What's in this image?" } ] } ] }
  3. 运行以下命令,根据图像和随附的文本提示将文本输出生成到名为的文件中invoke-model-output.txt

    aws bedrock-runtime invoke-model \ --model-id anthropic.claude-3-haiku-20240307-v1:0 \ --body file://image-text-input.json \ --cli-binary-format raw-in-base64-out \ invoke-model-output.txt
  4. 在当前文件夹invoke-model-output.txt的文件中找到输出。

Python

在以下 python 脚本中,在运行脚本之前替换/path/to/image.png为图像的实际路径:

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to run a multimodal prompt with Anthropic Claude (on demand) and InvokeModel. """ import json import logging import base64 import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens): """ Invokes a model with a multimodal prompt. Args: bedrock_runtime: The Amazon Bedrock boto3 client. model_id (str): The model ID to use. messages (JSON) : The messages to send to the model. max_tokens (int) : The maximum number of tokens to generate. Returns: None. """ body = json.dumps( { "anthropic_version": "bedrock-2023-05-31", "max_tokens": max_tokens, "messages": messages } ) response = bedrock_runtime.invoke_model( body=body, modelId=model_id) response_body = json.loads(response.get('body').read()) return response_body def main(): """ Entrypoint for Anthropic Claude multimodal prompt example. """ try: bedrock_runtime = boto3.client(service_name='bedrock-runtime') model_id = 'anthropic.claude-3-sonnet-20240229-v1:0' max_tokens = 1000 input_text = "What's in this image?" input_image = "/path/to/image" # Replace with actual path to image file # Read reference image from file and encode as base64 strings. image_ext = input_image.split(".")[-1] with open(input_image, "rb") as image_file: content_image = base64.b64encode(image_file.read()).decode('utf8') message = { "role": "user", "content": [ { "type": "image", "source": { "type": "base64", "media_type": f"image/{image_ext}", "data": content_image } }, { "type": "text", "text": input_text } ] } messages = [message] response = run_multi_modal_prompt( bedrock_runtime, model_id, messages, max_tokens) print(json.dumps(response, indent=4)) except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) if __name__ == "__main__": main()

以下示例说明了如何使用Amazon Nova Lite模型生成响应,前提是您上传到 S3 存储桶的视频以及随附的文本提示。

先决条件:按照亚马逊简单存储服务用户指南中的上传对象中的步骤将标题video.mp4上传到您账户中的 Amazon S3 存储桶的视频。记下视频的 S3 URI。

选择您首选方法的选项卡,然后按照以下步骤操作:

CLI

打开终端并运行以下命令,s3://amzn-s3-demo-bucket/video.mp4替换为视频的实际 S3 位置:

aws bedrock-runtime invoke-model \ --model-id amazon.nova-lite-v1:0 \ --body '{ "messages": [ { "role": "user", "content": [ { "video": { "format": "mp4", "source": { "s3Location": { "uri": "s3://amzn-s3-demo-bucket/video.mp4" } } } }, { "text": "What happens in this video?" } ] } ] }' \ --cli-binary-format raw-in-base64-out \ invoke-model-output.txt

在当前文件夹invoke-model-output.txt的文件中找到输出。

Python

在以下 Python 脚本中,s3://amzn-s3-demo-bucket/video.mp4替换为视频的实际 S3 位置。然后运行脚本:

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to run a multimodal prompt with Nova Lite (on demand) and InvokeModel. """ import json import logging import base64 import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens): """ Invokes a model with a multimodal prompt. Args: bedrock_runtime: The Amazon Bedrock boto3 client. model_id (str): The model ID to use. messages (JSON) : The messages to send to the model. max_tokens (int) : The maximum number of tokens to generate. Returns: None. """ body = json.dumps( { "messages": messages, "inferenceConfig": { "maxTokens": max_tokens } } ) response = bedrock_runtime.invoke_model( body=body, modelId=model_id) response_body = json.loads(response.get('body').read()) return response_body def main(): """ Entrypoint for Nova Lite video prompt example. """ try: bedrock_runtime = boto3.client(service_name='bedrock-runtime') model_id = "amazon.nova-lite-v1:0" max_tokens = 1000 input_video_s3_uri = "s3://amzn-s3-demo-bucket/video.mp4" # Replace with real S3 URI video_ext = input_video_s3_uri.split(".")[-1] input_text = "What happens in this video?" message = { "role": "user", "content": [ { "video": { "format": video_ext, "source": { "s3Location": { "uri": input_video_s3_uri } } } }, { "text": input_text } ] } messages = [message] response = run_multi_modal_prompt( bedrock_runtime, model_id, messages, max_tokens) print(json.dumps(response, indent=4)) except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) if __name__ == "__main__": main()

以下示例说明如何使用Amazon Nova Lite模型生成响应,前提是视频转换为 base64 编码的字符串以及随附的文本提示。选择您首选方法的选项卡,然后按照以下步骤操作:

CLI

执行以下操作:

  1. 通过运行以下命令,将当前文件夹video.mp4中标题的视频转换为 base64:

    base64 -i video.mp4 -o video.txt
  2. 创建一个名为的 JSON 文件video-text-input.json并粘贴${video-base64}以下 JSON,替换为该video.txt文件的内容(确保最后没有新行):

    { "messages": [ { "role": "user", "content": [ { "video": { "format": "mp4", "source": { "bytes": ${video-base64} } } }, { "text": "What happens in this video?" } ] } ] }
  3. 运行以下命令,根据视频和随附的文本提示生成文本输出到名为的文件invoke-model-output.txt

    aws bedrock-runtime invoke-model \ --model-id amazon.nova-lite-v1:0 \ --body file://video-text-input.json \ --cli-binary-format raw-in-base64-out \ invoke-model-output.txt
  4. 在当前文件夹invoke-model-output.txt的文件中找到输出。

Python

在以下 Python 脚本中,/path/to/video.mp4替换为视频的实际路径。然后运行脚本:

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to run a multimodal prompt with Nova Lite (on demand) and InvokeModel. """ import json import logging import base64 import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens): """ Invokes a model with a multimodal prompt. Args: bedrock_runtime: The Amazon Bedrock boto3 client. model_id (str): The model ID to use. messages (JSON) : The messages to send to the model. max_tokens (int) : The maximum number of tokens to generate. Returns: None. """ body = json.dumps( { "messages": messages, "inferenceConfig": { "maxTokens": max_tokens } } ) response = bedrock_runtime.invoke_model( body=body, modelId=model_id) response_body = json.loads(response.get('body').read()) return response_body def main(): """ Entrypoint for Nova Lite video prompt example. """ try: bedrock_runtime = boto3.client(service_name='bedrock-runtime') model_id = "amazon.nova-lite-v1:0" max_tokens = 1000 input_video = "/path/to/video.mp4" # Replace with real path to video video_ext = input_video.split(".")[-1] input_text = "What happens in this video?" # Read reference video from file and encode as base64 string. with open(input_video, "rb") as video_file: content_video = base64.b64encode(video_file.read()).decode('utf8')\ message = { "role": "user", "content": [ { "video": { "format": video_ext, "source": { "bytes": content_video } } }, { "text": input_text } ] } messages = [message] response = run_multi_modal_prompt( bedrock_runtime, model_id, messages, max_tokens) print(json.dumps(response, indent=4)) except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) if __name__ == "__main__": main()

通过流式传输调用模型代码示例

注意

AWS CLI 不支持流式传输。

以下示例说明如何使用 InvokeModelWithResponseStreamAPI 使用提示符在 Python 中生成流式文本write an essay for living on mars in 1000 words

import boto3 import json brt = boto3.client(service_name='bedrock-runtime') body = json.dumps({ 'prompt': '\n\nHuman: write an essay for living on mars in 1000 words\n\nAssistant:', 'max_tokens_to_sample': 4000 }) response = brt.invoke_model_with_response_stream( modelId='anthropic.claude-v2', body=body ) stream = response.get('body') if stream: for event in stream: chunk = event.get('chunk') if chunk: print(json.loads(chunk.get('bytes').decode()))