-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: allow thinking_config in generate_content_config (#4108) #4117
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
base: main
Are you sure you want to change the base?
Changes from all commits
0883d5f
2d485cb
25a9186
c5aa50e
abce588
3a492da
d8dd6c9
bec52bb
a631439
412447a
8438ed6
4b23903
072f8ad
74600d8
f137045
56526d9
978d7d1
01462c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,7 +285,7 @@ class LlmAgent(BaseAgent): | |
| """The additional content generation configurations. | ||
|
|
||
| NOTE: not all fields are usable, e.g. tools must be configured via `tools`, | ||
| thinking_config must be configured via `planner` in LlmAgent. | ||
| thinking_config can be configured here or via the `planner`. If both are set, the planner's configuration takes precedence. | ||
|
|
||
| For example: use this config to adjust model temperature, configure safety | ||
| settings, etc. | ||
|
|
@@ -849,8 +849,6 @@ def validate_generate_content_config( | |
| ) -> types.GenerateContentConfig: | ||
| if not generate_content_config: | ||
| return types.GenerateContentConfig() | ||
| if generate_content_config.thinking_config: | ||
| raise ValueError('Thinking config should be set via LlmAgent.planner.') | ||
| if generate_content_config.tools: | ||
| raise ValueError('All tools must be set via LlmAgent.tools.') | ||
| if generate_content_config.system_instruction: | ||
|
|
@@ -862,6 +860,28 @@ def validate_generate_content_config( | |
| 'Response schema must be set via LlmAgent.output_schema.' | ||
| ) | ||
| return generate_content_config | ||
|
|
||
| def model_post_init(self, __context: Any) -> None: | ||
| """Provides a warning if multiple thinking configurations are found.""" | ||
| super().model_post_init(__context) | ||
|
|
||
| # Check if thinking_config is set in both the model config and the planner. | ||
| # Using getattr for consistency in checking optional attributes. | ||
| has_manual_thinking_config = ( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "manual": This is not a good naming in my opinion. But see the other comment. |
||
| getattr(self.generate_content_config, 'thinking_config', None) is not None | ||
| ) | ||
| planner_has_thinking_config = ( | ||
| getattr(self.planner, 'thinking_config', None) is not None | ||
| ) | ||
Akshat8510 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if has_manual_thinking_config and planner_has_thinking_config: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just write |
||
| warnings.warn( | ||
| 'Both `thinking_config` in `generate_content_config` and an ' | ||
| 'agent `planner` with thinking enabled are provided. The ' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer phrasing it as " |
||
| 'planner\'s configuration will take precedence.', | ||
| UserWarning, | ||
| stacklevel=2, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be 3 if you want to point to where user creates the LlmAgent. |
||
| ) | ||
|
|
||
| @classmethod | ||
| @experimental | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
|
|
||
| from typing import List | ||
| from typing import Optional | ||
| import logging | ||
|
|
||
| from google.genai import types | ||
| from typing_extensions import override | ||
|
|
@@ -57,6 +58,11 @@ def apply_thinking_config(self, llm_request: LlmRequest) -> None: | |
| """ | ||
| if self.thinking_config: | ||
| llm_request.config = llm_request.config or types.GenerateContentConfig() | ||
| if llm_request.config.thinking_config: | ||
| logging.info( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use hierarchical logger in ADK codebase instead of the root logger. https://google.github.io/adk-docs/observability/logging/#logging-philosophy |
||
| 'Overwriting `thinking_config` from `generate_content_config` with ' | ||
| 'the one provided by the `BuiltInPlanner`.' | ||
| ) | ||
| llm_request.config.thinking_config = self.thinking_config | ||
|
|
||
| @override | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@OverRide ?