You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
"Thisnotebook shows you how to create structuredLLMoutput by specifying anoutput schemawhen generating predictions with a Gemini model."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setup"
"## Costs\n",
"\n",
"This tutorial uses billable components of Google Cloud:\n",
"\n",
"* BigQuery (compute)\n",
"* BigQuery ML\n",
"* Generative AI support on Vertex AI\n",
"\n",
"Learn about [BigQuery compute pricing](https://cloud.google.com/bigquery/pricing#analysis_pricing_models), [Generative AI support on Vertex AI pricing](https://cloud.google.com/vertex-ai/generative-ai/pricing),\n",
"and [BigQuery ML pricing](https://cloud.google.com/bigquery/pricing#section-11),\n",
"and use the [Pricing Calculator](https://cloud.google.com/products/calculator/)\n",
"to generate a cost estimate based on your projected usage."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Before you begin\n",
"\n",
"Complete the tasks in this section to set up your environment."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Set up your Google Cloud project\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 credit towards your compute/storage costs.\n",
"\n",
"2. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
"\n",
"3. [Click here](https://console.cloud.google.com/flows/enableapi?apiid=bigquery.googleapis.com,bigqueryconnection.googleapis.com,aiplatform.googleapis.com) to enable the following APIs:\n",
"\n",
" * BigQuery API\n",
" * BigQuery Connection API\n",
" * Vertex AI API\n",
"\n",
"4. If you are running this notebook locally, install the [Cloud SDK](https://cloud.google.com/sdk)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Authenticate your Google Cloud account\n",
"\n",
"Depending on your Jupyter environment, you might have to manually authenticate. Follow the relevant instructions below.\n",
"\n",
"**BigQuery Studio** or **Vertex AI Workbench**\n",
"\n",
"Do nothing, you are already authenticated.\n",
"\n",
"**Local JupyterLab instance**\n",
"\n",
"Uncomment and run the following cell:"
]
},
{
"cell_type": "code",
"execution_count":2,
"execution_count":null,
"metadata": {},
"outputs": [],
"source": [
"PROJECT = \"bigframes-dev\" # replace with your project\n",
"# ! gcloud auth login"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Colab**\n",
"\n",
"Uncomment and run the following cell:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# from google.colab import auth\n",
"# auth.authenticate_user()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set up your project"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set your project and import necessary modules. If you don't know your project ID, see [Locate the project ID](https://support.google.com/googleapi/answer/7014113)."
"### 1.Create a BigFrames DataFrame and a Gemini model\n",
"Starting from creatinga simple dataframe of several cities and a Gemini model in BigFrames"
"##Create a DataFrame and a Gemini model\n",
"Createa simple[DataFrame](https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes.dataframe.DataFrame) of several cities:"
]
},
{
Expand DownExpand Up
@@ -162,6 +260,13 @@
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Connect to a Gemini model using the [`GeminiTextGenerator` class](https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes.ml.llm.GeminiTextGenerator):"
]
},
{
"cell_type": "code",
"execution_count": 4,
Expand All
@@ -186,8 +291,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Generate structured output data\n",
"Before, llm models canonly generate text output.Saying if youwant to knowwhetherthecity is a US city, for example:"
"## Generate structured output data\n",
"Previously, LLMs couldonly generate text output.For example, youcould generate output that identifieswhethera givencity is a US city:"
]
},
{
Expand DownExpand Up
@@ -273,9 +378,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Theoutputs are textresultsthat human can read.But if want the outputdatato be more useful for analysis, it is better totransfer tostructured data like boolean, int or float values. Usuallytheprocess wasn't easy.\n",
"Theoutput is text thatahuman can read.However, ifyouwant the output to be more useful for analysis, it is better toformat the output asstructured data. This is especially true when you want to have Boolean, integer, or float values to work with instead of string values. Previously, formattingtheoutput in this way wasn't easy.\n",
"\n",
"Now you can get structured output out-of-the-box by specifying the output_schema parameterinGemini modelpredict method. Inbelowexample, theoutputs are only booleanvalues."
"Now, you can get structured output out-of-the-box by specifying the`output_schema` parameterwhen calling theGemini model's [`predict` method](https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes.ml.llm.GeminiTextGenerator#bigframes_ml_llm_GeminiTextGenerator_predict). Inthe followingexample, themodel output is formatted as Booleanvalues:"
]
},
{
Expand DownExpand Up
@@ -361,7 +466,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can alsogetfloat orint values, forexample, toget populations in millions:"
"You can alsoformat model output asfloat orinteger values. In the followingexample,the model output is formatted as float valuestoshow the city's population in millions:"
]
},
{
Expand DownExpand Up
@@ -447,7 +552,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"And yearly rainy days:"
"In the following example, the model output is formatted as integer values to show the count of the city's rainy days:"
]
},
{
Expand DownExpand Up
@@ -533,10 +638,10 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"###3. Generate all types ofdata in one prediction\n",
"You can get the different outputcolumnsand types in one prediction. \n",
"###Format output as multipledata types in one prediction\n",
"Within a single prediction, you can generate multiplecolumnsof output that use different data types. \n",
"\n",
"Note it doesn'trequirededicated prompts, as long as the output column names are informative to the model."
"The input doesn'thave to bededicated prompts as long as the output column names are informative to the model."
]
},
{
Expand DownExpand Up
@@ -630,14 +735,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"###4. Generatecomposite datatypes"
"###Format output as acomposite datatype"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Composite datatypes like array and struct can also be generated. Here the example generates a places_to_visit column as array of strings and a gps_coordinatesas struct of floats. Along with previous fields, all in one prediction."
"You can generate composite data types like arrays and structs. The following example generates a`places_to_visit` column asanarray of strings and a`gps_coordinates` columnasastruct of floats:"
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, run the following cell to delete the temporary cloud artifacts created during the BigFrames session:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bpd.close_session()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Next steps\n",
"\n",
"Learn more about BigQuery DataFrames in the [documentation](https://cloud.google.com/python/docs/reference/bigframes/latest) and find more sample notebooks in the [GitHub repo](https://github.com/googleapis/python-bigquery-dataframes/tree/main/notebooks)."
]
}
],
"metadata": {
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.