AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-06-13 · Documentation low

File: code-library/latest/ug/swift_1_bedrock-runtime_code_examples.md

Summary

Added documentation and code examples for Meta Llama integration using Bedrock's Converse API and ConverseStream API

Security assessment

The changes add documentation for a new model integration and streaming API usage. No security vulnerabilities or security-specific features are mentioned in the diff.

Diff

diff --git a/code-library/latest/ug/swift_1_bedrock-runtime_code_examples.md b/code-library/latest/ug/swift_1_bedrock-runtime_code_examples.md
index 562e0999b..cbcbbe0c8 100644
--- a//code-library/latest/ug/swift_1_bedrock-runtime_code_examples.md
+++ b//code-library/latest/ug/swift_1_bedrock-runtime_code_examples.md
@@ -5 +5 @@
-Amazon NovaAnthropic Claude
+Amazon NovaAnthropic ClaudeMeta Llama
@@ -20,0 +21,2 @@ Each example includes a link to the complete source code, where you can find ins
+  * Meta Llama
+
@@ -322,0 +325,155 @@ Send a text message to Anthropic Claude, using Bedrock's Converse API and proces
+        }
+    }
+    
+    
+    
+
+  * For API details, see [ConverseStream](https://sdk.amazonaws.com/swift/api/awsbedrockruntime/latest/documentation/awsbedrockruntime/bedrockruntimeclient/conversestream\(input:\)) in _AWS SDK for Swift API reference_. 
+
+
+
+
+## Meta Llama
+
+The following code example shows how to send a text message to Meta Llama, using Bedrock's Converse API.
+
+**SDK for Swift**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/swift/example_code/bedrock-runtime#code-examples). 
+
+Send a text message to Meta Llama, using Bedrock's Converse API.
+    
+    
+    // An example demonstrating how to use the Conversation API to send 
+    // a text message to Meta Llama.
+    
+    import AWSBedrockRuntime
+    
+    func converse(_ textPrompt: String) async throws -> String {
+    
+        // Create a Bedrock Runtime client in the AWS Region you want to use.
+        let config =
+            try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration(
+                region: "us-east-1"
+            )
+        let client = BedrockRuntimeClient(config: config)
+    
+        // Set the model ID.
+        let modelId = "meta.llama3-8b-instruct-v1:0"
+    
+        // Start a conversation with the user message.
+        let message = BedrockRuntimeClientTypes.Message(
+            content: [.text(textPrompt)],
+            role: .user
+        )
+    
+        // Optionally use inference parameters
+        let inferenceConfig =
+            BedrockRuntimeClientTypes.InferenceConfiguration(
+                maxTokens: 512,
+                stopSequences: ["END"],
+                temperature: 0.5,
+                topp: 0.9
+            )
+    
+        // Create the ConverseInput to send to the model
+        let input = ConverseInput(
+            inferenceConfig: inferenceConfig, messages: [message], modelId: modelId)
+    
+        // Send the ConverseInput to the model
+        let response = try await client.converse(input: input)
+    
+        // Extract and return the response text.
+        if case let .message(msg) = response.output {
+            if case let .text(textResponse) = msg.content![0] {
+                return textResponse
+            } else {
+                return "No text response found in message content"
+            }
+        } else {
+            return "No message found in converse output"
+        }
+    }
+    
+    
+    
+
+  * For API details, see [Converse](https://sdk.amazonaws.com/swift/api/awsbedrockruntime/latest/documentation/awsbedrockruntime/bedrockruntimeclient/converse\(input:\)) in _AWS SDK for Swift API reference_. 
+
+
+
+
+The following code example shows how to send a text message to Meta Llama, using Bedrock's Converse API and process the response stream in real-time.
+
+**SDK for Swift**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/swift/example_code/bedrock-runtime#code-examples). 
+
+Send a text message to Meta Llama, using Bedrock's Converse API and process the response stream in real-time.
+    
+    
+    // An example demonstrating how to use the Conversation API to send a text message
+    // to Meta Llama and print the response stream.
+    
+    import AWSBedrockRuntime
+    
+    func printConverseStream(_ textPrompt: String) async throws {
+    
+        // Create a Bedrock Runtime client in the AWS Region you want to use.
+        let config =
+            try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration(
+                region: "us-east-1"
+            )
+        let client = BedrockRuntimeClient(config: config)
+    
+        // Set the model ID.
+        let modelId = "meta.llama3-8b-instruct-v1:0"
+    
+        // Start a conversation with the user message.
+        let message = BedrockRuntimeClientTypes.Message(
+            content: [.text(textPrompt)],
+            role: .user
+        )
+    
+        // Optionally use inference parameters.
+        let inferenceConfig =
+            BedrockRuntimeClientTypes.InferenceConfiguration(
+                maxTokens: 512,
+                stopSequences: ["END"],
+                temperature: 0.5,
+                topp: 0.9
+            )
+    
+        // Create the ConverseStreamInput to send to the model.
+        let input = ConverseStreamInput(
+            inferenceConfig: inferenceConfig, messages: [message], modelId: modelId)
+    
+        // Send the ConverseStreamInput to the model.
+        let response = try await client.converseStream(input: input)
+    
+        // Extract the streaming response.
+        guard let stream = response.stream else {
+            print("No stream available")
+            return
+        }
+    
+        // Extract and print the streamed response text in real-time.
+        for try await event in stream {
+            switch event {
+            case .messagestart(_):
+                print("\nMeta Llama:")
+    
+            case .contentblockdelta(let deltaEvent):
+                if case .text(let text) = deltaEvent.delta {
+                    print(text, terminator: "")
+                }
+    
+            default:
+                break
+            }