|
| 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 | +"""Example agent demonstrating the use of SkillToolset with GCS. |
| 16 | +
|
| 17 | +Set the following environment variables before running: |
| 18 | +SAMPLE_SKILLS_SANDBOX_RESOURCE_NAME="projects/{PROJECT_NUMBER}/locations/{LOCATION}/reasoningEngines/{ENGINE_ID}/sandboxEnvironments/{SANDBOX_ID}" |
| 19 | +SAMPLE_SKILLS_AGENT_ENGINE_RESOURCE_NAME="projects/{PROJECT_NUMBER}/locations/{LOCATION}/reasoningEngines/{ENGINE_ID}" |
| 20 | +
|
| 21 | +Go to parent directory and run with `adk web --host=0.0.0.0`. |
| 22 | +""" |
| 23 | + |
| 24 | +import asyncio |
| 25 | +import logging |
| 26 | +import os |
| 27 | + |
| 28 | +from google.adk import Agent |
| 29 | +from google.adk import Runner |
| 30 | +from google.adk.code_executors.agent_engine_sandbox_code_executor import AgentEngineSandboxCodeExecutor |
| 31 | +from google.adk.plugins import LoggingPlugin |
| 32 | +from google.adk.skills import list_skills_in_gcs_dir |
| 33 | +from google.adk.skills import load_skill_from_gcs_dir |
| 34 | +from google.adk.tools.skill_toolset import SkillToolset |
| 35 | + |
| 36 | +# Define the GCS bucket and skills prefix |
| 37 | +BUCKET_NAME = "sample-skills" |
| 38 | +SKILLS_PREFIX = "static-skills" |
| 39 | + |
| 40 | +logging.info("Loading skills from gs://%s/%s...", BUCKET_NAME, SKILLS_PREFIX) |
| 41 | + |
| 42 | +# List and load skills from GCS |
| 43 | +skills = [] |
| 44 | +try: |
| 45 | + available_skills = list_skills_in_gcs_dir( |
| 46 | + bucket_name=BUCKET_NAME, skills_base_path=SKILLS_PREFIX |
| 47 | + ) |
| 48 | + for skill_id in available_skills.keys(): |
| 49 | + skills.append( |
| 50 | + load_skill_from_gcs_dir( |
| 51 | + bucket_name=BUCKET_NAME, |
| 52 | + skills_base_path=SKILLS_PREFIX, |
| 53 | + skill_id=skill_id, |
| 54 | + ) |
| 55 | + ) |
| 56 | + logging.info("Loaded %d skills successfully.", len(skills)) |
| 57 | +except Exception as e: # pylint: disable=broad-exception-caught |
| 58 | + logging.error("Failed to load skills from GCS: %s", e) |
| 59 | + |
| 60 | +# Create the SkillToolset |
| 61 | +my_skill_toolset = SkillToolset(skills=skills) |
| 62 | + |
| 63 | +# Create the Agent |
| 64 | +root_agent = Agent( |
| 65 | + model="gemini-3-flash-preview", |
| 66 | + name="skill_user_agent", |
| 67 | + description="An agent that can use specialized skills loaded from GCS.", |
| 68 | + tools=[ |
| 69 | + my_skill_toolset, |
| 70 | + ], |
| 71 | + code_executor=AgentEngineSandboxCodeExecutor( |
| 72 | + sandbox_resource_name=os.getenv("SAMPLE_SKILLS_SANDBOX_RESOURCE_NAME"), |
| 73 | + agent_engine_resource_name=os.getenv( |
| 74 | + "SAMPLE_SKILLS_AGENT_ENGINE_RESOURCE_NAME" |
| 75 | + ), |
| 76 | + ), |
| 77 | +) |
| 78 | + |
| 79 | + |
| 80 | +async def main(): |
| 81 | + # Initialize the plugins |
| 82 | + logging_plugin = LoggingPlugin() |
| 83 | + |
| 84 | + # Create a Runner |
| 85 | + runner = Runner( |
| 86 | + agents=[root_agent], |
| 87 | + plugins=[logging_plugin], |
| 88 | + ) |
| 89 | + |
| 90 | + # Example run |
| 91 | + print("Agent initialized with GCS skills. Sending a test prompt...") |
| 92 | + # You can replace this with an interactive loop if needed. |
| 93 | + responses = await runner.run( |
| 94 | + user_input="Hello! What skills do you have access to?" |
| 95 | + ) |
| 96 | + |
| 97 | + if responses and responses[-1].content and responses[-1].content.parts: |
| 98 | + print(f"\nResponse: {responses[-1].content.parts[0].text}") |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + asyncio.run(main()) |
0 commit comments