-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Swift Playground as an example
- Loading branch information
1 parent
a155dbd
commit c10b7be
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
Examples/GoogleAIPlayground.swiftpm/GenerateTextView.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,44 @@ | ||
import GoogleGenerativeAI | ||
import SwiftUI | ||
|
||
struct GenerateTextView: View { | ||
let model = GenerativeModel( | ||
name: "gemini-pro", | ||
// Enter your API key below; do not share or commit it. | ||
apiKey: "MY_API_KEY" | ||
) | ||
|
||
@State var text: String = "" | ||
@State var prompt: String = "" | ||
|
||
var body: some View { | ||
VStack { | ||
ScrollView { | ||
Text(text) | ||
} | ||
Spacer() | ||
HStack { | ||
TextField("Enter a prompt...", text: $prompt) | ||
Button("Send", systemImage: "arrow.up.circle.fill") { | ||
generateText() | ||
}.labelStyle(.iconOnly) | ||
} | ||
}.padding() | ||
} | ||
|
||
func generateText() { | ||
Task { | ||
do { | ||
let response = try await model.generateContent(prompt) | ||
guard let responseText = response.text else { | ||
text = "No text in response" | ||
return | ||
} | ||
text = responseText | ||
} catch { | ||
print(error) | ||
text = error.localizedDescription | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Examples/GoogleAIPlayground.swiftpm/GoogleAIPlaygroundApp.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,11 @@ | ||
import GoogleGenerativeAI | ||
import SwiftUI | ||
|
||
@main | ||
struct GoogleAIPlaygroundApp: App { | ||
var body: some Scene { | ||
WindowGroup { | ||
GenerateTextView() | ||
} | ||
} | ||
} |
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,53 @@ | ||
// swift-tools-version: 5.9 | ||
|
||
// WARNING: | ||
// This file is automatically generated. | ||
// Do not edit it by hand because the contents will be replaced. | ||
|
||
import AppleProductTypes | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "Google AI Playground", | ||
platforms: [ | ||
.iOS("16.0"), | ||
], | ||
products: [ | ||
.iOSApplication( | ||
name: "Google AI Playground", | ||
targets: ["AppModule"], | ||
displayVersion: "1.0", | ||
bundleVersion: "1", | ||
appIcon: .placeholder(icon: .pencil), | ||
accentColor: .presetColor(.blue), | ||
supportedDeviceFamilies: [ | ||
.pad, | ||
.phone, | ||
], | ||
supportedInterfaceOrientations: [ | ||
.portrait, | ||
.landscapeRight, | ||
.landscapeLeft, | ||
.portraitUpsideDown(.when(deviceFamilies: [.pad])), | ||
], | ||
capabilities: [ | ||
.outgoingNetworkConnections(), | ||
] | ||
), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/google/generative-ai-swift.git", "0.4.3" ..< "0.5.0"), | ||
], | ||
targets: [ | ||
.executableTarget( | ||
name: "AppModule", | ||
dependencies: [ | ||
.product(name: "GoogleGenerativeAI", package: "generative-ai-swift"), | ||
], | ||
path: ".", | ||
swiftSettings: [ | ||
.enableUpcomingFeature("BareSlashRegexLiterals"), | ||
] | ||
), | ||
] | ||
) |