1+ //===----------------------------------------------------------------------===//
2+ //
3+ // This source file is part of the Swift Bedrock Library open source project
4+ //
5+ // Copyright (c) 2025 Amazon.com, Inc. or its affiliates
6+ // and the Swift Bedrock Library project authors
7+ // Licensed under Apache License v2.0
8+ //
9+ // See LICENSE.txt for license information
10+ // See CONTRIBUTORS.txt for the list of Swift Bedrock Library project authors
11+ //
12+ // SPDX-License-Identifier: Apache-2.0
13+ //
14+ //===----------------------------------------------------------------------===//
15+
16+ @preconcurrency import AWSBedrockAgentRuntime
17+ import Logging
18+
19+ extension BedrockService {
20+ /// Creates a BedrockAgentRuntimeClient
21+ /// - Parameters:
22+ /// - region: The AWS region to configure the client for
23+ /// - authentication: The authentication type to use
24+ /// - logger: Logger instance
25+ /// - Returns: Configured BedrockAgentRuntimeProtocol instance
26+ /// - Throws: Error if client creation fails
27+ internal static func createBedrockAgentRuntimeClient(
28+ region: Region ,
29+ authentication: BedrockAuthentication ,
30+ logger: Logging . Logger
31+ ) async throws -> BedrockAgentRuntimeClient {
32+ let config : BedrockAgentRuntimeClient . BedrockAgentRuntimeClientConfiguration = try await prepareConfig (
33+ initialConfig: BedrockAgentRuntimeClient . BedrockAgentRuntimeClientConfiguration ( region: region. rawValue) ,
34+ authentication: authentication,
35+ logger: logger
36+ )
37+ return BedrockAgentRuntimeClient ( config: config)
38+ }
39+ /// Retrieves information from a knowledge base
40+ /// - Parameters:
41+ /// - knowledgeBaseId: The unique identifier of the knowledge base to query
42+ /// - retrievalQuery: The query to search for in the knowledge base
43+ /// - numberOfResults: The number of results to return (optional, defaults to 3)
44+ /// - Returns: RetrieveResult containing the retrieved results
45+ /// - Throws: BedrockLibraryError or other errors from the underlying service
46+ public func retrieve(
47+ knowledgeBaseId: String ,
48+ retrievalQuery: String ,
49+ numberOfResults: Int = 3
50+ ) async throws -> RetrieveResult {
51+ logger. trace ( " Retrieving from knowledge base " , metadata: [
52+ " knowledgeBaseId " : . string( knowledgeBaseId) ,
53+ " numberOfResults " : . stringConvertible( numberOfResults)
54+ ] )
55+
56+ let input = RetrieveInput (
57+ knowledgeBaseId: knowledgeBaseId,
58+ retrievalQuery: BedrockAgentRuntimeClientTypes . KnowledgeBaseQuery ( text: retrievalQuery)
59+ )
60+
61+ do {
62+ let response = try await bedrockAgentRuntimeClient. retrieve ( input: input)
63+ logger. trace ( " Successfully retrieved from knowledge base " )
64+ return RetrieveResult ( response)
65+ } catch {
66+ try handleCommonError ( error, context: " retrieving from knowledge base " )
67+ }
68+ }
69+ }
0 commit comments