Skip to content
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

Message Batches API #53

Merged
merged 26 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0d77f3f
add entities for message batches api
fumito-ito Oct 9, 2024
0349e23
refactor APIClient with using `Request` protocol for request object
fumito-ito Oct 17, 2024
e0da226
separate directory for response object
fumito-ito Oct 17, 2024
f5f5609
update support macOS version to conform `Never` to `Encodable`
fumito-ito Oct 17, 2024
a9e888d
use `Request` protocol to build each request objects
fumito-ito Oct 17, 2024
8d37383
move `HttpMethod`
fumito-ito Oct 18, 2024
18b60d6
update beta version for batch api
fumito-ito Oct 18, 2024
fa5f3a6
publish response entities
fumito-ito Oct 18, 2024
28b25e3
[should be merged] fix query handling
fumito-ito Oct 18, 2024
9657630
make intertinate unneeded object
fumito-ito Oct 18, 2024
8acbee5
add request entities
fumito-ito Oct 18, 2024
ecea79c
add API object and methods
fumito-ito Oct 18, 2024
f436525
add interface for message batches api
fumito-ito Oct 18, 2024
914bee6
add empty tests
fumito-ito Oct 18, 2024
7de3a5f
add tests for request object
fumito-ito Oct 22, 2024
fcf5db3
add tests for response object
fumito-ito Oct 23, 2024
f09c437
update Mock to handle errors
fumito-ito Oct 24, 2024
3dc3cc2
add tests for message batches api
fumito-ito Oct 23, 2024
2353162
use `SwiftyJSONLines` to handle json lines fetched from batche result…
fumito-ito Oct 24, 2024
934d685
add docs
fumito-ito Oct 24, 2024
55482b9
add readme about prompt caching
fumito-ito Oct 25, 2024
e8bcc2f
add readme about message batches
fumito-ito Oct 25, 2024
7fb1ddd
update runner for swift 6
fumito-ito Oct 25, 2024
2040347
[temp] update example
fumito-ito Oct 25, 2024
943b074
update example
fumito-ito Oct 25, 2024
2069387
fix SwiftyJSONLines version for iOS
fumito-ito Oct 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
jobs:
build:

runs-on: macos-14
runs-on: macos-15

steps:
- uses: actions/checkout@v4
Expand Down
86 changes: 0 additions & 86 deletions .swiftpm/configuration/Package.resolved

This file was deleted.

100 changes: 26 additions & 74 deletions Example.swiftpm/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import SwiftUI
import AnthropicSwiftSDK_VertexAI
import AnthropicSwiftSDK
import AWSBedrockRuntime
import AnthropicSwiftSDK_Bedrock

