feat(ai): GeminiLanguageModel thought summaries - #16623
Conversation
Add support for configuring and observing Gemini internal reasoning and thought summaries across model, profile, and request layers. - Add `GeminiLanguageModel.Thinking` to configure default thought summary modes (`.auto`, `.off`) on `GeminiLanguageModel` and `FirebaseAI`. - Add `LanguageModelSession.DynamicProfile` extensions `.geminiThinking` to enable thought summaries and observe incoming reasoning thoughts without mutating the underlying model instance. - Expose `session.properties.geminiThoughtSummary` as observable state managed automatically during session turns. - Provide zero-synchronization `geminiThoughtSummary` properties on `LanguageModelSession.Response`, `ResponseStream.Snapshot`, and `Transcript`. - Introduce `GeminiRequestMetadata` to enable per-turn thought summary overrides via `session.respond(metadata:)`. - Map Apple's `ContextOptions.ReasoningLevel` (`.light`, `.moderate`, `.deep`, `.custom`) to Gemini's thinking level in the request translator. - Add unit and integration tests covering thought summaries, profile modifiers, request metadata precedence, and deep reasoning.
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. |
9d8872a to
7733eb2
Compare
Added support for configuring Gemini thinking levels (reasoningLevel) and thought summaries within Apple's
FoundationModelsframework. These can be configured at the model level, with dynamic session profiles, or per request.Apple's
FoundationModelsframework models reasoning depth usingContextOptions.ReasoningLevel(.light,.moderate,.deep), but does not have a native representation for configuring Gemini-specific thought summaries. This PR bridges the two systems cleanly:ContextOptions.ReasoningLevelacross profiles and request options.summaries: .auto/.off) that align with the Gemini Interactions API naming.Key Features & Architecture
1. Model Configuration (
GeminiLanguageModel.Thinking)GeminiLanguageModel.Thinkingto set the default thought summary mode (.autoor.off) onGeminiLanguageModelandFirebaseAI.2. Dynamic Profile Modifiers (
.geminiThinking)geminiThinking(summaries:)andgeminiThinking(perform:)toLanguageModelSession.DynamicProfile.GeminiRequestMetadatato outgoing prompts in session history without mutating or re-instantiating the underlying model.session.properties.geminiThoughtSummaryas@Observablestate across conversation turns.3. Direct, Zero-Synchronization Inspection
geminiThoughtSummary: String?directly on:LanguageModelSession.ResponseLanguageModelSession.ResponseStream.Snapshot(for streaming)TranscriptMutex, or async stream tapping to retrieve the generated reasoning summary.4. Per-Turn Request Metadata (
GeminiRequestMetadata)GeminiRequestMetadataconforming toConvertibleToGeneratedContentandConvertibleFromGeneratedContent."gemini"metadata namespace to keep the session metadata clean..gemini(thinkingSummaries: .auto)for convenient per-request configuration.5. Standard Apple Reasoning Level Mapping
ContextOptions.ReasoningLeveldirectly to Gemini'sThinkingConfig.thinkingLevel:.light->.low.moderate->.medium.deep->.high.custom(...)-> case-insensitive string mapping ("MINIMAL","LOW","MEDIUM","HIGH").6. Strict Precedence Hierarchy
The request translator resolves thinking configurations hierarchically:
Usage
Examples
Example 1: Model Default Configuration
Configure thought summaries globally when initializing the model:
Example 2: Dynamic Profile with SwiftUI Observability
Bind a SwiftUI view directly to
session.properties.geminiThoughtSummarywithoutany manual state management or synchronization:
Example 3: Live Thought Streaming with
perform:Receive thought updates during the generation cycle using the observer modifier:
Example 4: Streaming Snapshot Inspection
Inspect partial thought summaries in real time while streaming response tokens:
Example 5: Per-Turn Request Metadata Override
Enable thought summaries and deep reasoning for a single difficult query without
altering session- or model-level settings:
#no-changelog