教程 7:插件 您将学到的内容 插件基础:了解什么是插件以及它们的工作原理 全局回调管理:使用插件处理横切关注点 实用插件示例:日志记录、监控和请求修改 核心概念:插件 Google ADK 中的插件是自定义代码模块,可通过回调钩子在代理工作流生命周期的各个阶段执行。与针对单个代理或工具配置的常规回调不同,插件只需在 上注册一次,即可全局应用于该运行器管理的所有代理、工具和 LLM 调用。 插件与回调对比 插件生命周期钩子 为什么要使用插件?
Google ADK 中的插件是自定义代码模块,可通过回调钩子在代理工作流生命周期的各个阶段执行。与针对单个代理或工具配置的常规回调不同,插件只需在 Runner 上注册一次,即可全局应用于该运行器管理的所有代理、工具和 LLM 调用。
┌─────────────────┐ ┌─────────────────┐ │ Regular │ │ Plugin │ │ Callbacks │ │ Callbacks │ ├─────────────────┤ ├─────────────────┤ │ • Per agent │ │ • Global scope │ │ • Per tool │ │ • Runner level │ │ • Specific task │ │ • Cross-cutting │ │ • Local effect │ │ • Reusable │ └─────────────────┘ └─────────────────┘
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ User Message │───▶│ Runner Start │───▶│ Agent Execute │ │ Callback │ │ Callback │ │ Callback │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ │ │ ▼ ▼ ▼ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ Model Callback │ │ Tool Callback │ │ Event Callback │ │ (before/after)│ │ (before/after)│ │ (modify event)│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ │ │ ▼ ▼ ▼ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ Error Handling │ │ Runner End │ │ Cleanup Tasks │ │ Callback │ │ Callback │ │ & Reporting │ └─────────────────┘ └─────────────────┘ └─────────────────┘
本教程通过一个结合多个用例的实战示例,演示如何在 Google ADK 中创建和使用插件:
7_plugins/ ├── README.md # This file - concept explanation ├── agent.py # Agent implementation with plugin ├── app.py # Streamlit interface ├── requirements.txt # Dependencies └── plugin_example.py # Standalone plugin demonstration
完成本教程后,您将理解:
.env 的文件,并填写:
GOOGLE_API_KEY=your_google_ai_studio_api_key_here
pip install -r requirements.txtImportant:
.env file is in the same directory as the tutorial filesyour_google_ai_studio_api_key_here with your actual API key.env 文件不应提交到版本控制cd 7_plugins streamlit run app.py
from google.adk.plugins.base_plugin import BasePlugin class MyPlugin(BasePlugin): def __init__(self): super().__init__(name="my_plugin") # Initialize plugin state async def before_agent_callback(self, *, agent, callback_context): # Called before each agent execution pass async def after_model_callback(self, *, callback_context, llm_response): # Called after each model call pass
from google.adk.runners import InMemoryRunner runner = InMemoryRunner( agent=my_agent, app_name="my_app", plugins=[MyPlugin()] # Register plugins here )
on_user_message_callback: Modify user inputbefore_run_callback: Setup before executionbefore_agent_callback / after_agent_callback: Agent lifecyclebefore_model_callback / after_model_callback: Model interactionsbefore_tool_callback / after_tool_callback: Tool executionon_model_error_callback / on_tool_error_callback: Error handlingon_event_callback: Modify events before streamingafter_run_callback: Cleanup after executionAfter completing this tutorial, you can:
"Missing key inputs argument" Error
.env file with your Google AI Studio API key.env file is in the same directory as the tutorial filesImport Errors
pip install -r requirements.txt插件无法正常工作
免责声明:
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文(母语)文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。