Code
imagen_tool_advanced.py
Usage
1
Set up your virtual environment
2
Set up Vertex AI authentication
Follow the authentication guide to set up Vertex AI credentials.
3
Set environment variables
4
Install dependencies
5
Update the Imagen model
Replace
imagen-4.0-generate-preview-05-20 with imagen-4.0-generate-001 in the saved file.6
Use the v2 run output accessor
Replace
response = agent.run_response with response = agent.get_last_run_output() in the saved file.7
Encode the image bytes
Add
import base64, then replace save_base64_data(str(response.images[0].content), "tmp/baleen_whale.png") with save_base64_data(base64.b64encode(response.images[0].content).decode("ascii"), "tmp/baleen_whale.png") in the saved file.8
Run Agent
Save the code above as
imagen_tool_advanced.py, then run: