This agent functions as an automated image generation and evaluation system specifically designed to create and validate images based on text descriptions while adhering to predefined policies. Its primary role is to ensure that generated images meet specific compliance and quality standards by iteratively generating and evaluating images until they satisfy policy requirements.
This agent generates and evaluates images based on text descriptions while ensuring compliance with predefined policies. Its primary purpose is to serve as an automated image generation and validation system that maintains high standards of quality and policy compliance.
- Generates images from text descriptions using Imagen
- Evaluates generated images against a set of predefined policies
- Iteratively improves images that don't meet policy requirements
- Provides detailed scoring and feedback for each generated image
This sample agent enables users to generate images from text descriptions while ensuring the output meets specific policy requirements through an automated evaluation and iteration process.
This diagram shows the detailed architecture of the agents and tools used to implement this workflow.
| Feature | Description |
|---|---|
| Interaction Type | Workflow |
| Complexity | Medium |
| Agent Type | Multi Agent |
| Components | Tools: Imagen, Image Evaluation Tools |
| Vertical | Horizontal |
-
Python 3.10+
-
uv for dependency management and packaging
- See the official uv website for installation.
curl -LsSf https://astral.sh/uv/install.sh | sh
Use the Google Agents CLI to scaffold a production-ready project and choose your deployment target (Agent Runtime or Cloud Run), with CI/CD and other production features.
Install the CLI (one-time):
uvx google-agents-cli setupCreate the project from this sample (replace my-image-scorer with your project name):
agents-cli create my-image-scorer -a adk@image-scoringThe Google Agents CLI will prompt you to select deployment options and set up your Google Cloud project.
From your newly created project directory (e.g. my-image-scorer), run:
cd my-image-scorer
uv sync --dev
uv run adk run image_scoringFor the web UI:
uv run adk webThen select image_scoring from the dropdown menu.
Alternative: Local development (run from this sample repo)
-
Clone the repository:
git clone https://github.com/google/adk-samples.git cd adk-samples/python/agents/image-scoringFor the rest of this tutorial ensure you remain in the
python/agents/image-scoringdirectory. -
Install the dependencies:
uv sync --dev
-
Configure settings:
There is a
.env-examplefile included in the repository. Update this file with the values appropriate to your project, and save it as.env. The values in this file will be read into the environment of your application.Authenticate your GCloud account:
gcloud auth application-default login gcloud auth application-default set-quota-project $GOOGLE_CLOUD_PROJECT
You can run the agent locally using the adk command in your terminal. Here are some example requests:
a peaceful mountain landscape at sunseta cat riding a bicycle
-
To run the agent from the CLI:
adk run image_scoring
-
To run the agent from the ADK web UI:
adk web
Then select
image_scoringfrom the dropdown.
uv sync --dev
uv run pytest evaleval is a demonstration of how to evaluate the agent, using the AgentEvaluator in ADK. It sends a sample request to the image_scoring agent and checks if the tool usage is as expected.
The Image Scoring Agent can be customized to better suit your requirements. For example:
- Policy Customization: Modify the policy evaluation criteria to match your specific requirements and standards.
- Image Generation Parameters: Adjust the Imagen parameters to control image generation quality and characteristics.
- Evaluation Metrics: Add or modify evaluation metrics to assess different aspects of the generated images.
- Iteration Strategy: Customize the iteration process to optimize for specific aspects of image quality or policy compliance.
The Image Scoring Agent implements a sequential workflow using the following sub-agents:
-
Prompt Generation Agent (
prompt_agent.py)- Primary responsibility: Creates optimized prompts for Imagen based on input text descriptions
- Uses Gemini model to generate prompts that comply with policies
- Outputs prompts that are stored in session state for image generation
-
Image Generation Agent (
imagen_agent.py)- Primary responsibility: Generates images using Imagen 3.0 based on the prompts
- Configures image generation parameters (aspect ratio, safety filters, etc.)
- Saves generated images to Google Cloud Storage (GCS)
- Stores image artifacts and GCS URIs in session state
-
Scoring Agent (
scoring_agent.py)- Primary responsibility: Evaluates generated images against policy rules
- Loads policy rules from
policy.json - Analyzes images and assigns scores (0-5) for each policy criterion
- Computes total score and stores it in session state
- Provides detailed scoring feedback for each policy rule
-
Checker Agent (
checker_agent.py)- Primary responsibility: Evaluates if the generated image meets quality thresholds
- Manages iteration count and maximum iteration limits
- Compares total score against configured threshold (default: 10)
- Controls workflow termination based on score or iteration limits
- The workflow starts with the Prompt Generation Agent creating an optimized prompt
- The Image Generation Agent uses this prompt to create an image with Imagen 3.0
- The Scoring Agent evaluates the generated image against policy rules
- The Checker Agent determines if the score meets the threshold
- If the score is below threshold and max iterations not reached, the process repeats
- The workflow terminates when either:
- The image score meets or exceeds the threshold
- The maximum number of iterations is reached
