mirror of
https://github.com/google/adk-docs.git
synced 2026-09-14 16:16:59 +08:00
22 lines
957 B
Python
22 lines
957 B
Python
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.
|