-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Swift: code examples for Meta Llama to use Converse and ConverseStream, Amazon Bedrock #7434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
monadierickx
wants to merge
4
commits into
awsdocs:main
Choose a base branch
from
monadierickx:llama-converse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
swift/example_code/bedrock-runtime/models/meta_llama/Package.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// swift-tools-version: 6.1 | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MetaLlamaConverse", | ||
// Let Xcode know the minimum Apple platforms supported. | ||
platforms: [ | ||
.macOS(.v13), | ||
.iOS(.v15) | ||
], | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.2.61") | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package, defining a module or a test suite. | ||
// Targets can depend on other targets in this package and products from dependencies. | ||
.executableTarget( | ||
name: "Converse", | ||
dependencies: [ | ||
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"), | ||
], | ||
path: "Sources/Converse" | ||
), | ||
.executableTarget( | ||
name: "ConverseStream", | ||
dependencies: [ | ||
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"), | ||
], | ||
path: "Sources/ConverseStream" | ||
) | ||
] | ||
) |
65 changes: 65 additions & 0 deletions
65
swift/example_code/bedrock-runtime/models/meta_llama/Sources/Converse/main.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// snippet-start:[swift.example_code.bedrock-runtime.Converse_MetaLlama] | ||
// 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" | ||
} | ||
} | ||
|
||
// snippet-end:[swift.example_code.bedrock-runtime.Converse_MetaLlama] | ||
|
||
do { | ||
let reply = try await converse( | ||
"Describe the purpose of a 'hello world' program in one line." | ||
) | ||
print(reply) | ||
} catch { | ||
print("An error occurred: \(error)") | ||
} |
75 changes: 75 additions & 0 deletions
75
swift/example_code/bedrock-runtime/models/meta_llama/Sources/ConverseStream/main.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// snippet-start:[swift.example_code.bedrock-runtime.ConverseStream_MetaLlama] | ||
// 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 | ||
} | ||
} | ||
} | ||
|
||
// snippet-end:[swift.example_code.bedrock-runtime.ConverseStream_MetaLlama] | ||
|
||
do { | ||
try await printConverseStream( | ||
"Describe the purpose of a 'hello world' program in two paragraphs." | ||
) | ||
} catch { | ||
print("An error occurred: \(error)") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.