-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[AI] Server Prompt Templates #15402
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
[AI] Server Prompt Templates #15402
Changes from 39 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
216a06d
[AI] Server Prompt Templates
paulb777 405d5fe
copyrights
paulb777 c3c03be
Checkpoint after trying to add imagen implementation
paulb777 953b3f0
end to end testGenerateContentWithText passes
paulb777 daf7b4e
FirebaseAI/Tests/Unit/TemplateGenerativeModelTests.swift
paulb777 29c79b1
CI fixes
paulb777 6aa13e0
Streaming APIs
paulb777 e75d7e9
chat history refactor
paulb777 6cc6c9d
Re-sort tests
paulb777 ac996fd
Fix TemplateImagenModel to encode variables as inputs
google-labs-jules[bot] 7ba2c28
Fix: Correct URL construction for image generation requests
google-labs-jules[bot] dd66187
checkpoint
paulb777 b3c28d1
Imagen generate test works
paulb777 705d0c2
fix template imagen response return type
paulb777 17fd0b8
existing tests passing
paulb777 c4068e9
build fix
paulb777 7af117c
style
paulb777 32bc581
Add streaming integration tests
paulb777 1d162dc
testChatStream
paulb777 d7beef0
unit tests run
paulb777 fd8f76a
streaming unit tests
paulb777 a49dee1
existing mock files for template unit tests
paulb777 3f49b09
streamline integration tests
paulb777 3f3607a
fixes
paulb777 64df672
Templates do not have an implicit .prompt suffix and only global loca…
paulb777 9f91c21
fixes
paulb777 fa4a3f4
self review, including file/naming updates
paulb777 7bd5d2c
Update Integration test xcodeproj after rebase
paulb777 eeb9e66
module rename updates after rebase for unit tests
paulb777 1840566
[Firebase AI] Refactor SPT integration tests to Swift Testing (#15426)
andrewheard 67d40eb
Continue migration from template templates to project templates
paulb777 6338983
Rename template variables to template inputs
paulb777 a11c20c
template to templateID
paulb777 bc4ab28
doc updates
paulb777 5c784c7
Add sendMessage modelContent variations
paulb777 60f32ef
Convert tests to use test project prompt templates
paulb777 fe3ae25
Eliminate force unwraps in url creation
paulb777 f63c4c0
misc review fixes
paulb777 83a0f3a
aligned structure and naming
paulb777 9ee5acc
Update FirebaseAI/Sources/AILog.swift
paulb777 287dd12
Build fixes
paulb777 f705699
Make template chat APIs internal for now
paulb777 fae5ca9
style
paulb777 98a2ba4
us-central1 is now supported for vertexAI
paulb777 085cc97
Update FirebaseAI/Sources/AILog.swift
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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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,94 @@ | ||
| // 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 Foundation | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| final class History: Sendable { | ||
| private let historyLock = NSLock() | ||
| private nonisolated(unsafe) var _history: [ModelContent] = [] | ||
| /// The previous content from the chat that has been successfully sent and received from the | ||
| /// model. This will be provided to the model for each message sent as context for the discussion. | ||
| public var history: [ModelContent] { | ||
| get { | ||
| historyLock.withLock { _history } | ||
| } | ||
| set { | ||
| historyLock.withLock { _history = newValue } | ||
| } | ||
| } | ||
|
|
||
| init(history: [ModelContent]) { | ||
| self.history = history | ||
| } | ||
|
|
||
| func append(contentsOf: [ModelContent]) { | ||
| historyLock.withLock { | ||
| _history.append(contentsOf: contentsOf) | ||
| } | ||
| } | ||
|
|
||
| func append(_ newElement: ModelContent) { | ||
| historyLock.withLock { | ||
| _history.append(newElement) | ||
| } | ||
| } | ||
|
|
||
| func aggregatedChunks(_ chunks: [ModelContent]) -> ModelContent { | ||
| var parts: [InternalPart] = [] | ||
| var combinedText = "" | ||
| var combinedThoughts = "" | ||
|
|
||
| func flush() { | ||
| if !combinedThoughts.isEmpty { | ||
| parts.append(InternalPart(.text(combinedThoughts), isThought: true, thoughtSignature: nil)) | ||
| combinedThoughts = "" | ||
| } | ||
| if !combinedText.isEmpty { | ||
| parts.append(InternalPart(.text(combinedText), isThought: nil, thoughtSignature: nil)) | ||
| combinedText = "" | ||
| } | ||
| } | ||
|
|
||
| // Loop through all the parts, aggregating the text. | ||
| for part in chunks.flatMap({ $0.internalParts }) { | ||
| // Only text parts may be combined. | ||
| if case let .text(text) = part.data, part.thoughtSignature == nil { | ||
| // Thought summaries must not be combined with regular text. | ||
| if part.isThought ?? false { | ||
| // If we were combining regular text, flush it before handling "thoughts". | ||
| if !combinedText.isEmpty { | ||
| flush() | ||
| } | ||
| combinedThoughts += text | ||
| } else { | ||
| // If we were combining "thoughts", flush it before handling regular text. | ||
| if !combinedThoughts.isEmpty { | ||
| flush() | ||
| } | ||
| combinedText += text | ||
| } | ||
| } else { | ||
| // This is a non-combinable part (not text), flush any pending text. | ||
| flush() | ||
| parts.append(part) | ||
| } | ||
| } | ||
|
|
||
| // Flush any remaining text. | ||
| flush() | ||
|
|
||
| return ModelContent(role: "model", parts: parts) | ||
| } | ||
| } |
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.