The AI.GENERATE_TABLE function
This document describes the AI.GENERATE_TABLE function, which lets you perform
generative natural language tasks by using any combination of text and
unstructured data from BigQuery
standard tables, and also
specify a schema to format the response from the model.
The function works by sending requests to a BigQuery ML remote model that represents a Vertex AI Gemini model, and then returning that model's response. The function supports remote models over any of the generally available or preview Gemini models.
Several of the AI.GENERATE_TABLE function's arguments provide the
parameters that shape the Vertex AI model's response.
You can use the AI.GENERATE_TABLE function to perform tasks such as
classification, sentiment analysis, image captioning, and transcription.
Prompt design can strongly affect the responses returned by the Vertex AI model. For more information, see Introduction to prompting.
Input
Using the AI.GENERATE_TABLE function, you can use the following types
of input:
- Text data from standard tables.
ObjectRefRuntimevalues that are generated by theOBJ.GET_ACCESS_URLfunction. You can useObjectRefvalues from standard tables as input to theOBJ.GET_ACCESS_URLfunction. (Preview)
When you analyze unstructured data, that data must meet the following requirements:
- Content must be in one of the supported formats that are
described in the Gemini API model
mimeTypeparameter. - If you are analyzing a video, the maximum supported length is two minutes.
If the video is longer than two minutes,
AI.GENERATE_TABLEonly returns results for the first two minutes.
Syntax
AI.GENERATE_TABLE(
MODEL `PROJECT_ID.DATASET.MODEL`,
{ TABLE `PROJECT_ID.DATASET.TABLE` | (QUERY_STATEMENT) },
STRUCT(
OUTPUT_SCHEMA AS output_schema
[, MAX_OUTPUT_TOKENS AS max_output_tokens]
[, TOP_P AS top_p]
[, TEMPERATURE AS temperature]
[, STOP_SEQUENCES AS stop_sequences]
[, SAFETY_SETTINGS AS safety_settings]
[, REQUEST_TYPE AS request_type])
)
Arguments
AI.GENERATE_TABLE takes the following arguments:
PROJECT_ID: the project that contains the resource.DATASET: the dataset that contains the resource.MODEL: the name of the remote model. For more information, see TheCREATE MODELstatement for remote models over LLMs.TABLE: the name of the BigQuery table that contains the prompt data. The text in the column that's namedpromptis sent to the model. If your table does not have apromptcolumn, use theQUERY_STATEMENTargument instead and provide aSELECTstatement that includes an alias for an existing table column. An error occurs if nopromptcolumn is available.QUERY_STATEMENT: the GoogleSQL query that generates the prompt data. The query must produce a column namedprompt. Within the query, you can provide the prompt value in the following ways:- Specify a
STRINGvalue. For example,('Write a poem about birds'). Specify a
STRUCTvalue that contains one or more fields. You can use the following types of fields within the struct value:Field type Description Examples STRINGA string literal, or the name of a STRINGcolumn.String literal: 'Is Seattle a US city?'
String column name:my_string_columnARRAY<STRING>You can only use string literals in the array. Array of string literals: ['Is ', 'Seattle', ' a US city']ObjectRefRuntimeAn
ObjectRefRuntimevalue returned by theOBJ.GET_ACCESS_URLfunction. TheOBJ.GET_ACCESS_URLfunction takes anObjectRefvalue as input, which you can provide by either specifying the name of a column that containsObjectRefvalues, or by constructing anObjectRefvalue.ObjectRefRuntimevalues must have theaccess_url.read_urlanddetails.gcs_metadata.content_typeelements of the JSON value populated.Function call with ObjectRefcolumn:OBJ.GET_ACCESS_URL(my_objectref_column, 'r')
Function call with constructedObjectRefvalue:OBJ.GET_ACCESS_URL(OBJ.MAKE_REF('gs://image.jpg', 'myconnection'), 'r')ARRAY<ObjectRefRuntime>ObjectRefRuntimevalues returned from multiple calls to theOBJ.GET_ACCESS_URLfunction. TheOBJ.GET_ACCESS_URLfunction takes anObjectRefvalue as input, which you can provide by either specifying the name of a column that containsObjectRefvalues, or by constructing anObjectRefvalue.ObjectRefRuntimevalues must have theaccess_url.read_urlanddetails.gcs_metadata.content_typeelements of the JSON value populated.Function calls with ObjectRefcolumns:[OBJ.GET_ACCESS_URL(my_objectref_column1, 'r'), OBJ.GET_ACCESS_URL(my_objectref_column2, 'r')]
Function calls with constructedObjectRefvalues:[OBJ.GET_ACCESS_URL(OBJ.MAKE_REF('gs://image1.jpg', 'myconnection'), 'r'), OBJ.GET_ACCESS_URL(OBJ.MAKE_REF('gs://image2.jpg', 'myconnection'), 'r')]The function combines
STRUCTfields similarly to aCONCAToperation and concatenates the fields in their specified order. The same is true for the elements of any arrays used within the struct. The following table shows some examples ofSTRUCTprompt values and how they are interpreted:Struct field types Struct value Semantic equivalent STRUCT<STRING>('Describe the city of Seattle')'Describe the city of Seattle' STRUCT<STRING, STRING, STRING>('Describe the city ', my_city_column, ' in 15 words')'Describe the city my_city_column_value in 15 words' STRUCT<STRING, ARRAY<STRING>>('Describe ', ['the city of', 'Seattle'])'Describe the city of Seattle' STRUCT<STRING, ObjectRefRuntime>('Describe this city', OBJ.GET_ACCESS_URL(image_objectref_column, 'r'))'Describe this city' image STRUCT<STRING, ObjectRefRuntime, ObjectRefRuntime>('If the city in the first image is within the country of the second image, provide a ten word description of the city',
OBJ.GET_ACCESS_URL(city_image_objectref_column, 'r'),
OBJ.GET_ACCESS_URL(country_image_objectref_column, 'r'))'If the city in the first image is within the country of the second image, provide a ten word description of the city' city_image country_image
- Specify a
OUTPUT_SCHEMA: a schema to use to format the model's response. TheOUTPUT_SCHEMAvalue must a SQL schema definition, similar to that used in theCREATE TABLEstatement. The following data types are supported:INT64FLOAT64BOOLSTRINGARRAYSTRUCT
When using the
OUTPUT_SCHEMAargument to generate structured data based on prompts from a table, it is important to understand the prompt data in order to specify an appropriate schema.For example, say you are analyzing movie review content from a table that has the following fields:
- movie_id
- review
- prompt
Then you might create prompt text by running a query similar to the following:
UPDATE `mydataset.movie_review` SET prompt = CONCAT('Extract the key words and key sentiment from the text below: ', review) WHERE review IS NOT NULL;
And you might specify a
OUTPUT_SCHEMAvalue similar to"keywords ARRAY<STRING>, sentiment STRING" AS output_schema.MAX_OUTPUT_TOKENS: anINT64value that sets the maximum number of tokens that can be generated in the response. A token might be smaller than a word and is approximately four characters. One hundred tokens correspond to approximately 60-80 words.This value must be in the range[1,8192]. Specify a lower value for shorter responses and a higher value for longer responses. If you don't specify a value, the model determines an appropriate value.TOP_P: aFLOAT64value in the range[0.0,1.0]that changes how the model selects tokens for output. Specify a lower value for less random responses and a higher value for more random responses. If you don't specify a value, the model determines an appropriate value.Tokens are selected from the most to least probable until the sum of their probabilities equals the
TOP_Pvalue. For example, if tokens A, B, and C have a probability of0.3,0.2, and0.1, and theTOP_Pvalue is0.5, then the model selects either A or B as the next token by using theTEMPERATUREvalue and doesn't consider C.TEMPERATURE: aFLOAT64value in the range[0.0,2.0]that controls the degree of randomness in token selection. LowerTEMPERATUREvalues are good for prompts that require a more deterministic and less open-ended or creative response, while higherTEMPERATUREvalues can lead to more diverse or creative results. ATEMPERATUREvalue of0is deterministic, meaning that the highest probability response is always selected. If you don't specify a value, the model determines an appropriate value.STOP_SEQUENCES: anARRAY<STRING>value that removes the specified strings if they are included in responses from the model. Strings are matched exactly, including capitalization. The default is an empty array.SAFETY_SETTINGS: anARRAY<STRUCT<STRING AS category, STRING AS threshold>>value that configures content safety thresholds to filter responses. The first element in the struct specifies a harm category, and the second element in the struct specifies a corresponding blocking threshold. The model filters out content that violate these settings. You can only specify each category once. For example, you can't specify bothSTRUCT('HARM_CATEGORY_DANGEROUS_CONTENT' AS category, 'BLOCK_MEDIUM_AND_ABOVE' AS threshold)andSTRUCT('HARM_CATEGORY_DANGEROUS_CONTENT' AS category, 'BLOCK_ONLY_HIGH' AS threshold). If there is no safety setting for a given category, theBLOCK_MEDIUM_AND_ABOVEsafety setting is used.Supported categories are as follows:
HARM_CATEGORY_HATE_SPEECHHARM_CATEGORY_DANGEROUS_CONTENTHARM_CATEGORY_HARASSMENTHARM_CATEGORY_SEXUALLY_EXPLICIT
Supported thresholds are as follows:
BLOCK_NONE(Restricted)BLOCK_LOW_AND_ABOVEBLOCK_MEDIUM_AND_ABOVE(Default)BLOCK_ONLY_HIGHHARM_BLOCK_THRESHOLD_UNSPECIFIED
For more information, see Harm categories and How to configure content filters.
REQUEST_TYPE: aSTRINGvalue that specifies the type of inference request to send to the Gemini model. The request type determines what quota the request uses. Valid values are as follows:DEDICATED: TheAI.GENERATE_TABLEfunction only uses Provisioned Throughput quota. TheAI.GENERATE_TABLEfunction returns the errorProvisioned throughput is not purchased or is not activeif Provisioned Throughput quota isn't available.SHARED: TheAI.GENERATE_TABLEfunction only uses dynamic shared quota (DSQ), even if you have purchased Provisioned Throughput quota.UNSPECIFIED: TheAI.GENERATE_TABLEfunction uses quota as follows:- If you haven't purchased Provisioned Throughput quota,
the
AI.GENERATE_TABLEfunction uses DSQ quota. - If you have purchased Provisioned Throughput quota,
the
AI.GENERATE_TABLEfunction uses the Provisioned Throughput quota first. If requests exceed the Provisioned Throughput quota, the overflow traffic uses DSQ quota.
- If you haven't purchased Provisioned Throughput quota,
the
The default value is
UNSPECIFIED.
Examples
The following examples demonstrate how to use AI.GENERATE_TABLE.
Format text input
The following example shows a request that provides a SQL schema to format the model's response:
SELECT address, age, is_married, name, phone_number, weight_in_pounds FROM AI.GENERATE_TABLE( MODEL `mydataset.gemini_model`, ( SELECT 'John Smith is a 20-year old single man living at 1234 NW 45th St, Kirkland WA, 98033. He has two phone numbers 123-123-1234, and 234-234-2345. He is 200.5 pounds.' AS prompt ), STRUCT("address STRING, age INT64, is_married BOOL, name STRING, phone_number ARRAY<STRING>, weight_in_pounds FLOAT64" AS output_schema, 8192 AS max_output_tokens));
The results look similar to the following:
+-------------------------------------+-----+------------+------------+---------------+------------------+ | address | age | is_married | name | phone_number | weight_in_pounds | +-------------------------------------+-----+------------+------------+---------------+------------------+ | 1234 NW 45th St, Kirkland WA, 98033 | 20 | No | John Smith | 123-123-1234 | 200.5 | | | | | | 234-234-2345 | | | | | | | | | +-------------------------------------+-----+------------+------------+---------------+------------------+
Create a column based on image data
The following example shows how to to create and populate an
image_description column by analyzing a product image that is stored as
an ObjectRef value in a standard table:
CREATE OR REPLACE TABLE `mydataset.products` AS SELECT product_id, product_name, image, image_description FROM AI.GENERATE_TABLE( MODEL `mydataset.gemini`, ( SELECT ('Can you describe the following image?', OBJ.GET_ACCESS_URL(image, 'r')) AS prompt, * FROM `mydataset.products` ), STRUCT ( "image_description STRING" AS output_schema ));
Details
The model and input table must be in the same region.
Output
AI.GENERATE_TABLE returns the following columns:
All columns in the input table.
All columns specified in the
output_responseargument.full_response: this is the JSON response from theprojects.locations.endpoints.generateContentcall to the model. The generated data is in thetextelement.status: aSTRINGvalue that contains the API response status for the corresponding row. This value is empty if the operation was successful.
Locations
AI.GENERATE_TABLE must run in the same
region or multi-region as the remote model that the
function references.
You can create remote models over Gemini models in the
supported regions
for the given Gemini model, and also in the US and EU multi-regions.
Quotas
See Vertex AI and Cloud AI service functions quotas and limits.
Known issues
This section contains information about known issues.Resource exhausted errors
Sometimes after a query job that uses this function finishes successfully, some returned rows contain the following error message:
A retryable error occurred: RESOURCE EXHAUSTED error from <remote endpoint>
This issue occurs because BigQuery query jobs finish successfully
even if the function fails for some of the rows. The function fails when the
volume of API calls to the remote endpoint exceeds the quota limits for that
service. This issue occurs most often when you are running multiple parallel
batch queries. BigQuery retries these calls, but if the retries
fail, the resource exhausted error message is returned.
To iterate through inference calls until all rows are successfully processed, you can use the BigQuery remote inference SQL scripts or the BigQuery remote inference pipeline Dataform package.
What's next
- Get step-by-step instructions on how to generate structured data using your own data.
- For more information about using Vertex AI models to generate text and embeddings, see Generative AI overview.
- For more information about using Cloud AI APIs to perform AI tasks, see AI application overview.
- For more information about supported SQL statements and functions for generative AI models, see End-to-end user journeys for generative AI models.