mirror of
https://github.com/google/adk-docs.git
synced 2026-09-14 16:16:59 +08:00
2ccd53a3d9
* fix: update gemini 2.0 flash to latest model id for Live API * fix: use -flash for most pages instead of the live api model * fix: wording andformatting * fix: resolve merge conflict
89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
from google.adk.agents import LlmAgent
|
|
from google.adk.agents.callback_context import CallbackContext
|
|
from google.adk.models import LlmResponse, LlmRequest
|
|
from google.adk.runners import Runner
|
|
from typing import Optional
|
|
from google.genai import types
|
|
from google.adk.sessions import InMemorySessionService
|
|
|
|
GEMINI_2_FLASH="gemini-2.0-flash"
|
|
|
|
# --- Define the Callback Function ---
|
|
def simple_before_model_modifier(
|
|
callback_context: CallbackContext, llm_request: LlmRequest
|
|
) -> Optional[LlmResponse]:
|
|
"""Inspects/modifies the LLM request or skips the call."""
|
|
agent_name = callback_context.agent_name
|
|
print(f"[Callback] Before model call for agent: {agent_name}")
|
|
|
|
# Inspect the last user message in the request contents
|
|
last_user_message = ""
|
|
if llm_request.contents and llm_request.contents[-1].role == 'user':
|
|
if llm_request.contents[-1].parts:
|
|
last_user_message = llm_request.contents[-1].parts[0].text
|
|
print(f"[Callback] Inspecting last user message: '{last_user_message}'")
|
|
|
|
# --- Modification Example ---
|
|
# Add a prefix to the system instruction
|
|
original_instruction = llm_request.config.system_instruction or types.Content(role="system", parts=[])
|
|
prefix = "[Modified by Callback] "
|
|
# Ensure system_instruction is Content and parts list exists
|
|
if not isinstance(original_instruction, types.Content):
|
|
# Handle case where it might be a string (though config expects Content)
|
|
original_instruction = types.Content(role="system", parts=[types.Part(text=str(original_instruction))])
|
|
if not original_instruction.parts:
|
|
original_instruction.parts.append(types.Part(text="")) # Add an empty part if none exist
|
|
|
|
# Modify the text of the first part
|
|
modified_text = prefix + (original_instruction.parts[0].text or "")
|
|
original_instruction.parts[0].text = modified_text
|
|
llm_request.config.system_instruction = original_instruction
|
|
print(f"[Callback] Modified system instruction to: '{modified_text}'")
|
|
|
|
# --- Skip Example ---
|
|
# Check if the last user message contains "BLOCK"
|
|
if "BLOCK" in last_user_message.upper():
|
|
print("[Callback] 'BLOCK' keyword found. Skipping LLM call.")
|
|
# Return an LlmResponse to skip the actual LLM call
|
|
return LlmResponse(
|
|
content=types.Content(
|
|
role="model",
|
|
parts=[types.Part(text="LLM call was blocked by before_model_callback.")],
|
|
)
|
|
)
|
|
else:
|
|
print("[Callback] Proceeding with LLM call.")
|
|
# Return None to allow the (modified) request to go to the LLM
|
|
return None
|
|
|
|
|
|
# Create LlmAgent and Assign Callback
|
|
my_llm_agent = LlmAgent(
|
|
name="ModelCallbackAgent",
|
|
model=GEMINI_2_FLASH,
|
|
instruction="You are a helpful assistant.", # Base instruction
|
|
description="An LLM agent demonstrating before_model_callback",
|
|
before_model_callback=simple_before_model_modifier # Assign the function here
|
|
)
|
|
|
|
APP_NAME = "guardrail_app"
|
|
USER_ID = "user_1"
|
|
SESSION_ID = "session_001"
|
|
|
|
# Session and Runner
|
|
session_service = InMemorySessionService()
|
|
session = session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID)
|
|
runner = Runner(agent=my_llm_agent, app_name=APP_NAME, session_service=session_service)
|
|
|
|
|
|
# Agent Interaction
|
|
def call_agent(query):
|
|
content = types.Content(role='user', parts=[types.Part(text=query)])
|
|
events = runner.run(user_id=USER_ID, session_id=SESSION_ID, new_message=content)
|
|
|
|
for event in events:
|
|
if event.is_final_response():
|
|
final_response = event.content.parts[0].text
|
|
print("Agent Response: ", final_response)
|
|
|
|
call_agent("callback example") |