-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Server Prompt Templates #1792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Server Prompt Templates #1792
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
92c3619
Build UI for template generate content
paulb777 4d507a3
UI in place for 3 new examples
paulb777 3ed1f5c
Two templates working
paulb777 1c5ed38
missing files
paulb777 bfacc00
style
paulb777 1a15495
Missing files
paulb777 4557a05
[Firebase AI] Add parameters to SPT Chat sample (#1793)
andrewheard 940cabf
Add template details to comments
paulb777 d7c2e89
Remove template chat for now
paulb777 1437684
Update to main Firebase branch
paulb777 48fcc88
Update now that 12.6.0 has released
paulb777 24ac0c4
Update firebaseai/GenerativeAITextExample/ViewModels/GenerateContentF…
paulb777 80809c3
fix style
paulb777 8b50c34
Apply suggestions from code review
paulb777 3686a68
review suggestion typo
paulb777 101b8ca
Update firebaseai/GenerativeAITextExample/ViewModels/GenerateContentF…
paulb777 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
91 changes: 91 additions & 0 deletions
91
firebaseai/GenerativeAITextExample/Screens/GenerateContentFromTemplateScreen.swift
This file contains hidden or 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,91 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import MarkdownUI | ||
| import SwiftUI | ||
| #if canImport(FirebaseAILogic) | ||
| import FirebaseAILogic | ||
| #else | ||
| import FirebaseAI | ||
| #endif | ||
| import GenerativeAIUIComponents | ||
|
|
||
| struct GenerateContentFromTemplateScreen: View { | ||
| let firebaseService: FirebaseAI | ||
| @StateObject var viewModel: GenerateContentFromTemplateViewModel | ||
| @State var userInput = "" | ||
|
|
||
| init(firebaseService: FirebaseAI) { | ||
| self.firebaseService = firebaseService | ||
| _viewModel = | ||
| StateObject( | ||
| wrappedValue: GenerateContentFromTemplateViewModel(firebaseService: firebaseService) | ||
| ) | ||
| } | ||
|
|
||
| enum FocusedField: Hashable { | ||
| case message | ||
| } | ||
|
|
||
| @FocusState | ||
| var focusedField: FocusedField? | ||
|
|
||
| var body: some View { | ||
| VStack { | ||
| VStack(alignment: .leading) { | ||
| Text("Enter your name, then tap on _Go_ to run generateContent from template on it.") | ||
| .padding(.horizontal, 6) | ||
| InputField("Enter your name", text: $userInput) { | ||
| Text("Go") | ||
| } | ||
| .focused($focusedField, equals: .message) | ||
| .onSubmit { onGenerateContentTapped() } | ||
paulb777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| .padding(.horizontal, 16) | ||
|
|
||
| List { | ||
| HStack(alignment: .top) { | ||
| if viewModel.inProgress { | ||
| ProgressView() | ||
| } else { | ||
| Image(systemName: "cloud.circle.fill") | ||
| .font(.title2) | ||
| } | ||
|
|
||
| Markdown("\(viewModel.outputText)") | ||
| } | ||
| .listRowSeparator(.hidden) | ||
| } | ||
| .listStyle(.plain) | ||
| } | ||
| .onTapGesture { | ||
| focusedField = nil | ||
| } | ||
| .navigationTitle("Template Generate Content") | ||
| } | ||
|
|
||
| private func onGenerateContentTapped() { | ||
| focusedField = nil | ||
|
|
||
| Task { | ||
| await viewModel.generateContentFromTemplate(name: userInput) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #Preview { | ||
| NavigationStack { | ||
| GenerateContentFromTemplateScreen(firebaseService: FirebaseAI.firebaseAI()) | ||
| } | ||
| } | ||
92 changes: 92 additions & 0 deletions
92
firebaseai/GenerativeAITextExample/ViewModels/GenerateContentFromTemplateViewModel.swift
This file contains hidden or 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,92 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #if canImport(FirebaseAILogic) | ||
| import FirebaseAILogic | ||
| #else | ||
| import FirebaseAI | ||
| #endif | ||
| import Foundation | ||
| import OSLog | ||
|
|
||
| // Template Details | ||
| // | ||
| // Configuration | ||
| // | ||
| // input: | ||
| // default: | ||
| // language: english | ||
| // schema: | ||
| // name: string | ||
| // language?: string | ||
| // | ||
| // Prompt and system instructions | ||
| // | ||
| // {{role "system"}} | ||
| // The user's name is {{name}}. They prefer to communicate in {{language}}. | ||
| // {{role "user"}} | ||
| // Say hello. | ||
| // | ||
|
|
||
| @MainActor | ||
| class GenerateContentFromTemplateViewModel: ObservableObject { | ||
| private var logger = Logger( | ||
| subsystem: Bundle.main.bundleIdentifier ?? "com.google.firebase.quickstart.FirebaseAIExample", | ||
| category: "generative-ai" | ||
| ) | ||
|
|
||
| @Published | ||
| var outputText = "" | ||
|
|
||
| @Published | ||
| var errorMessage: String? | ||
|
|
||
| @Published | ||
| var inProgress = false | ||
|
|
||
| private var model: TemplateGenerativeModel? | ||
|
|
||
| init(firebaseService: FirebaseAI) { | ||
| model = firebaseService.templateGenerativeModel() | ||
| } | ||
|
|
||
| func generateContentFromTemplate(name: String) async { | ||
| defer { | ||
| inProgress = false | ||
| } | ||
| guard let model else { | ||
| return | ||
| } | ||
|
|
||
| do { | ||
| inProgress = true | ||
| errorMessage = nil | ||
| outputText = "" | ||
|
|
||
| let response = try await model.generateContent( | ||
| templateID: "apple-qs-greeting", | ||
| inputs: [ | ||
| "name": name, | ||
| "language": "Spanish", | ||
| ] | ||
paulb777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| if let text = response.text { | ||
| outputText = text | ||
| } | ||
| } catch { | ||
| logger.error("\(error.localizedDescription)") | ||
| errorMessage = error.localizedDescription | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.