Skip to content

Commit

Permalink
Merge pull request #23 from fumito-ito/feature/examples-for-bedrock-a…
Browse files Browse the repository at this point in the history
…nd-vertex

add example UI for VertexAI and AWS Bedrock
  • Loading branch information
fumito-ito authored Jul 10, 2024
2 parents e03d209 + 133680f commit 4bee087
Show file tree
Hide file tree
Showing 16 changed files with 792 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1540"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Example"
BuildableName = "Example"
BlueprintName = "Example"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Example"
BuildableName = "Example"
BlueprintName = "Example"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "AWS_ACCESS_KEY_ID"
value = ""
isEnabled = "YES">
</EnvironmentVariable>
<EnvironmentVariable
key = "AWS_SECRET_ACCESS_KEY"
value = ""
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Example"
BuildableName = "Example"
BlueprintName = "Example"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
169 changes: 154 additions & 15 deletions Example.swiftpm/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,168 @@
import SwiftUI
import AnthropicSwiftSDK_VertexAI
import AnthropicSwiftSDK
import AWSBedrockRuntime
import AnthropicSwiftSDK_Bedrock

struct ContentView: View {
@State private var apiKey = ""
// 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 {
NavigationStack {
VStack {
Spacer()
TextField("Enter API Key", text: $apiKey)
.padding()
.textFieldStyle(.roundedBorder)
NavigationLink(destination: DemoView(observable: MessageSubject(apiKey: apiKey))) {
Text("Continue")
TabView {
// MARK: Claude
NavigationStack {
VStack {
Spacer()

TextField("Enter API Key", text: $claudeAPIKey)
.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)
}
} label: {
Text("Continue")
.frame(maxWidth: .infinity, minHeight: 48)
.foregroundColor(.white)
.background(
Capsule()
.foregroundColor(apiKey.isEmpty ? .gray.opacity(0.2) : .blue))
.foregroundColor(
claudeAPIKey.isEmpty ? .gray.opacity(0.2) : .blue
)
)
}
.padding()
.disabled(claudeAPIKey.isEmpty)

Spacer()
}
.padding()
.disabled(apiKey.isEmpty)
Spacer()
.navigationTitle("Claude Demo")
}
.tabItem {
Image(systemName: "pencil.and.scribble")
Text("Claude")
}
.padding()
.navigationTitle("API KEY registration")

// MARK: Bedrock
NavigationStack {
VStack {
Spacer()

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

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

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

Spacer()
}
.navigationTitle("Bedrock Demo")
}
.tabItem {
Image(systemName: "globe.americas.fill")
Text("Bedrock")
}

// MARK: Vertex
NavigationStack {
VStack {
Spacer()

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

TextField("Enter Auth Token", text: $vertexAuthToken)
.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)
}
} label: {
Text("Continue")
.frame(maxWidth: .infinity, minHeight: 48)
.foregroundColor(.white)
.background(
Capsule()
.foregroundColor(
vertexProjectID.isEmpty || vertexAuthToken.isEmpty ? .gray.opacity(0.2) : .blue
)
)
}
.padding()
.disabled(vertexProjectID.isEmpty || vertexAuthToken.isEmpty)

Spacer()
}
.navigationTitle("VertexAI Demo")
}
.tabItem {
Image(systemName: "mountain.2.fill")
Text("Vertex")
}

}
}
}
68 changes: 0 additions & 68 deletions Example.swiftpm/DemoView.swift

This file was deleted.

14 changes: 14 additions & 0 deletions Example.swiftpm/Entity/ChatMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// ChatMessage.swift
//
//
// Created by Fumito Ito on 2024/07/05.
//

import Foundation

struct ChatMessage: Identifiable {
let id = UUID()
let user: ChatUser
let text: String
}
13 changes: 13 additions & 0 deletions Example.swiftpm/Entity/ChatUser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ChatUser.swift
//
//
// Created by Fumito Ito on 2024/07/05.
//

import Foundation

enum ChatUser: String {
case user
case assistant
}
18 changes: 18 additions & 0 deletions Example.swiftpm/Extension/Messages+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Messages+Extension.swift
//
//
// Created by Fumito Ito on 2024/07/04.
//

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 {}
Loading

0 comments on commit 4bee087

Please sign in to comment.