Using the VertexAI SDK

In this page we will go over what VertexAI is and how to get started with using it in python to use generative AI in your projects.

What is VertexAI

VertexAI is a platform offered by Google Cloud Protocols that allows users to seamlessly integrate generative AI into their applications. The platform allows users to access PALM2, which is Google’s state of the art large language model (LLM), train their own models, and deploy their own models for production use.

In terms of generative AI, Google offers 3 types, Language, Vision, and Speech. The Language generative AI is a typical large language model where the user passes in prompts and the AI generates a response. The Vision generative AI has 4 use cases, but at the time of writing there are only 2 are available to the general public. These are captioning images, asking questions about images that you upload. Lastly is the speech AI, this generative AI allows you to convert text to speech or speech to text. The speech generated is very human like and you have the option to choose between a male english speaker, a female english speaker, or a male spanish speaker (at the time of writing).

You can read more at their own website. VertexAI

Getting started with VertexAI

In this article we will provide a quick start guide to using Google’s PALM2 model in python.

Creating a Google Cloud Account

To use VertexAI we must first create a Google Cloud Account. When you first sign up everyone gets a certain amount of free credits for you to use. Visit The following site and sign up for free using an existing or a new Google account. Create your Account

Accessing VertexAI

To navigate to the VertexAI page, visit the Products and Solutions page found in the Navigation menu on the top left corner of your console. In the Products and Solutions page, scroll to the section on Artificial Intelligence and click on VertexAI. Alternitavely, you can search for VertexAI in the search bar or you can visit the following link VertexAI.

Using VertexAI Online

For this section we will focus on using generative AI. VertexAI offers 3 types of generative AI, text, vision, and speech. To try out the generative AI online visit the generative AI studio on the left hand side. Here you can choose which generative AI you want to use, but we will focus on using text generation so click on Language. Here you can choose to checkout their premade prompts, or you can create your own prompts by clicking text prompts at the top of the page. Here you can test out different prompts, different models, and different parameters. Their are 2 options for creating prompts, freeform or structured, you can make your selection on the top right corner. Any prompts that you make can also be saved in the top right corner for you to look back on.

Using the VertexAI SDK in Python

Setup

To use the VertexAI SDK have Python installed with version above 3.7. You can install install Python here.

Next, activate all required API’s on your dashboard of the VertexAI Page, this will require you to link billing to your account.

Now install the VertexAI library using pip. you may choose to do this step in a Virtual Environment.

pip install google-cloud-aiplatform

To use the VertexSDK we must first authenticate ourselves using credentials. We will do this by creating a service account. You can find the instructions here. In short, create a service account in your google cloud console and assign it permissions to access API’s, then create a key for it and select to save it as a JSON and download the created credentials.json file into your working directory.

Usage

First, we will import the required packages

import vertexai
from vertexai.language_models import TextGenerationModel
from google.oauth2 import service_account

Next, we will initialize the SDK and authenticate ourselves. You can find the project name and location by going to the VertexAI page, visiting the generative studio, and using one of the models. In the top right corner there should be a button, view code. In the code you can find the project name and location. Now initialize your self with the following code snippet.

vertexai.init(project="project", location="location", credentials=service_account.Credentials.from_service_account_file('credentials.json'))

Now you can use PALM2 with the following code. Change the parameters and prompt to be your own!

parameters = {
    "candidate_count": 1,
    "max_output_tokens": 1024,
    "temperature": 0.2,
    "top_p": 0.8,
    "top_k": 40
}
model = TextGenerationModel.from_pretrained("text-bison")
response = model.predict(
    """Write your prompt here!

    """,
    **parameters
)
print(f"Response from Model: {response.text}")

Application for 301 Students

VertexAI is highly applicable to us as CSC301 Students because as we leave the course and continue our studies we may be looking to build personal project and we may want to incoporate generative AI into the projects. Leveraging the technology offered by VertexAI we can easily build projects such as chat bots, and maybe come up with our own start-ups.

Conclusion

This article was guide for getting started with using the VertexAI SDK for the purpose of integrating generative AI into your applications. The provided instructions and code snippets showed how to use the generative AI studio by Google and how to use the VertexAI SDK in python to use PALM2 in python. Hopefully, you were able to learn how to get started with integrating PALM2 into your applications and were able to try out the code snippets yourself! Below are further readings that can help you use the other features that VertexAI offers and more in-depth guides.

Articles and Resources

More information on VertexAI
VertexAI SDK Documentation
Creating Access Credentials