Skip to content

Commit 4bee087

Browse files
authored
Merge pull request #23 from fumito-ito/feature/examples-for-bedrock-and-vertex
add example UI for VertexAI and AWS Bedrock
2 parents e03d209 + 133680f commit 4bee087

File tree

16 files changed

+792
-143
lines changed

16 files changed

+792
-143
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1540"
4+
version = "1.7">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES"
8+
buildArchitectures = "Automatic">
9+
<BuildActionEntries>
10+
<BuildActionEntry
11+
buildForTesting = "YES"
12+
buildForRunning = "YES"
13+
buildForProfiling = "YES"
14+
buildForArchiving = "YES"
15+
buildForAnalyzing = "YES">
16+
<BuildableReference
17+
BuildableIdentifier = "primary"
18+
BlueprintIdentifier = "Example"
19+
BuildableName = "Example"
20+
BlueprintName = "Example"
21+
ReferencedContainer = "container:">
22+
</BuildableReference>
23+
</BuildActionEntry>
24+
</BuildActionEntries>
25+
</BuildAction>
26+
<TestAction
27+
buildConfiguration = "Debug"
28+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
29+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
30+
shouldUseLaunchSchemeArgsEnv = "YES"
31+
shouldAutocreateTestPlan = "YES">
32+
</TestAction>
33+
<LaunchAction
34+
buildConfiguration = "Debug"
35+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
36+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
37+
launchStyle = "0"
38+
useCustomWorkingDirectory = "NO"
39+
ignoresPersistentStateOnLaunch = "NO"
40+
debugDocumentVersioning = "YES"
41+
debugServiceExtension = "internal"
42+
allowLocationSimulation = "YES">
43+
<BuildableProductRunnable
44+
runnableDebuggingMode = "0">
45+
<BuildableReference
46+
BuildableIdentifier = "primary"
47+
BlueprintIdentifier = "Example"
48+
BuildableName = "Example"
49+
BlueprintName = "Example"
50+
ReferencedContainer = "container:">
51+
</BuildableReference>
52+
</BuildableProductRunnable>
53+
<EnvironmentVariables>
54+
<EnvironmentVariable
55+
key = "AWS_ACCESS_KEY_ID"
56+
value = ""
57+
isEnabled = "YES">
58+
</EnvironmentVariable>
59+
<EnvironmentVariable
60+
key = "AWS_SECRET_ACCESS_KEY"
61+
value = ""
62+
isEnabled = "YES">
63+
</EnvironmentVariable>
64+
</EnvironmentVariables>
65+
</LaunchAction>
66+
<ProfileAction
67+
buildConfiguration = "Release"
68+
shouldUseLaunchSchemeArgsEnv = "YES"
69+
savedToolIdentifier = ""
70+
useCustomWorkingDirectory = "NO"
71+
debugDocumentVersioning = "YES">
72+
<BuildableProductRunnable
73+
runnableDebuggingMode = "0">
74+
<BuildableReference
75+
BuildableIdentifier = "primary"
76+
BlueprintIdentifier = "Example"
77+
BuildableName = "Example"
78+
BlueprintName = "Example"
79+
ReferencedContainer = "container:">
80+
</BuildableReference>
81+
</BuildableProductRunnable>
82+
</ProfileAction>
83+
<AnalyzeAction
84+
buildConfiguration = "Debug">
85+
</AnalyzeAction>
86+
<ArchiveAction
87+
buildConfiguration = "Release"
88+
revealArchiveInOrganizer = "YES">
89+
</ArchiveAction>
90+
</Scheme>

Example.swiftpm/ContentView.swift

Lines changed: 154 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,168 @@
11
import SwiftUI
2+
import AnthropicSwiftSDK_VertexAI
3+
import AnthropicSwiftSDK
4+
import AWSBedrockRuntime
5+
import AnthropicSwiftSDK_Bedrock
26