struct ContentView: View {
// MARK: Properties for Claude
@State private var claudeAPIKey = ""
@State private var isStreamClaude: Bool = false

// MARK: Properties for Bedrock
@State private var bedrockRegion = ""
@State private var isStreamBedrock: Bool = false

// MARK: Properties for Vertex
@State private var vertexProjectID = ""
@State private var vertexAuthToken = ""
@State private var isStreamVertex: Bool = false

var body: some View {
TabView {
// MARK: Claude
// MARK: Claude Messages API
NavigationStack {
VStack {
Spacer()
Expand All @@ -29,20 +16,10 @@ struct ContentView: View {
.padding()
.textFieldStyle(.roundedBorder)

Toggle(isOn: $isStreamClaude) {
Text("Enable Stream API")
}
.padding()

NavigationLink {
let claude = Anthropic(apiKey: claudeAPIKey)
if isStreamClaude {
let observable = StreamViewModel(messageHandler: claude.messages, title: "Stream \\w Claude")
StreamView(observable: observable)
} else {
let observable = SendViewModel(messageHandler: claude.messages, title: "Message \\w Claude")
SendView(observable: observable)
}
let observable = SendViewModel(messageHandler: claude.messages, title: "Message \\w Claude")
SendView(observable: observable)
} label: {
Text("Continue")
.frame(maxWidth: .infinity, minHeight: 48)
Expand All @@ -59,108 +36,83 @@ struct ContentView: View {

Spacer()
}
.navigationTitle("Claude Demo")
.navigationTitle("Claude Send Message Demo")
}
.tabItem {
Image(systemName: "pencil.and.scribble")
Text("Claude")
Text("Send Message")
}

// MARK: Bedrock
// MARK: Claude Stream Messages API
NavigationStack {
VStack {
Spacer()

TextField("Enter Region Code", text: $bedrockRegion)
TextField("Enter API Key", text: $claudeAPIKey)
.padding()
.textFieldStyle(.roundedBorder)

Toggle(isOn: $isStreamBedrock) {
Text("Enable Stream API")
}
.padding()

NavigationLink {
let bedrockClient = try! BedrockRuntimeClient(region: bedrockRegion)
let claude = bedrockClient.useAnthropic()
if isStreamBedrock {
let observable = StreamViewModel(messageHandler: claude.messages, title: "Stream \\w Bedrock", model: .claude_3_Opus)
StreamView(observable: observable)
} else {
let observable = SendViewModel(messageHandler: claude.messages, title: "Message \\w Bedrock", model: .claude_3_Opus)
SendView(observable: observable)
}
let claude = Anthropic(apiKey: claudeAPIKey)
let observable = StreamViewModel(messageHandler: claude.messages, title: "Stream \\w Claude")
StreamView(observable: observable)
} label: {
Text("Continue")
.frame(maxWidth: .infinity, minHeight: 48)
.foregroundColor(.white)
.background(
Capsule()
.foregroundColor(
bedrockRegion.isEmpty ? .gray.opacity(0.2) : .blue
claudeAPIKey.isEmpty ? .gray.opacity(0.2) : .blue
)
)
}
.padding()
.disabled(bedrockRegion.isEmpty)
.disabled(claudeAPIKey.isEmpty)

Spacer()
}
.navigationTitle("Bedrock Demo")
.navigationTitle("Claude Stream Message Demo")
}
.tabItem {
Image(systemName: "globe.americas.fill")
Text("Bedrock")
Image(systemName: "pencil.and.scribble")
Text("Stream Message")
}

// MARK: Vertex
// MARK: Claude Send Message Batches API
NavigationStack {
VStack {
Spacer()

TextField("Enter Project ID", text: $vertexProjectID)
.padding()
.textFieldStyle(.roundedBorder)

TextField("Enter Auth Token", text: $vertexAuthToken)
TextField("Enter API Key", text: $claudeAPIKey)
.padding()
.textFieldStyle(.roundedBorder)

Toggle(isOn: $isStreamVertex) {
Text("Enable Stream API")
}
.padding()

NavigationLink {
let claude = AnthropicVertexAIClient(projectId: vertexProjectID, accessToken: vertexAuthToken, region: .europeWest1)
if isStreamVertex {
let observable = StreamViewModel(messageHandler: claude.messages, title: "Stream \\w Vertex")
StreamView(observable: observable)
} else {
let observable = SendViewModel(messageHandler: claude.messages, title: "Message \\w Vertex")
SendView(observable: observable)
}
let claude = Anthropic(apiKey: claudeAPIKey)
let observable = SendMessageBatchesViewModel(messageHandler: claude.messageBatches, title: "Batch \\w Claude")
SendMessageBatchView(observable: observable)
} label: {
Text("Continue")
.frame(maxWidth: .infinity, minHeight: 48)
.foregroundColor(.white)
.background(
Capsule()
.foregroundColor(
vertexProjectID.isEmpty || vertexAuthToken.isEmpty ? .gray.opacity(0.2) : .blue
claudeAPIKey.isEmpty ? .gray.opacity(0.2) : .blue
)
)
}
.padding()
.disabled(vertexProjectID.isEmpty || vertexAuthToken.isEmpty)
.disabled(claudeAPIKey.isEmpty)

Spacer()
}
.navigationTitle("VertexAI Demo")
.navigationTitle("Claude Batch Message Demo")
}
.tabItem {
Image(systemName: "mountain.2.fill")
Text("Vertex")
Image(systemName: "pencil.and.scribble")
Text("Batch Message")
}

}
Expand Down
7 changes: 1 addition & 6 deletions Example.swiftpm/Extension/Messages+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@

import Foundation
import AnthropicSwiftSDK
import AnthropicSwiftSDK_Bedrock
import AnthropicSwiftSDK_VertexAI

extension AnthropicSwiftSDK.Messages: MessageSendable {}
extension AnthropicSwiftSDK.Messages: MessageStreamable {}
extension AnthropicSwiftSDK_Bedrock.Messages: MessageSendable {}
extension AnthropicSwiftSDK_Bedrock.Messages: MessageStreamable {}
extension AnthropicSwiftSDK_VertexAI.Messages: MessageSendable {}
extension AnthropicSwiftSDK_VertexAI.Messages: MessageStreamable {}
extension AnthropicSwiftSDK.MessageBatches: MessageBatchSendable {}
45 changes: 9 additions & 36 deletions Example.swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
{
"pins" : [
{
"identity" : "aws-crt-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/awslabs/aws-crt-swift",
"state" : {
"revision" : "7b42e0343f28b3451aab20840dc670abd12790bd",
"version" : "0.36.0"
}
},
{
"identity" : "aws-sdk-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/awslabs/aws-sdk-swift",
"state" : {
"revision" : "192bc5984cb765a88f8a59d0678aeb85a888a9f5",
"version" : "1.0.8"
}
},
{
"identity" : "documentationcomment",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -45,15 +27,6 @@
"version" : "5.0.1"
}
},
{
"identity" : "smithy-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/smithy-lang/smithy-swift",
"state" : {
"revision" : "c5242d4e06e721293ee91c7affd1d527912f596d",
"version" : "0.76.0"
}
},
{
"identity" : "swift-cmark",
"kind" : "remoteSourceControl",
Expand All @@ -63,15 +36,6 @@
"version" : "0.4.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "9cb486020ebf03bfa5b5df985387a14a98744537",
"version" : "1.6.1"
}
},
{
"identity" : "swift-markdown",
"kind" : "remoteSourceControl",
Expand All @@ -89,6 +53,15 @@
"revision" : "0687f71944021d616d34d922343dcef086855920",
"version" : "600.0.1"
}
},
{
"identity" : "swiftyjsonlines",
"kind" : "remoteSourceControl",
"location" : "https://github.com/fumito-ito/SwiftyJSONLines.git",
"state" : {
"revision" : "ae30455420897b2c45621a88b3ae3f052a8b5f3d",
"version" : "0.0.2"
}
}
],
"version" : 2
Expand Down
6 changes: 2 additions & 4 deletions Example.swiftpm/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
name: "Example",
targets: ["AppModule"],
bundleIdentifier: "com.github.fumito-ito.AnthropicSwiftSDK.Example",
teamIdentifier: "L8LPZ499U7",
teamIdentifier: "K489QY5CFD",
displayVersion: "1.0",
bundleVersion: "1",
appIcon: .placeholder(icon: .clock),
Expand All @@ -44,9 +44,7 @@ let package = Package(
.executableTarget(
name: "AppModule",
dependencies: [
.product(name: "AnthropicSwiftSDK", package: "AnthropicSwiftSDK"),
.product(name: "AnthropicSwiftSDK-Bedrock", package: "AnthropicSwiftSDK"),
.product(name: "AnthropicSwiftSDK-VertexAI", package: "AnthropicSwiftSDK")
.product(name: "AnthropicSwiftSDK", package: "AnthropicSwiftSDK")
],
path: "."
)
Expand Down
Loading