Lab 4.5 Challenge: Professional Model Configuration
Goal
In this lab, you will upgrade your "Support Analyzer" agent to a production-ready state. You will learn how to implement advanced retry logic using a custom Gemini subclass and how to provide a multi-model fallback using LiteLlm.
Prerequisites
- Install LiteLLM:
pip install litellm
Lab Tasks
-
Create a Resilient Subclass:
- In your
support_analyzer/agent.py, create a class namedResilientGeminithat inherits fromGemini. - Override the
api_clientproperty. - Configure it with a
HttpRetryOptionspolicy:max_delay=10,exp_base=2.0, andjitter=0.5.
- In your
-
Implement Multi-Model Fallback:
- Update your agent's
modellogic. - If the environment variable
USE_LOCAL_MODELis set to"1", useLiteLlmwithollama_chat/mistral. - Otherwise, use your new
ResilientGeminiclass.
- Update your agent's
-
Verify the Configuration:
- Run the agent using
adk run support_analyzer. - Verify it works as expected. (Note: You won't "see" the retries unless a network error occurs, but your code is now protected!).
- Run the agent using
Python Approach (Primary)
Modify agent.py to use the advanced configuration patterns.
import os
from functools import cached_property
from google.adk.agents import LlmAgent
from google.adk.models import Gemini
from google.adk.models.lite_llm import LiteLlm
from google.genai import Client, types
# TODO: Step 1 - Define the ResilientGemini subclass
class ResilientGemini(Gemini):
@cached_property
def api_client(self) -> Client:
# TODO: Implement Client with HttpRetryOptions
pass
# TODO: Step 2 - Implement the model selection logic
if os.getenv("USE_LOCAL_MODEL") == "1":
# Use LiteLLM
model_to_use = ...
else:
# Use your ResilientGemini
model_to_use = ...
root_agent = LlmAgent(
name="support_analyzer_agent",
model=model_to_use,
instruction="Analyze customer support issues."
)
Self-Reflection Questions
- Why is "Jitter" important in a retry policy for a high-traffic production application?
- What are the advantages of centralizing model configuration in a subclass instead of passing parameters to every agent instance?
- In which scenario would you prefer using the native
Geminiclass over theLiteLlmabstraction?
🕵️ Hidden Solution 🕵️
Looking for the solution? Here's a hint (Base64 decode me):
L2RvYy1hZGstdHJhaW5pbmcvbW9kdWxlMDQuNS1tdWx0aS1tb2RlbC1saXRlbGxtL2xhYi1zb2x1dGlvbg==
The direct link is: Lab Solution