-
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.
- Loading branch information
1 parent
9c0c1e6
commit 1a4f4e1
Showing
9 changed files
with
286 additions
and
0 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
18 changes: 18 additions & 0 deletions
18
Sources/AnthropicSwiftSDK-Bedrock/BedrockAnthropicClient.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,18 @@ | ||
// | ||
// BedrockAnthropicClient.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/22. | ||
// | ||
|
||
import Foundation | ||
import AWSBedrockRuntime | ||
import AnthropicSwiftSDK | ||
|
||
public final class BedrockAnthropicClient { | ||
public let messages: AnthropicSwiftSDK_Bedrock.Messages | ||
|
||
init(client: BedrockRuntimeClient, model: Model) { | ||
self.messages = .init(client: client, model: model) | ||
} | ||
} |
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 | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
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,16 @@ | ||
// | ||
// BedrockClient+Extension.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/22. | ||
// | ||
|
||
import Foundation | ||
import AWSBedrockRuntime | ||
import AnthropicSwiftSDK | ||
|
||
public extension BedrockRuntimeClient { | ||
static func useAnthropic(_ client: BedrockRuntimeClient, model: Model) -> BedrockAnthropicClient { | ||
BedrockAnthropicClient(client: client, model: model) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
// | ||
// InvokeModelInput+Extension.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/23. | ||
// | ||
|
||
import Foundation | ||
import AnthropicSwiftSDK | ||
import AWSBedrockRuntime | ||
|
||
extension InvokeModelInput { | ||
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 | ||
) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...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,23 @@ | ||
// | ||
// InvokeModelWithResponseStreamInput+Extension.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/23. | ||
// | ||
|
||
import Foundation | ||
import AnthropicSwiftSDK | ||
import AWSBedrockRuntime | ||
|
||
extension InvokeModelWithResponseStreamInput { | ||
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 | ||
) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
// | ||
// MessagesResponse+Extension.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/23. | ||
// | ||
|
||
import Foundation | ||
import AnthropicSwiftSDK | ||
import AWSBedrockRuntime | ||
|
||
extension MessagesResponse { | ||
init (from invokeModelOutput: InvokeModelOutput) throws { | ||
guard let data = invokeModelOutput.body else { | ||
fatalError() | ||
} | ||
|
||
self = try anthropicJSONDecoder.decode(MessagesResponse.self, from: data) | ||
} | ||
} |
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,116 @@ | ||
// | ||
// Messages.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/22. | ||
// | ||
|
||
import Foundation | ||
import AnthropicSwiftSDK | ||
import AWSBedrockRuntime | ||
|
||
public struct Messages { | ||
private let acceptContentType = "application/json" | ||
private let requestContentType = "application/json" | ||
private let client: BedrockRuntimeClient | ||
|
||
public let model: Model | ||
|
||
init(client: BedrockRuntimeClient, model: Model) { | ||
self.client = client | ||
self.model = model | ||
} | ||
|
||
public func createMessage( | ||
_ messages: [Message], | ||
model: Model = .claude_3_Haiku, | ||
system: String? = nil, | ||
maxTokens: Int, | ||
metaData: MetaData? = nil, | ||
stopSequence: [String]? = nil, | ||
temperature: Double? = nil, | ||
topP: Int? = nil, | ||
topK: Int? = nil | ||
) async throws -> MessagesResponse { | ||
// In the inference call, fill the body field with a JSON object that conforms the type call you want to make [Anthropic Claude Messages API](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html). | ||
let requestBody = MessagesRequest( | ||
model: model, | ||
messages: messages, | ||
system: system, | ||
maxTokens: maxTokens, | ||
metaData: metaData, | ||
stopSequences: stopSequence, | ||
stream: false, | ||
temperature: temperature, | ||
topP: topP, | ||
topK: topK | ||
) | ||
|
||
let input = try InvokeModelInput( | ||
accept: acceptContentType, | ||
request: requestBody, | ||
contentType: requestContentType | ||
) | ||
|
||
let response = try await client.invokeModel(input: input) | ||
|
||
return try MessagesResponse(from: response) | ||
} | ||
|
||
public func streamMessage( | ||
_ messages: [Message], | ||
model: Model = .claude_3_Haiku, | ||
system: String? = nil, | ||
maxTokens: Int, | ||
metaData: MetaData? = nil, | ||
stopSequence: [String]? = nil, | ||
temperature: Double? = nil, | ||
topP: Int? = nil, | ||
topK: Int? = nil | ||
) async throws -> AsyncThrowingStream<StreamingResponse, Error> { | ||
// In the inference call, fill the body field with a JSON object that conforms the type call you want to make [Anthropic Claude Messages API](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html). | ||
let requestBody = MessagesRequest( | ||
model: model, | ||
messages: messages, | ||
system: system, | ||
maxTokens: maxTokens, | ||
metaData: metaData, | ||
stopSequences: stopSequence, | ||
stream: true, | ||
temperature: temperature, | ||
topP: topP, | ||
topK: topK | ||
) | ||
|
||
let data = try anthropicJSONEncoder.encode(requestBody) | ||
|
||
let input = try InvokeModelWithResponseStreamInput( | ||
accept: acceptContentType, | ||
request: requestBody, | ||
contentType: requestContentType | ||
) | ||
|
||
let response = try await client.invokeModelWithResponseStream(input: input) | ||
|
||
guard let responseStream = response.body else { | ||
fatalError() | ||
} | ||
|
||
return try await AnthropicStreamingParser.parse(stream: responseStream.map { try $0.toString() }) | ||
} | ||
} | ||
|
||
extension BedrockRuntimeClientTypes.ResponseStream { | ||
func toString() throws -> String { | ||
guard case let .chunk(payload) = self else { | ||
fatalError() | ||
} | ||
|
||
guard let data = payload.bytes, | ||
let line = String(data: data, encoding: .utf8) else { | ||
fatalError() | ||
} | ||
|
||
return line | ||
} | ||
} |