AWS nova documentation change
Summary
Updated code examples to use nova-2-lite-v1 model ID, added InvokeModel API examples, expanded tool use implementation details, and improved multimodal input documentation
Security assessment
The changes primarily focus on API version updates (nova-lite-v1 to nova-2-lite-v1), expanded code examples for tool integration, and documentation structure improvements. No specific security vulnerabilities or security feature enhancements are mentioned in the diff. The timeout configuration changes (3600s) appear to be operational rather than security-related.
Diff
diff --git a/nova/latest/nova2-userguide/code-library.md b/nova/latest/nova2-userguide/code-library.md index 3b1c4710c..14ce5259e 100644 --- a//nova/latest/nova2-userguide/code-library.md +++ b//nova/latest/nova2-userguide/code-library.md @@ -4,0 +5,2 @@ +Converse API ExamplesInvokeModel API Examples + @@ -7 +9,5 @@ -This section provides code examples for common Amazon Nova operations using the Converse API. +This section provides code examples for common Amazon Nova operations using either the Converse API or the InvokeModel API. + +## Converse API Examples + +### Basic request @@ -9 +15 @@ This section provides code examples for common Amazon Nova operations using the -Send a basic text request to Amazon Nova models. +Send a basic text request to Amazon Nova models using the Converse API. @@ -100 +106,3 @@ Streaming -Process images and text together by embedding image data directly in the request. +### Multimodal input using embedded asset + +Process multimodal content by embedding document, image, video, or audio data directly in the request. This example uses image data. For details on the content structure for other modalities, see the [ContentBlock details in the Amazon Bedrock API documentation.](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) @@ -142 +150 @@ Non-streaming - modelId="us.amazon.nova-lite-v1:0", + modelId="us.amazon.nova-2-lite-v1:0", @@ -192 +200 @@ Streaming - modelId="us.amazon.nova-lite-v1:0", + modelId="us.amazon.nova-2-lite-v1:0", @@ -203 +211,3 @@ Streaming -Process images and text together by referencing images stored in S3. +### Multimodal input using S3 URI + +Process multimodal content by referencing documents, images, videos, or audio files stored in S3. This example uses an image reference. For details on the content structure for other modalities, see the [ContentBlock details in the Amazon Bedrock API documentation.](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ContentBlock.html) @@ -243 +253 @@ Non-streaming - modelId="us.amazon.nova-lite-v1:0", + modelId="us.amazon.nova-2-lite-v1:0", @@ -291 +301 @@ Streaming - modelId="us.amazon.nova-lite-v1:0", + modelId="us.amazon.nova-2-lite-v1:0", @@ -301,0 +312,2 @@ Streaming +### Extended thinking (reasoning) + @@ -408,0 +421,2 @@ Streaming +### Built-in tool: Nova Grounding with citations + @@ -501,0 +516,2 @@ Streaming +### Built-in tool: Code Interpreter + @@ -614,0 +631,2 @@ Streaming +### Tool use + @@ -624,6 +641,0 @@ Non-streaming - # Create the Bedrock Runtime client - bedrock = boto3.client( - "bedrock-runtime", - region_name="us-east-1", - config=Config(read_timeout=3600), - ) @@ -631,4 +643,7 @@ Non-streaming - # Define the tool configuration - tool_config = { - "tools": [ - { + def get_weather(city): + # Mock function to simulate weather API + return {"temperatureF": 48, "conditions": "light rain"} + + + # Define the toolSpec for the weather tool + weather_tool = { @@ -637 +652 @@ Non-streaming - "description": "Get the current weather for a location", + "description": "Get the current weather conditions in a given location", @@ -642 +657 @@ Non-streaming - "location": { + "city": { @@ -647 +662 @@ Non-streaming - "required": ["location"], + "required": ["city"], @@ -652,2 +666,0 @@ Non-streaming - ] - } @@ -655 +668,15 @@ Non-streaming - messages = [ + # Define the list of tools the model may use + tool_config = {"tools": [weather_tool]} + + # Create the Bedrock Runtime client, using an extended timeout configuration + # to support long-running requests. + bedrock = boto3.client( + "bedrock-runtime", + region_name="us-east-1", + config=Config(read_timeout=3600), + ) + + # Start tracking message history + messages = [] + + messages.append( @@ -658 +685,3 @@ Non-streaming - "content": [{"text": "What's the weather like in Seattle?"}], + "content": [ + { + "text": "Suggest some activities to do in Seattle based on the current weather." @@ -660 +689,3 @@ Non-streaming - ] + ], + } + ) @@ -667 +698,42 @@ Non-streaming - # Extract tool use from response + assistant_message = response["output"]["message"] + + # Add the assistant response to the message history + messages.append(assistant_message) + + content_list = assistant_message["content"] + stop_reason = response["stopReason"] + + if stop_reason == "tool_use": + # Extract the toolUse details + tool_use = next( + content["toolUse"] for content in content_list if "toolUse" in content + ) + tool_name = tool_use["name"] + tool_use_id = tool_use["toolUseId"] + + if tool_name == "get_weather": + # Call the tool + weather = get_weather(tool_use["input"]["city"]) + + # Send the result back to the model + messages.append( + { + "role": "user", + "content": [ + { + "toolResult": { + "toolUseId": tool_use_id, + "content": [{"json": weather}], + } + } + ], + } + ) + + # Submit the tool result back to the model + response = bedrock.converse( + modelId="us.amazon.nova-2-lite-v1:0", + messages=messages, + toolConfig=tool_config, + ) + @@ -670,4 +742,11 @@ Non-streaming - if "toolUse" in content: - tool_use = content["toolUse"] - print(f"Tool: {tool_use['name']}") - print(f"Input: {tool_use['input']}") + # Extract the text response + if "text" in content: + print("\n== Text ==") + print(content["text"]) + else: + # A tool call was not needed + for content in content_list: + # Extract the text response + if "text" in content: + print("\n== Text ==") + print(content["text"]) @@ -683,6 +761,0 @@ Streaming - # Create the Bedrock Runtime client - bedrock = boto3.client( - "bedrock-runtime", - region_name="us-east-1", - config=Config(connect_timeout=3600, read_timeout=3600), - ) @@ -690,4 +763,7 @@ Streaming - # Define the tool configuration - tool_config = { - "tools": [ - { + def get_weather(city): + # Mock function to simulate weather API + return {"temperatureF": 48, "conditions": "light rain"} + + + # Define the toolSpec for the weather tool + weather_tool = { @@ -696 +772 @@ Streaming - "description": "Get the current weather for a location", + "description": "Get the current weather conditions in a given location", @@ -701 +777 @@ Streaming - "location": { + "city": { @@ -706 +782 @@ Streaming - "required": ["location"], + "required": ["city"], @@ -711,2 +786,0 @@ Streaming - ] - }