mirror of
https://github.com/google/adk-docs.git
synced 2026-09-14 16:16:59 +08:00
3b350ea25b
Three example files were missing the required Google license header, which fails the License Header Lint CI check. Prepend the standard header.
36 lines
1.5 KiB
Python
36 lines
1.5 KiB
Python
# 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.
|
|
|
|
from google.adk.tools import ToolContext, FunctionTool
|
|
|
|
def update_user_preference(preference: str, value: str, tool_context: ToolContext):
|
|
"""Updates a user-specific preference."""
|
|
user_prefs_key = "user:preferences"
|
|
# Get current preferences or initialize if none exist
|
|
preferences = tool_context.state.get(user_prefs_key, {})
|
|
preferences[preference] = value
|
|
# Write the updated dictionary back to the state
|
|
tool_context.state[user_prefs_key] = preferences
|
|
print(f"Tool: Updated user preference '{preference}' to '{value}'")
|
|
return {"status": "success", "updated_preference": preference}
|
|
|
|
pref_tool = FunctionTool(func=update_user_preference)
|
|
|
|
# In an Agent:
|
|
# my_agent = Agent(..., tools=[pref_tool])
|
|
|
|
# When the LLM calls update_user_preference(preference='theme', value='dark', ...):
|
|
# The tool_context.state will be updated, and the change will be part of the
|
|
# resulting tool response event's actions.state_delta.
|