|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from typing import Generator |
| 18 | + |
| 19 | +from google.adk import Agent |
| 20 | +from google.adk import Event |
| 21 | +from google.adk import Workflow |
| 22 | +from google.adk.workflow import node |
| 23 | +from pydantic import BaseModel |
| 24 | +from pydantic import Field |
| 25 | + |
| 26 | + |
| 27 | +# 1. Define schemas |
| 28 | +class CustomerLookupArgs(BaseModel): |
| 29 | + user_id: str = Field(description="The customer's unique identifier.") |
| 30 | + |
| 31 | + |
| 32 | +# 2. Define a regular Node using the @node decorator. |
| 33 | +# This Node is wrapped as a NodeTool automatically by the Agent. |
| 34 | +# As a NodeTool, it has the ability to yield intermediate Events during execution. |
| 35 | +@node |
| 36 | +def calculate_discount(tier: str, ctx) -> Generator[Event | str, None, None]: |
| 37 | + """Calculates the discount percentage based on customer tier. |
| 38 | +
|
| 39 | + Args: |
| 40 | + tier: The customer's membership tier (e.g., VIP, Standard). |
| 41 | + """ |
| 42 | + yield Event(message=f"Checking discount rules for tier '{tier}'...") |
| 43 | + discount = "20% off" if "VIP" in tier else "5% off" |
| 44 | + yield discount |
| 45 | + |
| 46 | + |
| 47 | +# 3. Define a Workflow. |
| 48 | +# This Workflow is wrapped as a NodeTool automatically by the Agent. |
| 49 | +def lookup_customer_data(node_input: CustomerLookupArgs, ctx) -> dict[str, str]: |
| 50 | + return {"user_id": node_input.user_id, "tier": "Verified VIP Member"} |
| 51 | + |
| 52 | + |
| 53 | +customer_lookup_workflow = Workflow( |
| 54 | + name="customer_lookup_workflow", |
| 55 | + description="Looks up customer status and tier by user_id.", |
| 56 | + input_schema=CustomerLookupArgs, |
| 57 | + edges=[ |
| 58 | + ("START", lookup_customer_data), |
| 59 | + ], |
| 60 | +) |
| 61 | + |
| 62 | + |
| 63 | +# 4. Define the Agent that uses both Node and Workflow as tools. |
| 64 | +root_agent = Agent( |
| 65 | + name="customer_service_agent", |
| 66 | + instruction=""" |
| 67 | + You are a customer service assistant. |
| 68 | + 1. First, call `customer_lookup_workflow` using the user_id to get their membership tier. |
| 69 | + 2. Then, call `calculate_discount` node with that tier to find out what discount they get. |
| 70 | + Summarize these details for the customer. |
| 71 | + """, |
| 72 | + tools=[customer_lookup_workflow, calculate_discount], |
| 73 | +) |
0 commit comments