Agent with Vertex AI
Code
1"""2To use Vertex AI, with the Gemini Model class, you need to set the following environment variables:34export GOOGLE_GENAI_USE_VERTEXAI="true"5export GOOGLE_CLOUD_PROJECT="your-project-id"6export GOOGLE_CLOUD_LOCATION="your-location"78Or you can set the following parameters in the `Gemini` class:910gemini = Gemini(11 vertexai=True,12 project_id="your-google-cloud-project-id",13 location="your-google-cloud-location",14)15"""1617from kern.agent import Agent, RunOutput # noqa18from kern.models.google import Gemini1920agent = Agent(model=Gemini(id="gemini-2.0-flash-001"), markdown=True)2122# Get the response in a variable23# run: RunOutput = agent.run("Share a 2 sentence horror story")24# print(run.content)2526# Print the response in the terminal27agent.print_response("Share a 2 sentence horror story")Usage
Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateSet up Vertex AI
Set your environment variables:
1export GOOGLE_GENAI_USE_VERTEXAI="true"2export GOOGLE_CLOUD_PROJECT="your-project-id"3export GOOGLE_CLOUD_LOCATION="your-location"Or configure in code:
1gemini = Gemini(2 vertexai=True,3 project_id="your-google-cloud-project-id",4 location="your-google-cloud-location",5)Install dependencies
1uv pip install -U google-genai kern-aiRun Agent
1python cookbook/11_models/google/gemini/vertexai.py