generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 2
Add basic support for agent runtime #58
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e0211e4
add basic support for agentruntime
sebsto cc7a36f
Add retrieve example to the CI
sebsto a2b985d
Swift-format
sebsto 7061b00
Update Sources/BedrockService/BedrockService+AgentRuntime.swift
sebsto c0b81be
Update Sources/BedrockService/RetrieveResult.swift
sebsto f43297e
provide a convenience wrapper around retrieveinput
sebsto fc4553b
add swift docc header
sebsto ebc1671
swift-format
sebsto b842ede
move files to an agent runtime directory
sebsto f8c37bb
rearrange files in different directory structure
sebsto 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| .DS_Store | ||
| /.build | ||
| /Packages | ||
| xcuserdata/ | ||
| DerivedData/ | ||
| .swiftpm/configuration/registries.json | ||
| .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
| .netrc |
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,30 @@ | ||
| // swift-tools-version: 6.0 | ||
| // The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
|
||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "Retrieve", | ||
| platforms: [.macOS(.v15), .iOS(.v18), .tvOS(.v18)], | ||
| products: [ | ||
| .executable(name: "Retrieve", targets: ["Retrieve"]) | ||
| ], | ||
| dependencies: [ | ||
| // for production use, uncomment the following line | ||
| // .package(url: "https://github.com/build-on-aws/swift-bedrock-library.git", branch: "main"), | ||
|
|
||
| // for local development, use the following line | ||
| .package(name: "swift-bedrock-library", path: "../.."), | ||
|
|
||
| .package(url: "https://github.com/apple/swift-log.git", from: "1.5.0"), | ||
| ], | ||
| targets: [ | ||
| .executableTarget( | ||
| name: "Retrieve", | ||
| dependencies: [ | ||
| .product(name: "BedrockService", package: "swift-bedrock-library"), | ||
| .product(name: "Logging", package: "swift-log"), | ||
| ] | ||
| ) | ||
| ] | ||
| ) |
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,82 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift Bedrock Library open source project | ||
| // | ||
| // Copyright (c) 2025 Amazon.com, Inc. or its affiliates | ||
| // and the Swift Bedrock Library project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift Bedrock Library project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import BedrockService | ||
| import Logging | ||
|
|
||
| @main | ||
| struct Main { | ||
| static func main() async throws { | ||
| do { | ||
| try await Main.retrieve() | ||
| } catch { | ||
| print("Error:\n\(error)") | ||
| } | ||
| } | ||
|
|
||
| static func retrieve() async throws { | ||
| var logger = Logger(label: "Retrieve") | ||
| logger.logLevel = .debug | ||
|
|
||
| let bedrock = try await BedrockService( | ||
| region: .uswest2, | ||
| logger: logger | ||
| // uncomment if you use SSO with AWS Identity Center | ||
| // authentication: .sso | ||
| ) | ||
|
|
||
| let knowledgeBaseId = "EQ13XRVPLE" | ||
| let query = "should I write open source or open-source" | ||
| let numberOfResults = 3 | ||
|
|
||
| print("Retrieving from knowledge base...") | ||
| print("Knowledge Base ID: \(knowledgeBaseId)") | ||
| print("Query: \(query)") | ||
| print("Number of results: \(numberOfResults)") | ||
| print() | ||
|
|
||
| let response = try await bedrock.retrieve( | ||
| knowledgeBaseId: knowledgeBaseId, | ||
| retrievalQuery: query, | ||
| numberOfResults: numberOfResults | ||
| ) | ||
|
|
||
| print("Retrieved \(response.results?.count ?? 0) results:") | ||
|
|
||
| // Show best match using convenience function | ||
| if let bestMatch = response.bestMatch() { | ||
| print("\n--- Best Match (Score: \(bestMatch.score ?? 0)) ---") | ||
| if let content = bestMatch.content?.text { | ||
| print("Content: \(content)") | ||
| } | ||
| } | ||
|
|
||
| // Show all results using convenience property | ||
| // if let results = response.results { | ||
| // for (index, result) in results.enumerated() { | ||
| // print("\n--- Result \(index + 1) ---") | ||
| // if let content = result.content?.text { | ||
| // print("Content: \(content)") | ||
| // } | ||
| // if let score = result.score { | ||
| // print("Score: \(score)") | ||
| // } | ||
| // if let location = result.location?.s3Location { | ||
| // print("Source: s3://\(location.uri ?? "unknown")") | ||
| // } | ||
| // } | ||
| // } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift Bedrock Library open source project | ||
| // | ||
| // Copyright (c) 2025 Amazon.com, Inc. or its affiliates | ||
| // and the Swift Bedrock Library project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift Bedrock Library project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| @preconcurrency import AWSBedrockAgentRuntime | ||
| import Logging | ||
|
|
||
| extension BedrockService { | ||
| /// Creates a BedrockAgentRuntimeClient | ||
| /// - Parameters: | ||
| /// - region: The AWS region to configure the client for | ||
| /// - authentication: The authentication type to use | ||
| /// - logger: Logger instance | ||
| /// - Returns: Configured BedrockAgentRuntimeProtocol instance | ||
| /// - Throws: Error if client creation fails | ||
| internal static func createBedrockAgentRuntimeClient( | ||
| region: Region, | ||
| authentication: BedrockAuthentication, | ||
| logger: Logging.Logger | ||
| ) async throws -> BedrockAgentRuntimeClient { | ||
| let config: BedrockAgentRuntimeClient.BedrockAgentRuntimeClientConfiguration = try await prepareConfig( | ||
| initialConfig: BedrockAgentRuntimeClient.BedrockAgentRuntimeClientConfiguration(region: region.rawValue), | ||
| authentication: authentication, | ||
| logger: logger | ||
| ) | ||
| return BedrockAgentRuntimeClient(config: config) | ||
| } | ||
| /// Retrieves information from a knowledge base | ||
| /// - Parameters: | ||
| /// - knowledgeBaseId: The unique identifier of the knowledge base to query | ||
| /// - retrievalQuery: The query to search for in the knowledge base | ||
| /// - numberOfResults: The number of results to return (optional, defaults to 3) | ||
| /// - Returns: RetrieveResult containing the retrieved results | ||
| /// - Throws: BedrockLibraryError or other errors from the underlying service | ||
| public func retrieve( | ||
| knowledgeBaseId: String, | ||
| retrievalQuery: String, | ||
| numberOfResults: Int = 3 | ||
| ) async throws -> RetrieveResult { | ||
| logger.trace("Retrieving from knowledge base", metadata: [ | ||
| "knowledgeBaseId": .string(knowledgeBaseId), | ||
| "numberOfResults": .stringConvertible(numberOfResults) | ||
| ]) | ||
|
|
||
| let input = RetrieveInput( | ||
| knowledgeBaseId: knowledgeBaseId, | ||
| retrievalQuery: BedrockAgentRuntimeClientTypes.KnowledgeBaseQuery(text: retrievalQuery) | ||
| ) | ||
|
|
||
| do { | ||
| let response = try await bedrockAgentRuntimeClient.retrieve(input: input) | ||
| logger.trace("Successfully retrieved from knowledge base") | ||
| return RetrieveResult(response) | ||
| } catch { | ||
| try handleCommonError(error, context: "retrieving from knowledge base") | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.