73
73
"""
74
74
75
75
76
- class GeminiModelSettings (ModelSettings ):
76
+ class GeminiModelSettings (ModelSettings , total = False ):
77
77
"""Settings used for a Gemini model request.
78
78
79
79
ALL FIELDS MUST BE `gemini_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS.
80
80
"""
81
81
82
82
gemini_safety_settings : list [GeminiSafetySettings ]
83
83
84
+ gemini_thinking_config : ThinkingConfig
85
+ """Thinking is "on" by default in both the API and AI Studio.
86
+
87
+ Being on by default doesn't mean the model will send back thoughts. For that, you would need to set `include_thoughts`
88
+ to `True`, but since end of January 2025, `thoughts` are not returned anymore, and are only displayed in the Google
89
+ AI Studio. See https://discuss.ai.google.dev/t/thoughts-are-missing-cot-not-included-anymore/63653 for more details.
90
+
91
+ If you want to avoid the model spending any tokens on thinking, you can set `thinking_budget` to `0`.
92
+
93
+ See more about it on <https://ai.google.dev/gemini-api/docs/thinking>.
94
+ """
95
+
84
96
85
97
@dataclass (init = False )
86
98
class GeminiModel (Model ):
@@ -223,7 +235,9 @@ async def _make_request(
223
235
generation_config ['presence_penalty' ] = presence_penalty
224
236
if (frequency_penalty := model_settings .get ('frequency_penalty' )) is not None :
225
237
generation_config ['frequency_penalty' ] = frequency_penalty
226
- if (gemini_safety_settings := model_settings .get ('gemini_safety_settings' )) != []:
238
+ if (thinkingConfig := model_settings .get ('gemini_thinking_config' )) is not None :
239
+ generation_config ['thinking_config' ] = thinkingConfig # pragma: no cover
240
+ if (gemini_safety_settings := model_settings .get ('gemini_safety_settings' )) is not None :
227
241
request_data ['safetySettings' ] = gemini_safety_settings
228
242
if generation_config :
229
243
request_data ['generationConfig' ] = generation_config
@@ -497,6 +511,16 @@ class GeminiSafetySettings(TypedDict):
497
511
"""
498
512
499
513
514
+ class ThinkingConfig (TypedDict , total = False ):
515
+ """The thinking features configuration."""
516
+
517
+ include_thoughts : Annotated [bool , pydantic .Field (alias = 'includeThoughts' )]
518
+ """Indicates whether to include thoughts in the response. If true, thoughts are returned only if the model supports thought and thoughts are available."""
519
+
520
+ thinking_budget : Annotated [int , pydantic .Field (alias = 'thinkingBudget' )]
521
+ """Indicates the thinking budget in tokens."""
522
+
523
+
500
524
class _GeminiGenerationConfig (TypedDict , total = False ):
501
525
"""Schema for an API request to the Gemini API.
502
526
@@ -511,6 +535,7 @@ class _GeminiGenerationConfig(TypedDict, total=False):
511
535
presence_penalty : float
512
536
frequency_penalty : float
513
537
stop_sequences : list [str ]
538
+ thinking_config : ThinkingConfig
514
539
515
540
516
541
class _GeminiContent (TypedDict ):
0 commit comments