|
| 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 | +"""A ManagedAgent with an ``InstructionProvider`` system instruction. |
| 16 | +
|
| 17 | +``ManagedAgent.instruction`` is forwarded to the Managed Agents API as the |
| 18 | +interaction's system instruction, the same way ``LlmAgent.instruction`` shapes a |
| 19 | +local model. It accepts either a plain string (which may embed ``{state_var}`` |
| 20 | +placeholders resolved from session state) or an ``InstructionProvider`` — a |
| 21 | +callable invoked with a ``ReadonlyContext`` that returns the instruction string |
| 22 | +and bypasses placeholder injection. |
| 23 | +
|
| 24 | +This sample uses an ``InstructionProvider`` so the instruction is built |
| 25 | +dynamically, per turn, from session state: it pins a terse persona and reads the |
| 26 | +reply language from ``state['response_language']`` (defaulting to English). The |
| 27 | +persona keeps the effect visible in every reply, and because the provider runs |
| 28 | +on every turn the instruction adapts if the state changes on a later turn. |
| 29 | +
|
| 30 | +Run with ``adk web`` / ``adk run |
| 31 | +contributing/samples/managed_agent/system_instruction``. See the README for the |
| 32 | +required environment / auth setup. |
| 33 | +""" |
| 34 | + |
| 35 | +import os |
| 36 | + |
| 37 | +from google.adk.agents import ManagedAgent |
| 38 | +from google.adk.agents.readonly_context import ReadonlyContext |
| 39 | + |
| 40 | +# The Managed Agent id served by the Managed Agents API. Override with the |
| 41 | +# MANAGED_AGENT_ID environment variable if your project has access to a |
| 42 | +# different agent. |
| 43 | +_DEFAULT_AGENT_ID = 'antigravity-preview-05-2026' |
| 44 | + |
| 45 | + |
| 46 | +def persona_instruction(readonly_context: ReadonlyContext) -> str: |
| 47 | + """Builds the system instruction dynamically from session state. |
| 48 | +
|
| 49 | + An ``InstructionProvider`` is any callable that takes a ``ReadonlyContext`` |
| 50 | + and returns the instruction (a ``str``, or an awaitable ``str`` for async |
| 51 | + providers). It is invoked on every turn, so the instruction can adapt to the |
| 52 | + current session state, and — unlike a plain string — it bypasses |
| 53 | + ``{placeholder}`` injection, leaving you to build the final string yourself. |
| 54 | + """ |
| 55 | + language = readonly_context.state.get('response_language', 'English') |
| 56 | + return ( |
| 57 | + 'You are a terse assistant. Always answer in a single sentence, in' |
| 58 | + f' {language}, and end every reply with a relevant emoji.' |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +root_agent = ManagedAgent( |
| 63 | + name='managed_persona_agent', |
| 64 | + agent_id=os.environ.get('MANAGED_AGENT_ID', _DEFAULT_AGENT_ID), |
| 65 | + # Provision a remote sandbox; the environment id is recovered from prior |
| 66 | + # events so follow-up turns reuse the same sandbox. |
| 67 | + environment={'type': 'remote'}, |
| 68 | + # Pass an InstructionProvider callable instead of a plain string: it is |
| 69 | + # invoked per turn with a ReadonlyContext and returns the resolved |
| 70 | + # instruction that is forwarded as the interaction's system_instruction. |
| 71 | + instruction=persona_instruction, |
| 72 | +) |
0 commit comments