37
struct ContentView: View {
4-
@State private var apiKey = ""
8+
// MARK: Properties for Claude
9+
@State private var claudeAPIKey = ""
10+
@State private var isStreamClaude: Bool = false
11+
12+
// MARK: Properties for Bedrock
13+
@State private var bedrockRegion = ""
14+
@State private var isStreamBedrock: Bool = false
15+
16+
// MARK: Properties for Vertex
17+
@State private var vertexProjectID = ""
18+
@State private var vertexAuthToken = ""
19+
@State private var isStreamVertex: Bool = false
520

621
var body: some View {
7-
NavigationStack {
8-
VStack {
9-
Spacer()
10-
TextField("Enter API Key", text: $apiKey)
11-
.padding()
12-
.textFieldStyle(.roundedBorder)
13-
NavigationLink(destination: DemoView(observable: MessageSubject(apiKey: apiKey))) {
14-
Text("Continue")
22+
TabView {
23+
// MARK: Claude
24+
NavigationStack {
25+
VStack {
26+
Spacer()
27+
28+
TextField("Enter API Key", text: $claudeAPIKey)
29+
.padding()
30+
.textFieldStyle(.roundedBorder)
31+
32+
Toggle(isOn: $isStreamClaude) {
33+
Text("Enable Stream API")
34+
}
35+
.padding()
36+
37+
NavigationLink {
38+
let claude = Anthropic(apiKey: claudeAPIKey)
39+
if isStreamClaude {
40+
let observable = StreamViewModel(messageHandler: claude.messages, title: "Stream \\w Claude")
41+
StreamView(observable: observable)
42+
} else {
43+
let observable = SendViewModel(messageHandler: claude.messages, title: "Message \\w Claude")
44+
SendView(observable: observable)
45+
}
46+
} label: {
47+
Text("Continue")
1548
.frame(maxWidth: .infinity, minHeight: 48)
1649
.foregroundColor(.white)
1750
.background(
1851
Capsule()
19-
.foregroundColor(apiKey.isEmpty ? .gray.opacity(0.2) : .blue))
52+
.foregroundColor(
53+
claudeAPIKey.isEmpty ? .gray.opacity(0.2) : .blue
54+
)
55+
)
56+
}
57+
.padding()
58+
.disabled(claudeAPIKey.isEmpty)
59+
60+
Spacer()
2061
}
21-
.padding()
22-
.disabled(apiKey.isEmpty)
23-
Spacer()
62+
.navigationTitle("Claude Demo")
63+
}
64+
.tabItem {
65+
Image(systemName: "pencil.and.scribble")
66+
Text("Claude")
2467
}
25-
.padding()
26-
.navigationTitle("API KEY registration")
68+
69+
// MARK: Bedrock
70+
NavigationStack {
71+
VStack {
72+
Spacer()
73+
74+
TextField("Enter Region Code", text: $bedrockRegion)
75+
.padding()
76+
.textFieldStyle(.roundedBorder)
77+
78+
Toggle(isOn: $isStreamBedrock) {
79+
Text("Enable Stream API")
80+
}
81+
.padding()
82+
83+
NavigationLink {
84+
let bedrockClient = try! BedrockRuntimeClient(region: bedrockRegion)
85+
let claude = BedrockRuntimeClient.useAnthropic(bedrockClient, model: .claude_3_Opus)
86+
if isStreamBedrock {
87+
let observable = StreamViewModel(messageHandler: claude.messages, title: "Stream \\w Bedrock")
88+
StreamView(observable: observable)
89+
} else {
90+
let observable = SendViewModel(messageHandler: claude.messages, title: "Message \\w Bedrock")
91+
SendView(observable: observable)
92+
}
93+
} label: {
94+
Text("Continue")
95+
.frame(maxWidth: .infinity, minHeight: 48)
96+
.foregroundColor(.white)
97+
.background(
98+
Capsule()
99+
.foregroundColor(
100+
bedrockRegion.isEmpty ? .gray.opacity(0.2) : .blue
101+
)
102+
)
103+
}
104+
.padding()
105+
.disabled(bedrockRegion.isEmpty)
106+
107+
Spacer()
108+
}
109+
.navigationTitle("Bedrock Demo")
110+
}
111+
.tabItem {
112+
Image(systemName: "globe.americas.fill")
113+
Text("Bedrock")
114+
}
115+
116+
// MARK: Vertex
117+
NavigationStack {
118+
VStack {
119+
Spacer()
120+
121+
TextField("Enter Project ID", text: $vertexProjectID)
122+
.padding()
123+
.textFieldStyle(.roundedBorder)
124+
125+
TextField("Enter Auth Token", text: $vertexAuthToken)
126+
.padding()
127+
.textFieldStyle(.roundedBorder)
128+
129+
Toggle(isOn: $isStreamVertex) {
130+
Text("Enable Stream API")
131+
}
132+
.padding()
133+
134+
NavigationLink {
135+
let claude = AnthropicVertexAIClient(projectId: vertexProjectID, accessToken: vertexAuthToken, region: .europeWest1)
136+
if isStreamVertex {
137+
let observable = StreamViewModel(messageHandler: claude.messages, title: "Stream \\w Vertex")
138+
StreamView(observable: observable)
139+
} else {
140+
let observable = SendViewModel(messageHandler: claude.messages, title: "Message \\w Vertex")
141+
SendView(observable: observable)
142+
}
143+
} label: {
144+
Text("Continue")
145+
.frame(maxWidth: .infinity, minHeight: 48)
146+
.foregroundColor(.white)
147+
.background(
148+
Capsule()
149+
.foregroundColor(
150+
vertexProjectID.isEmpty || vertexAuthToken.isEmpty ? .gray.opacity(0.2) : .blue
151+
)
152+
)
153+
}
154+
.padding()
155+
.disabled(vertexProjectID.isEmpty || vertexAuthToken.isEmpty)
156+
157+
Spacer()
158+
}
159+
.navigationTitle("VertexAI Demo")
160+
}
161+
.tabItem {
162+
Image(systemName: "mountain.2.fill")
163+
Text("Vertex")
164+
}
165+
27166
}
28167
}
29168
}

Example.swiftpm/DemoView.swift

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// ChatMessage.swift
3+
//
4+
//
5+
// Created by Fumito Ito on 2024/07/05.
6+
//
7+
8+
import Foundation
9+
10+
struct ChatMessage: Identifiable {
11+
let id = UUID()
12+
let user: ChatUser
13+
let text: String
14+
}

Example.swiftpm/Entity/ChatUser.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// ChatUser.swift
3+
//
4+
//
5+
// Created by Fumito Ito on 2024/07/05.
6+
//
7+
8+
import Foundation
9+
10+
enum ChatUser: String {
11+
case user
12+
case assistant
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Messages+Extension.swift
3+
//
4+
//
5+
// Created by Fumito Ito on 2024/07/04.
6+
//
7+
8+
import Foundation
9+
import AnthropicSwiftSDK
10+
import AnthropicSwiftSDK_Bedrock
11+
import AnthropicSwiftSDK_VertexAI
12+
13+
extension AnthropicSwiftSDK.Messages: MessageSendable {}
14+
extension AnthropicSwiftSDK.Messages: MessageStreamable {}
15+
extension AnthropicSwiftSDK_Bedrock.Messages: MessageSendable {}
16+
extension AnthropicSwiftSDK_Bedrock.Messages: MessageStreamable {}
17+
extension AnthropicSwiftSDK_VertexAI.Messages: MessageSendable {}
18+
extension AnthropicSwiftSDK_VertexAI.Messages: MessageStreamable {}

0 commit comments

Comments
 (0)