diff --git a/Examples/GoogleAIPlayground.swiftpm/GenerateTextView.swift b/Examples/GoogleAIPlayground.swiftpm/GenerateTextView.swift new file mode 100644 index 0000000..ca1549e --- /dev/null +++ b/Examples/GoogleAIPlayground.swiftpm/GenerateTextView.swift @@ -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 + } + } + } +} diff --git a/Examples/GoogleAIPlayground.swiftpm/GoogleAIPlaygroundApp.swift b/Examples/GoogleAIPlayground.swiftpm/GoogleAIPlaygroundApp.swift new file mode 100644 index 0000000..bf8bc0d --- /dev/null +++ b/Examples/GoogleAIPlayground.swiftpm/GoogleAIPlaygroundApp.swift @@ -0,0 +1,11 @@ +import GoogleGenerativeAI +import SwiftUI + +@main +struct GoogleAIPlaygroundApp: App { + var body: some Scene { + WindowGroup { + GenerateTextView() + } + } +} diff --git a/Examples/GoogleAIPlayground.swiftpm/Package.swift b/Examples/GoogleAIPlayground.swiftpm/Package.swift new file mode 100644 index 0000000..83e9cd1 --- /dev/null +++ b/Examples/GoogleAIPlayground.swiftpm/Package.swift @@ -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"), + ] + ), + ] +)