|
| 1 | +# Copyright 2025 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 | +"""Sample agent demonstrating DebugLoggingPlugin usage. |
| 16 | +
|
| 17 | +This sample shows how to use the DebugLoggingPlugin to capture complete |
| 18 | +debug information (LLM requests/responses, tool calls, events, session state) |
| 19 | +to a YAML file for debugging purposes. |
| 20 | +
|
| 21 | +Usage: |
| 22 | + adk run contributing/samples/plugin_debug_logging |
| 23 | +
|
| 24 | +After running, check the generated `adk_debug.yaml` file for detailed logs. |
| 25 | +""" |
| 26 | + |
| 27 | +from typing import Any |
| 28 | + |
| 29 | +from google.adk.agents import LlmAgent |
| 30 | +from google.adk.apps import App |
| 31 | +from google.adk.plugins import DebugLoggingPlugin |
| 32 | + |
| 33 | + |
| 34 | +def get_weather(city: str) -> dict[str, Any]: |
| 35 | + """Get the current weather for a city. |
| 36 | +
|
| 37 | + Args: |
| 38 | + city: The name of the city to get weather for. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + A dictionary containing weather information. |
| 42 | + """ |
| 43 | + # Simulated weather data |
| 44 | + weather_data = { |
| 45 | + "new york": {"temperature": 22, "condition": "sunny", "humidity": 45}, |
| 46 | + "london": {"temperature": 15, "condition": "cloudy", "humidity": 70}, |
| 47 | + "tokyo": {"temperature": 28, "condition": "humid", "humidity": 85}, |
| 48 | + "paris": {"temperature": 18, "condition": "rainy", "humidity": 80}, |
| 49 | + } |
| 50 | + |
| 51 | + city_lower = city.lower() |
| 52 | + if city_lower in weather_data: |
| 53 | + data = weather_data[city_lower] |
| 54 | + return { |
| 55 | + "city": city, |
| 56 | + "temperature_celsius": data["temperature"], |
| 57 | + "condition": data["condition"], |
| 58 | + "humidity_percent": data["humidity"], |
| 59 | + } |
| 60 | + else: |
| 61 | + return { |
| 62 | + "city": city, |
| 63 | + "error": f"Weather data not available for {city}", |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +def calculate(expression: str) -> dict[str, Any]: |
| 68 | + """Evaluate a simple mathematical expression. |
| 69 | +
|
| 70 | + Args: |
| 71 | + expression: A mathematical expression to evaluate (e.g., "2 + 2"). |
| 72 | +
|
| 73 | + Returns: |
| 74 | + A dictionary containing the result or error. |
| 75 | + """ |
| 76 | + try: |
| 77 | + # Only allow safe mathematical operations |
| 78 | + allowed_chars = set("0123456789+-*/.() ") |
| 79 | + if not all(c in allowed_chars for c in expression): |
| 80 | + return {"error": "Invalid characters in expression"} |
| 81 | + |
| 82 | + result = eval(expression) # Safe due to character restriction |
| 83 | + return {"expression": expression, "result": result} |
| 84 | + except Exception as e: |
| 85 | + return {"expression": expression, "error": str(e)} |
| 86 | + |
| 87 | + |
| 88 | +# Sample queries to try: |
| 89 | +# - "What's the weather in Tokyo?" |
| 90 | +# - "Calculate 15 * 7 + 3" |
| 91 | +# - "What's the weather in London and calculate 100 / 4" |
| 92 | +root_agent = LlmAgent( |
| 93 | + name="debug_demo_agent", |
| 94 | + description="A demo agent that shows DebugLoggingPlugin capabilities", |
| 95 | + instruction="""You are a helpful assistant that can: |
| 96 | +1. Get weather information for cities (New York, London, Tokyo, Paris) |
| 97 | +2. Perform simple calculations |
| 98 | +
|
| 99 | +When asked about weather, use the get_weather tool. |
| 100 | +When asked to calculate, use the calculate tool. |
| 101 | +Be concise in your responses.""", |
| 102 | + model="gemini-2.0-flash", |
| 103 | + tools=[get_weather, calculate], |
| 104 | +) |
| 105 | + |
| 106 | + |
| 107 | +# Create the app with DebugLoggingPlugin |
| 108 | +# The plugin will write detailed debug information to adk_debug.yaml |
| 109 | +app = App( |
| 110 | + name="plugin_debug_logging", |
| 111 | + root_agent=root_agent, |
| 112 | + plugins=[ |
| 113 | + # DebugLoggingPlugin captures complete interaction data to a YAML file |
| 114 | + # Options: |
| 115 | + # output_path: Path to output file (default: "adk_debug.yaml") |
| 116 | + # include_session_state: Include session state snapshot (default: True) |
| 117 | + # include_system_instruction: Include full system instruction (default: True) |
| 118 | + DebugLoggingPlugin( |
| 119 | + output_path="adk_debug.yaml", |
| 120 | + include_session_state=True, |
| 121 | + include_system_instruction=True, |
| 122 | + ), |
| 123 | + ], |
| 124 | +) |
0 commit comments