1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| %pip install google-adk
from google.adk.agents import LlmAgent from google.adk.runners import InMemoryRunner from google.adk.tools import google_search, AgentTool, ToolContext from google.genai import types
from google.adk.models.google_llm import Gemini from google.adk.sessions import InMemorySessionService from google.adk.code_executors import BuiltInCodeExecutor
# 連線設定 def show_python_code_and_result(response): ... retry_config = types.HttpRetryOptions(...)
# 付款方式的匯率 def get_fee_for_payment_method(method: str) -> dict: ...
# 匯差 def get_exchange_rate(base_currency: str, target_currency: str) -> dict: ...
# 自訂Python Method Tools currency_agent = LlmAgent( name="currency_agent", model=Gemini(model="gemini-2.5-flash-lite", retry_options=retry_config), instruction="...提示詞...", tools=[get_fee_for_payment_method, get_exchange_rate], )
# 使用Agent換匯 currency_runner = InMemoryRunner(agent=currency_agent) _ = await currency_runner.run_debug( "I want to convert 500 US Dollars to Euros using my Platinum Credit Card. How much will I receive?" )
# 也可以用ADK內建的code_executor Tool calculation_agent = LlmAgent( ..., code_executor=BuiltInCodeExecutor(), )
# 將Agent加為另一個tool
enhanced_currency_agent = LlmAgent( ..., tools=[ get_fee_for_payment_method, get_exchange_rate, AgentTool(agent=calculation_agent), # Using another agent as a tool! ], )
# 撰寫MCP Tools並在Agent使用
mcp_image_server = McpToolset( connection_params=StdioConnectionParams( server_params=StdioServerParameters( command="npx", # Run MCP server via npx args=[ "-y", # Argument for npx to auto-confirm install "@modelcontextprotocol/server-everything", ], tool_filter=["getTinyImage"], ), timeout=30, ) )
image_agent = LlmAgent( ... tools=[mcp_image_server],
|