Skip to content

Commit

Permalink
Add a Swift Playground as an example
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard committed Dec 14, 2023
1 parent a155dbd commit c10b7be
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Examples/GoogleAIPlayground.swiftpm/GenerateTextView.swift
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 Examples/GoogleAIPlayground.swiftpm/GoogleAIPlaygroundApp.swift
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()
}
}
}
53 changes: 53 additions & 0 deletions Examples/GoogleAIPlayground.swiftpm/Package.swift
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"),
]
),
]
)

0 comments on commit c10b7be

Please sign in to comment.