-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from fumito-ito/feature/aws-bedrock
AWS Bedrock support
- Loading branch information
Showing
29 changed files
with
671 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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 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
20 changes: 20 additions & 0 deletions
20
Sources/AnthropicSwiftSDK-Bedrock/AnthropicBedrockClient.swift
This file contains 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,20 @@ | ||
// | ||
// AnthropicBedrockClient.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/22. | ||
// | ||
|
||
import Foundation | ||
import AWSBedrockRuntime | ||
import AnthropicSwiftSDK | ||
|
||
/// Claude API Client through Bedrock API | ||
public final class AnthropicBedrockClient { | ||
/// Endpoint for Messages API | ||
public let messages: AnthropicSwiftSDK_Bedrock.Messages | ||
|
||
init(client: BedrockRuntimeClient, model: Model) { | ||
self.messages = .init(client: client, model: model) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Sources/AnthropicSwiftSDK-Bedrock/AnthropicBedrockClientError.swift
This file contains 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,16 @@ | ||
// | ||
// AnthropicBedrockClientError.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/25. | ||
// | ||
|
||
import Foundation | ||
import AWSBedrockRuntime | ||
|
||
public enum AnthropicBedrockClientError: Error { | ||
case cannotGetAnyDataFromBedrockMessageResponse(InvokeModelOutput) | ||
case cannotGetAnyDataFromBedrockStreamResponse(InvokeModelWithResponseStreamOutput) | ||
case bedrockRuntimeClientGetsUnknownPayload(BedrockRuntimeClientTypes.ResponseStream) | ||
case cannotGetAnyDataFromBedrockRuntimeClientPayload(BedrockRuntimeClientTypes.PayloadPart) | ||
} |
27 changes: 27 additions & 0 deletions
27
Sources/AnthropicSwiftSDK-Bedrock/Extensions/AnthropicSwiftSDK_Model+Extension.swift
This file contains 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,27 @@ | ||
// | ||
// AnthropicSwiftSDK_Model+Extension.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/23. | ||
// | ||
|
||
import Foundation | ||
import AnthropicSwiftSDK | ||
|
||
extension AnthropicSwiftSDK.Model { | ||
/// Model name for Amazon Bedrock | ||
/// | ||
/// for more detail, see https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html | ||
var bedrockModelName: String? { | ||
switch self { | ||
case .claude_3_Opus: | ||
return nil | ||
case .claude_3_Sonnet: | ||
return "anthropic.claude-3-sonnet-20240229-v1:0" | ||
case .claude_3_Haiku: | ||
return "anthropic.claude-3-haiku-20240307-v1:0" | ||
case let .custom(modelName): | ||
return modelName | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Sources/AnthropicSwiftSDK-Bedrock/Extensions/BedrockClient+Extension.swift
This file contains 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,21 @@ | ||
// | ||
// BedrockClient+Extension.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/22. | ||
// | ||
|
||
import Foundation | ||
import AWSBedrockRuntime | ||
import AnthropicSwiftSDK | ||
|
||
public extension BedrockRuntimeClient { | ||
/// Create a client using Claude through Bedrock | ||
/// - Parameters: | ||
/// - client: Bedrock runtime client | ||
/// - model: Claude model, Bedrock supports Haiku or Sonnet instance type. | ||
/// - Returns: Client using Claude through Bedrock | ||
static func useAnthropic(_ client: BedrockRuntimeClient, model: Model) -> AnthropicBedrockClient { | ||
AnthropicBedrockClient(client: client, model: model) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Sources/AnthropicSwiftSDK-Bedrock/Extensions/InvokeModelInput+Extension.swift
This file contains 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,28 @@ | ||
// | ||
// InvokeModelInput+Extension.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/23. | ||
// | ||
|
||
import Foundation | ||
import AnthropicSwiftSDK | ||
import AWSBedrockRuntime | ||
|
||
extension InvokeModelInput { | ||
/// Constructor for Bedrock invoke model input with Claude request object | ||
/// - Parameters: | ||
/// - accept: acceptable response content type | ||
/// - request: Claude API request. It will be converted to `Data` and contained in bedrock request. | ||
/// - contentType: acceptable request content type | ||
init(accept: String, request: MessagesRequest, contentType: String) throws { | ||
let data = try anthropicJSONEncoder.encode(request) | ||
|
||
self.init( | ||
accept: accept, | ||
body: data, | ||
contentType: contentType, | ||
modelId: request.model.bedrockModelName | ||
) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...s/AnthropicSwiftSDK-Bedrock/Extensions/InvokeModelWithResponseStreamInput+Extension.swift
This file contains 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,28 @@ | ||
// | ||
// InvokeModelWithResponseStreamInput+Extension.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/23. | ||
// | ||
|
||
import Foundation | ||
import AnthropicSwiftSDK | ||
import AWSBedrockRuntime | ||
|
||
extension InvokeModelWithResponseStreamInput { | ||
/// Constructor for Bedrock invoke model stream input with Claude request object | ||
/// - Parameters: | ||
/// - accept: acceptable response content type | ||
/// - request: Claude API request. It will be converted to `Data` and contained in bedrock request. | ||
/// - contentType: acceptable request content type | ||
init(accept: String, request: MessagesRequest, contentType: String) throws { | ||
let data = try anthropicJSONEncoder.encode(request) | ||
|
||
self.init( | ||
accept: accept, | ||
body: data, | ||
contentType: contentType, | ||
modelId: request.model.bedrockModelName | ||
) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Sources/AnthropicSwiftSDK-Bedrock/Extensions/MessagesResponse+Extension.swift
This file contains 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,24 @@ | ||
// | ||
// MessagesResponse+Extension.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/23. | ||
// | ||
|
||
import Foundation | ||
import AnthropicSwiftSDK | ||
import AWSBedrockRuntime | ||
|
||
extension MessagesResponse { | ||
/// Create `MessagesResponse` object from InvokeModelOutput. | ||
/// | ||
/// This constructor converts `InvokeModelOutput.body` into `MessageResponse` | ||
/// - Parameter invokeModelOutput: model output to convert `MessageResponse` | ||
init (from invokeModelOutput: InvokeModelOutput) throws { | ||
guard let data = invokeModelOutput.body else { | ||
throw AnthropicBedrockClientError.cannotGetAnyDataFromBedrockMessageResponse(invokeModelOutput) | ||
} | ||
|
||
self = try anthropicJSONDecoder.decode(MessagesResponse.self, from: data) | ||
} | ||
} |
Oops, something went wrong.