The AI.SCORE function
This document describes the AI.SCORE function, which uses a
Vertex AI Gemini
model to rate inputs based
on a scoring system that you describe and returns a FLOAT64 value.
BigQuery rewrites your input prompt to generate a scoring rubric
that can improve the consistency and quality of the results.
The AI.SCORE function is commonly used with the ORDER BY clause and works
well when you want to rank items. The following are common use cases:
- Retail: Find the top 5 most negative customer reviews about a product.
- Hiring: Find the top 10 resumes that appear most qualified for a job post.
- Customer success: Find the top 20 best customer support interactions.
Input
AI.SCORE accepts 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. - For more information about accepted multimodal input, see the technical specifications for Gemini.
This function passes your input to a Gemini model and incurs charges in Vertex AI each time it's called.
Syntax
AI.SCORE( [ prompt => ] 'PROMPT', connection_id => 'CONNECTION' )
Arguments
AI.SCORE takes the following arguments:
PROMPT: The input to the model that consists of at least oneSTRINGliteral and at least one reference to either aSTRINGcolumn orObjectRefcolumn. The prompt must be the first argument that you specify. You can provide the prompt value in the following ways:- Specify a concatenation of one or more
STRINGcolumns andSTRINGliterals, such asCONCAT("Rate the review from 1-10: ", review_col). - Specify a
STRUCTvalue that contains two or more fields. The struct must contain at least oneSTRINGliteral and at least one reference to either aSTRINGcolumn or anObjectRefcolumn. You can use the following types of fields within theSTRUCTvalue:
Field type Description Examples STRINGA string literal, or the name of a STRINGcolumn.String literal: 'Rate how positive this review is:'
String column name:my_string_columnObjectRefRuntimeAn
ObjectRefRuntimevalue returned by theOBJ.GET_ACCESS_URLfunction. TheOBJ.GET_ACCESS_URLfunction takes anObjectRefvalue as input, which you can provide by specifying the name of a column that containsObjectRefvalues.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')The function combines
STRUCTfields similarly to aCONCAToperation and concatenates the fields in their specified order. For example:Input:
('Rate this review: ', review_col, ' Use a scale from 1-10.')Transformed function input:
'Rate this review: review_col Use a scale from 1-10.'- Specify a concatenation of one or more
CONNECTION: aSTRINGvalue specifying the Cloud resource connection to use. The following forms are accepted:Connection name:
[PROJECT_ID].LOCATION.CONNECTION_IDFor example,
myproject.us.myconnection.Fully qualified connection ID:
projects/PROJECT_ID/locations/LOCATION/connections/CONNECTION_IDFor example,
projects/myproject/locations/us/connections/myconnection.
Replace the following:
PROJECT_ID: the project ID of the project that contains the connection.LOCATION: the location used by the connection.CONNECTION_ID: the connection ID—for example,myconnection.You can get this value by viewing the connection details in the Google Cloud console and copying the value in the last section of the fully qualified connection ID that is shown in Connection ID. For example,
projects/myproject/locations/connection_location/connections/myconnection.
Output
AI.SCORE returns a FLOAT64 indicating the score assigned to the input. There
is no fixed default range for the score. For best results, provide a scoring
range in your prompt.
If the call to Vertex AI is unsuccessful for any reason,
such as exceeding quota or model unavailability, then the function returns
NULL.
Examples
The following examples show how to use the AI.SCORE function to assign
ratings.
Rate reviews
The following query uses the AI.SCORE function to assign ratings based
on movie reviews of 'The English Patient', alongside the ratings that the
human reviewers gave. It returns the top 10 highest AI rated reviews.
SELECT
AI.SCORE((
"""
On a scale from 1 to 10, rate how much the reviewer liked the movie.
Review:
""", review),
connection_id => 'us.example_connection') AS ai_rating,
reviewer_rating AS human_rating,
review
FROM
`bigquery-public-data.imdb.reviews`
WHERE
title = 'The English Patient'
ORDER BY ai_rating DESC
LIMIT 10;
Rate and filter reviews
The following query builds on the previous example by using the AI.IF function
to filter the results to reviews that mention at least one of the film's main
characters:
SELECT
AI.SCORE((
"""
On a scale from 1 to 10, rate how much the reviewer liked the movie.
Review:
""", review),
connection_id => 'us.example_connection') AS ai_rating,
reviewer_rating AS human_rating,
review
FROM
`bigquery-public-data.imdb.reviews`
WHERE
title = 'The English Patient' AND
AI.IF(
("This review mentions at least one of the film's main cast members: ", review),
connection_id => 'us.example_connection')
ORDER BY ai_rating DESC
LIMIT 10;
Rate images
The following query creates an external table from images of pet products
stored in a publicly available Cloud Storage bucket.
Then, it uses the AI.SCORE function to rate the images
on a scale of 1 to 10 based on how fun they look for a pet, and returns the
top 5 most fun looking items:
-- Create a dataset
CREATE SCHEMA IF NOT EXISTS cymbal_pets;
-- Create an object table
CREATE OR REPLACE EXTERNAL TABLE cymbal_pets.product_images
WITH CONNECTION us.example_connection
OPTIONS (
object_metadata = 'SIMPLE',
uris = ['gs://cloud-samples-data/bigquery/tutorials/cymbal-pets/images/*.png']
);
-- Find the top 5 most fun pet products
SELECT
STRING(OBJ.GET_ACCESS_URL(ref,'r').access_urls.read_url) AS signed_url,
AI.SCORE(
('Rate the product from 1-10 based on how fun it looks for a pet: ',
OBJ.GET_ACCESS_URL(ref, 'r')),
connection_id => 'us.example_connection') AS fun_score
FROM
`cymbal_pets.product_images`
ORDER BY
fun_score DESC
LIMIT 5;
Related functions
The AI.SCORE and
AI.GENERATE_DOUBLE
functions both use models to generate a
number in response to a prompt. The following differences can help you
choose which function to use:
- Prompt optimization:
AI.SCOREautomatically rewrites your prompt to generate a scoring rubric, which is especially helpful if you don't provide clear instructions for how to score input in your prompt. - Input:
AI.SCOREautomatically selects a Gemini model.AI.GENERATE_DOUBLElets you specify a specific model endpoint and model parameters to use. - Output:
AI.SCOREreturns aFLOAT64value, which makes it easier to work with in queries.AI.GENERATE_DOUBLEreturns aSTRUCTthat contains aFLOAT64, as well as additional information about the model call, which is useful if you need to view details such as the safety rating or API response status. - Error handling: If
AI.SCOREproduces an error for any input, then the function returnsNULL.AI.GENERATE_DOUBLErecords details about the errors in its output.
Locations
You can run AI.SCORE in all of the
regions
that support Gemini models, and also in the US and EU
multi-regions.
Quotas
See Generative AI functions quotas and limits.
What's next
- 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.