本页面介绍了如何使用 Spanner 模拟器生成机器学习预测。
Spanner Vertex AI 集成可与 Spanner 模拟器搭配使用,通过 GoogleSQL 或 PostgreSQL ML 预测函数生成预测。模拟器是一个模拟 Spanner 服务器的二进制文件,也可用于单元测试和集成测试。您可以将该模拟器作为开源项目使用,也可以通过 Google Cloud CLI 在本地使用。如需详细了解机器学习预测函数,请参阅 Spanner Vertex AI 集成如何运作?。
您可以将任何模型与模拟器搭配使用,以生成预测。您还可以使用 Vertex AI Model Garden 中的模型或部署到 Vertex AI 端点的模型。由于模拟器未连接到 Vertex AI,因此对于从 Vertex AI Model Garden 使用或部署到 Vertex AI 端点的任何模型,模拟器无法验证相应模型或其架构。
默认情况下,当您将预测函数与模拟器搭配使用时,该函数会根据提供的模型输入和模型输出架构生成一个随机值。您可以使用回调函数修改模型输入和输出,并根据特定行为生成预测结果。
准备工作
安装 Spanner 模拟器
您可以在本地安装模拟器,也可以使用 GitHub 代码库设置模拟器。
选择模型
使用 ML.PREDICT
(对于 GoogleSQL)或 ML_PREDICT_ROW
(对于 PostgreSQL)函数时,必须指定机器学习模型的位置。您可以使用任何经过训练的模型。如果您选择在 Vertex AI Model Garden 中运行的模型或已部署到您的 Vertex AI 端点的模型,则必须为这些模型提供 input
和 output
值。
如需详细了解 Spanner Vertex AI 集成,请参阅 Spanner Vertex AI 集成如何运作?。
生成预测
您可以在模拟器中使用 Spanner ML 预测函数生成预测。
默认行为
您可以将部署到端点的任何模型与 Spanner 模拟器搭配使用,以生成预测。以下示例使用名为 FraudDetection
的模型来生成结果。
GoogleSQL
如需详细了解如何使用 ML.PREDICT
函数生成预测,请参阅使用 SQL 生成机器学习预测。
注册模型
您必须先使用 CREATE MODEL 语句注册模型并提供 input
和 output
值,然后才能通过 ML.PREDICT 函数使用模型:
CREATE MODEL FraudDetection
INPUT (Amount INT64, Name STRING(MAX))
OUTPUT (Outcome BOOL)
REMOTE OPTIONS (
endpoint = '//aiplatform.googleapis.com/projects/PROJECT_ID/locations/REGION_ID/endpoints/ENDPOINT_ID'
);
请替换以下内容:
PROJECT_ID
:模型所在的 Google Cloud 项目的 IDREGION_ID
:模型所在的 Google Cloud 区域的 ID,例如us-central1
ENDPOINT_ID
:模型端点的 ID
运行预测
使用 ML.PREDICT
GoogleSQL 函数生成预测。
SELECT Outcome
FROM ML.PREDICT(
MODEL FraudDetection,
(SELECT 1000 AS Amount, "John Smith" AS Name))
此查询的预期输出为 TRUE
。
PostgreSQL
如需详细了解如何使用 spanner.ML_PREDICT_ROW
函数生成预测,请参阅使用 SQL 生成机器学习预测。
运行预测
使用 spanner.ML_PREDICT_ROW
PostgreSQL 函数生成预测。
SELECT (spanner.ml_predict_row(
'projects/`MODEL_ID`/locations/`REGION_ID`/endpoints/`ENDPOINT_ID`',
'{"instances": [{"Amount": "1000", "Name": "John Smith"}]}'
)->'predictions'->0->'Outcome')::boolean
请替换以下内容:
PROJECT_ID
:模型所在的 Google Cloud 项目的 IDREGION_ID
:模型所在的 Google Cloud 区域的 ID,例如us-central1
ENDPOINT_ID
:模型端点的 ID
此查询的预期输出为 TRUE
。
自定义回调
您可以使用自定义回调函数来实现所选模型行为,并将特定模型输入转换为输出。以下示例使用 Vertex AI Model Garden 中的 gemini-pro
模型和 Spanner 模拟器来使用自定义回调生成预测。
对模型使用自定义回调时,您必须复刻 Spanner 模拟器代码库,然后构建和部署该模型。如需详细了解如何构建和部署 Spanner 模拟器,请参阅 Spanner 模拟器快速入门。
GoogleSQL
注册模型
您必须先使用 CREATE MODEL 语句注册模型,然后才能通过 ML.PREDICT 函数使用模型:
CREATE MODEL GeminiPro
INPUT (prompt STRING(MAX))
OUTPUT (content STRING(MAX))
REMOTE OPTIONS (
endpoint = '//aiplatform.googleapis.com/projects/PROJECT_ID/locations/REGION_ID/publishers/google/models/gemini-pro',
default_batch_size = 1
);
由于模拟器未连接到 Vertex AI,因此您必须提供 input
和 output
值。
请替换以下内容:
PROJECT_ID
:模型所在的 Google Cloud 项目的 IDREGION_ID
:模型所在的 Google Cloud 区域的 ID,例如us-central1
回调
使用回调向 GeminiPro
模型添加自定义逻辑。
absl::Status ModelEvaluator::Predict(
const googlesql::Model* model,
const CaseInsensitiveStringMap<const ModelColumn>& model_inputs,
CaseInsensitiveStringMap<ModelColumn>& model_outputs) {
// Custom logic for GeminiPro.
if (model->Name() == "GeminiPro") {
RET_CHECK(model_inputs.contains("prompt"));
RET_CHECK(model_inputs.find("prompt")->second.value->type()->IsString());
RET_CHECK(model_outputs.contains("content"));
std::string content;
// Process prompts used in tests.
int64_t number;
static LazyRE2 is_prime_prompt = {R"(Is (\d+) a prime number\?)"};
if (RE2::FullMatch(
model_inputs.find("prompt")->second.value->string_value(),
*is_prime_prompt, &number)) {
content = IsPrime(number) ? "Yes" : "No";
} else {
// Default response.
content = "Sorry, I don't understand";
}
*model_outputs["content"].value = googlesql::values::String(content);
return absl::OkStatus();
}
// Custom model prediction logic can be added here.
return DefaultPredict(model, model_inputs, model_outputs);
}
运行预测
使用 ML.PREDICT
GoogleSQL 函数生成预测。
SELECT content
FROM ML.PREDICT(MODEL GeminiPro, (SELECT "Is 7 a prime number?" AS prompt))
此查询的预期输出为 "YES"
。
PostgreSQL
使用 spanner.ML_PREDICT_ROW
PostgreSQL 函数生成预测。
回调
使用回调向 GeminiPro
模型添加自定义逻辑。
absl::Status ModelEvaluator::PgPredict(
absl::string_view endpoint, const googlesql::JSONValueConstRef& instance,
const googlesql::JSONValueConstRef& parameters,
lesql::JSONValueRef prediction) {
if (endpoint.ends_with("publishers/google/models/gemini-pro")) {
RET_CHECK(instance.IsObject());
RET_CHECK(instance.HasMember("prompt"));
std::string content;
// Process prompts used in tests.
int64_t number;
static LazyRE2 is_prime_prompt = {R"(Is (\d+) a prime number\?)"};
if (RE2::FullMatch(instance.GetMember("prompt").GetString(),
*is_prime_prompt, &number)) {
content = IsPrime(number) ? "Yes" : "No";
} else {
// Default response.
content = "Sorry, I don't understand";
}
prediction.SetToEmptyObject();
prediction.GetMember("content").SetString(content);
return absl::OkStatus();
}
// Custom model prediction logic can be added here.
return DefaultPgPredict(endpoint, instance, parameters, prediction);
}
运行预测
SELECT (spanner.ml_predict_row(
'projects/`PROJECT_ID`/locations/`REGION_ID`/publishers/google/models/gemini-pro',
'{"instances": [{"prompt": "Is 7 a prime number?"}]}'
)->'predictions'->0->'content')::text
请替换以下内容:
PROJECT_ID
:模型所在的 Google Cloud 项目的 IDREGION_ID
:模型所在的 Google Cloud 区域的 ID,例如us-central1
此查询的预期输出为 "YES"
。