Google Vertex AI
This notebook demonstrates how to use AG2 with Gemini via Vertex AI, which enables enhanced authentication method that also supports enterprise requirements using service accounts or even a personal Google cloud account.
Requirements
Install AG2 with Gemini features:
Install other Dependencies of this Notebook
Google Cloud Account
To use VertexAI a Google Cloud account is needed. If you do not have one yet, just sign up for a free trial here.
Login to your account at console.cloud.google.com
In the next step we create a Google Cloud project, which is needed for VertexAI. The official guide for creating a project is available is here.
We will name our project AG2-with-Gemini.
Enable Google Cloud APIs
If you wish to use Gemini with your personal account, then creating a Google Cloud account is enough. However, if a service account is needed, then a few extra steps are needed.
Enable API for Gemini
- For enabling Gemini for Google Cloud search for “api” and select Enabled APIs & services.
- Then click ENABLE APIS AND SERVICES.
- Search for Gemini, and select Gemini for Google Cloud.
A direct link will look like this for our ag2-with-gemini project: https://console.cloud.google.com/apis/library/cloudaicompanion.googleapis.com?project=ag2-with-gemini&supportedpurview=project - Click ENABLE for Gemini for Google Cloud.
Enable API for Vertex AI
- For enabling Vertex AI for Google Cloud search for “api” and select Enabled APIs & services.
- Then click ENABLE APIS AND SERVICES.
- Search for Vertex AI, and select Vertex AI API.
A direct link for our ag2-with-gemini will be: https://console.cloud.google.com/apis/library/aiplatform.googleapis.com?project=ag2-with-gemini - Click ENABLE Vertex AI API for Google Cloud.
Create a Service Account
You can find an overview of service accounts can be found in the cloud console
Detailed guide: https://cloud.google.com/iam/docs/service-accounts-create
A service account can be created within the scope of a project, so a project needs to be selected.
Next we assign the Vertex AI User for the service account. This can be done in the Google Cloud console in our ag2-with-gemini
project.
Alternatively, we can also grant the Vertex AI User role by running a command using the gcloud CLI, for example in Cloud Shell:
- Under IAM & Admin > Service Account select the newly created service accounts, and click the option “Manage keys” among the items.
- From the “ADD KEY” dropdown select “Create new key” and select the JSON format and click CREATE.
- The new key will be downloaded automatically.
- You can then upload the service account key file to the from where you will be running AG2.
- Please consider restricting the permissions on the key file. For example, you could run
chmod 600 ag2-with-gemini-service-account-key.json
if your keyfile is called ag2-with-gemini-service-account-key.json.
- Please consider restricting the permissions on the key file. For example, you could run
Configure Authentication
Authentication happens using standard Google Cloud authentication methods,
which means
that either an already active session can be reused, or by specifying the Google application credentials of a service account.
Additionally, AG2 also supports authentication using Credentials
objects in Python with the google-auth library, which enables even more flexibility.
For example, we can even use impersonated credentials.
Use Service Account Keyfile
The Google Cloud service account can be specified by setting the GOOGLE_APPLICATION_CREDENTIALS
environment variable to the path to the JSON key file of the service account.
We could even just directly set the environment variable, or we can add the "google_application_credentials"
key with the respective value for our model in the OAI_CONFIG_LIST.
Use the Google Default Credentials
If you are using Cloud Shell or Cloud Shell editor in Google Cloud,
then you are already authenticated. If you have the Google Cloud SDK installed locally,
then you can login by running gcloud auth application-default login
in the command line.
Detailed instructions for installing the Google Cloud SDK can be found here.
Authentication with the Google Auth Library for Python
The google-auth library supports a wide range of authentication scenarios, and you can simply pass a previously created Credentials
object to the llm_config
.
The official documentation of the Python package provides a detailed overview of the supported methods and usage examples.
If you are already authenticated, like in Cloud Shell, or after running the gcloud auth application-default login
command in a CLI, then the google.auth.default()
Python method will automatically return your currently active credentials.
Example Config List
The config could look like the following (change project_id
and google_application_credentials
):
Configure Safety Settings for VertexAI
Configuring safety settings for VertexAI is slightly different, as we have to use the speicialized safety setting object types instead of plain strings
Example with Gemini Multimodal
Authentication is the same for vision models as for the text based Gemini models.
In this example an object of type Credentials
will be supplied in order to authenticate.
Here, we will use the google application default credentials, so make sure to run the following commands if you are not yet authenticated:
The GOOGLE_APPLICATION_CREDENTIALS
environment variable is a path to our service account JSON keyfile, as described in the Use Service Account Keyfile section above.
We also need to set the Google cloud project, which is ag2-with-gemini
in this example.
Note, we could also run gcloud auth application-default login
to use our personal Google account instead of a service account.
In this case we need to run the following commands:
Use Gemini via the OpenAI Library in AG2
Using Gemini via the OpenAI library is also possible once you are already authenticated.
Run gcloud auth application-default login
to set up application default credentials locally for the example below.
Also set the Google cloud project on the CLI if you have not done so far:
The prerequisites are essentially the same as in the example above.
You can read more on the topic in the official Google docs.
A list of currently supported models can also be found in the docs
Note, that you will need to refresh your token regularly, by default every 1 hour.