arrow_back

Summarize Text using SQL and LLMs in BigQuery ML

Sign in Join
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

Summarize Text using SQL and LLMs in BigQuery ML

Lab 1 hour universal_currency_alt 1 Credit show_chart Introductory
info This lab may incorporate AI tools to support your learning.
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

GSP835

Google Cloud self-paced labs logo

Overview

In this lab, you take steps to perform summarization of source code from GitHub, a popular open-source, source code repository, and identify the primary programming language using Vertex AI's Large Language Model (LLM) for text generation and hosted remote functions in BigQuery. The source data is from the GitHub Archive Project, which contains a full snapshot of over 2.8 million open source GitHub repositories in Google BigQuery Public Datasets.

Learning objectives

In this lab, you learn how to create:

  • A BigQuery dataset to contain a model.
  • A BigQuery model that hosts the Gemini API as a remote function.
  • An external connection to establish the connection between BigQuery and Vertex AI.

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left. Navigation menu icon

Task 1. Preparing the data

You will be using the source code content from the github_repos dataset in the Google BigQuery Public Datasets. To do so, do the following:

  1. From the Navigation menu choose BigQuery. If prompted click Done.

  2. In the BigQuery console, search for github_repos and press ENTER.

Note: If you do not see the github_repos dataset after performing a search you may need to select SEARCH ALL PROJECTS to add the dataset.
  1. Click on the star next to the dataset that is listed as the search result.

GitHub starred repos

  1. Expand the github_repos dataset and select the sample_contents table. This table contains sample data containing 10% of the full data in the contents table. Click PREVIEW.

Task 2. Create the BigQuery dataset

A BigQuery dataset is a collection of tables. All tables in a dataset are stored in the same data location.

  1. Clear the search filter in the BigQuery console and select Create dataset using the three dots next to your lab's project.

Create dataset

  1. In the Create dataset form enter bq_llm as the Dataset ID then click CREATE DATASET.

BQ LLM

The dataset is going to be used to house the model that you create in the next tasks of this lab.

Typically, data that is used by an ML application is stored in a table in the dataset as well. Since the data is in a BigQuery public dataset, you reference that data directly from the public data using an external connection. You create the external connection in the next task of this lab.

Click Check my progress to verify the objective. Create a dataset to house the ML model.

Task 3. Create the external connection

Now, create an external connection and save the Service Account ID from the connection configuration details.

  1. Click the + ADD button on the BigQuery Explorer pane, then click Connections to external data sources in the popular sources listed.

  2. Select Connection type as Vertex AI remote models, remote functions and BigLake (Cloud Resource) and set Connection ID to llm-connection.

  3. Click CREATE CONNECTION.

  4. Select us.llm-connection under the External connections section of the project's datasets. Copy the Service Account ID generated from the external connection configuration details to your clipboard. You use it in the next step.

External Connection

Grant permissions to the Service Account

You need to grant the Service Account generated by the external connection access to the Vertex AI service. To do so:

  1. From the Navigation menu select IAM & Admin.

  2. Click + GRANT ACCESS on the IAM page.

  3. Paste the Service Account ID generated by the external connection in the New principals form input.

  4. Set the Role to Vertex AI User then click SAVE.

Grant Role

Click Check my progress to verify the objective. Create the external connection.

Task 4. Create a remote ML model

In this task, you create a remote model that represents a hosted Vertex AI large language model (LLM).

  1. From the Navigation menu select BigQuery. Click on + Compose new query.

  2. Run the following SQL query in a new tab in BigQuery Explorer:

CREATE OR REPLACE MODEL bq_llm.llm_model REMOTE WITH CONNECTION `us.llm-connection` OPTIONS (remote_service_type = 'CLOUD_AI_LARGE_LANGUAGE_MODEL_V1');

This creates a model named llm_model in the dataset bq_llm created earlier in the lab. The model leverages the CLOUD_AI_LARGE_LANGUAGE_MODEL_V1 of Vertex AI as a remote function. Once completed you see the model appear in the BigQuery console.

LLM Model

Click Check my progress to verify the objective. Create a remote ML model.

Task 5. Generate text using the ML model

Use the ML model created to generate, summarize, or categorize text.

  1. Run the following query in a new tab of the BigQuery console:
SELECT ml_generate_text_result['predictions'][0]['content'] AS generated_text, ml_generate_text_result['predictions'][0]['safetyAttributes'] AS safety_attributes, * EXCEPT (ml_generate_text_result) FROM ML.GENERATE_TEXT( MODEL `bq_llm.llm_model`, ( SELECT CONCAT('Can you read the code in the following text and generate a summary for what the code is doing and what language it is written in:', content) AS prompt from `bigquery-public-data.github_repos.sample_contents` limit 5 ), STRUCT( 0.2 AS temperature, 100 AS max_output_tokens));
  1. Review the following to understand the SQL query in more depth:

ml_generate_text_result is the response from the text generation model in JSON format that contains both content and safety attributes:

  • Content represents the generated text result.
  • Safety attributes represent built-in content filters with an adjustable threshold that is enabled in the Gemini API to avoid any unintended or unforeseen responses from the LLM. Responses are blocked if they violate the safety thresholds defined.

ML.GENERATE_TEXT is the construct used in BigQuery to access the Vertex AI LLM to perform text generation tasks.

CONCAT appends the supplied PROMPT statement to a database record.

github_repos is the public dataset name and sample_contents is the name of the table that holds the data you use in the prompt design.

Temperature is the prompt parameter to control the randomness of the response - the lesser, the better the relevance.

Max_output_tokens is the number of words you want in response.

The query response should look similar to the following:

Query Response Prompt

Click Check my progress to verify the objective. Generate text using the ML model.

Congratulations!

Congratulations! You have successfully used a Vertex AI Text Generation LLM programmatically to perform text analytics on your data using only SQL-queries. Check out Vertex AI LLM product documentation to learn more about available models.

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated: August 23, 2024

Lab Last Tested: August 23, 2024

Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available