Agent with Vertex AI

Code

1"""
2To use Vertex AI, with the Gemini Model class, you need to set the following environment variables:
3
4export GOOGLE_GENAI_USE_VERTEXAI="true"
5export GOOGLE_CLOUD_PROJECT="your-project-id"
6export GOOGLE_CLOUD_LOCATION="your-location"
7
8Or you can set the following parameters in the `Gemini` class:
9
10gemini = Gemini(
11 vertexai=True,
12 project_id="your-google-cloud-project-id",
13 location="your-google-cloud-location",
14)
15"""
16
17from kern.agent import Agent, RunOutput # noqa
18from kern.models.google import Gemini
19
20agent = Agent(model=Gemini(id="gemini-2.0-flash-001"), markdown=True)
21
22# Get the response in a variable
23# run: RunOutput = agent.run("Share a 2 sentence horror story")
24# print(run.content)
25
26# Print the response in the terminal
27agent.print_response("Share a 2 sentence horror story")

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set 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-ai

Run Agent

1python cookbook/11_models/google/gemini/vertexai.py