本页面介绍了如何使用 AlloyDB AI 查询引擎提供的 AI 赋能的 SQL 运算符进行查询。您可以使用 ai.if
(用于过滤条件)、ai.rank
和 ai.generate
运算符将自然语言与 SQL 查询相结合。
如需按本页面上的说明操作,您必须了解 AlloyDB,并熟悉生成式 AI 概念。
AlloyDB AI 会预留并创建 ai
架构。
准备工作
在 SQL 运算符中使用自然语言之前,请执行以下操作:
- 验证
google_ml_integration
扩展程序是否已安装。 - 验证
google_ml_integration.enable_model_support
标志是否已设置为on
。 - 与 Vertex AI 集成。
- 使用您所在区域支持的 Gemini 模型。
与 Vertex AI 集成并安装扩展程序
- 与 Vertex AI 集成。
- 确保已安装最新版本的
google_ml_integration
。如需检查已安装的版本,请运行以下命令:
SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration'; extversion ------------ 1.4.3 (1 row)
如果未安装该扩展程序,或者安装的版本低于 1.4.3,请运行以下命令来更新该扩展程序:
CREATE EXTENSION IF NOT EXISTS google_ml_integration; ALTER EXTENSION google_ml_integration UPDATE;
如果您在运行上述命令时遇到问题,或者在运行上述命令后扩展程序未更新到 1.4.3 版,请与 AlloyDB 支持团队联系。
确保是最新版本后,运行
upgrade_to_preview_version
过程来安装预览版功能:CALL google_ml.upgrade_to_preview_version(); SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration'; extversion ------------ 1.4.4 (1 row)
使用您所在区域支持的 Gemini 模型
如果您的 AlloyDB for PostgreSQL 集群位于不支持 gemini-2.0-flash
的区域,您可以使用 model_id parameter
在您所在的区域中使用其他可用的 Gemini 模型。
或者,您可以注册 Gemini 模型端点,并将该模型 ID 提供给 AI 运算符。如需了解详情,请参阅使用模型端点管理注册和调用远程 AI 模型。
以下示例展示了如何注册另一个 Gemini 端点。在此示例中,第二个 Gemini 端点是 gemini-2.0-flash
的全球端点。您可以通过传递 model_id =>
gemini-2.0-flash-global` 作为附加参数,将此已注册的模型与 AI 运算符搭配使用。
CALL
google_ml.create_model(
model_id => 'gemini-2.0-flash-global',
model_type => 'llm',
model_provider => 'google',
model_qualified_name => 'gemini-2.0-flash',
model_request_url => 'https://aiplatform.googleapis.com/v1/projects/<project_id>/locations/global/publishers/google/models/gemini-2.0-flash:generateContent',
model_auth_type => 'alloydb_service_agent_iam'
);
在查询中使用过滤条件
AlloyDB AI 提供多个 AI 赋能的 SQL 函数,让您可以在数据库查询中直接使用自然语言处理和 LLM,包括 ai.if
和 ai.rank
运算符。
过滤条件
如需评估是否满足以自然语言陈述的条件,请使用 ai.if
/google_ml.if
运算符。该函数会返回布尔值 true 或 false,如果未明确检测到输出,则返回 false
。
- Function signature
FUNCTION ai.if(prompt TEXT, model_id VARCHAR(100) DEFAULT NULL) RETURNS bool
以下示例展示了如何使用 ai.if
运算符作为过滤条件,查找正面评价数量超过 500 且位于人口超过 100,000 的城市中的餐厅。该示例使用 restaurant_reviews
,并且包含评价和城市位置等数据。ai.if
运算符可帮助您了解评价情绪,并将数据库中的位置与 Gemini 关于这些位置的人口的一般知识相结合。
SELECT r.name, r.location_city
FROM restaurant_reviews r
WHERE
AI.IF(r.location_city || ' has a population OF more than 100,000 AND the following is a positive review; Review: ' || r.review)
GROUP BY r.name, r.location_city
HAVING COUNT(*) > 500;
以下示例展示了如何使用在使用您所在区域支持的 Gemini 模型中注册的模型。
SELECT r.name, r.location_city
FROM restaurant_reviews r
WHERE
AI.IF(r.location_city || ' has a population of more than 100,000 AND the following is a positive review; Review: ' || r.review, model_id => 'gemini-2.0-flash-global')
GROUP BY r.name, r.location_city
HAVING COUNT(*) > 500;
对查询执行使用 if 运算符的联接
如需执行联接操作,请将 ai.if
/google_ml.if
运算符与联接搭配使用。以下示例查询会查找提及餐厅菜单中的每个菜单项的评价数量。
SELECT item_name, COUNT(*)
FROM menu_items JOIN user_reviews
ON ai.if(
prompt => 'Does the following user review talk about the menu item mentioned ? review: ' || user_reviews.review_text || ' menu item: ' || item_name)
GROUP BY item_name;
文本生成和总结
ai.generate
函数通过将提供的数据与用户的提示相结合来生成文本。
-- Function Signature
FUNCTION ai.generate(prompt TEXT, model_id VARCHAR(100) DEFAULT NULL) RETURNS TEXT
例如,您可以使用以下查询为每条用户评价生成简明摘要。
SELECT
ai.generate(
prompt => 'Summarize the review in 20 words or less. Review: ' || review) AS review_summary
FROM user_reviews
为查询结果评分
如果您需要使用自定义的自然语言指令对查询结果进行排序,请使用 ai.rank
运算符。此函数可让您提供描述排名条件的提示,并返回每项的得分。
-- Function signature
FUNCTION ai.rank(prompt TEXT, model_id VARCHAR(100) DEFAULT NULL) RETURNS real
例如,以下查询使用来自 LLM 的评分获取前 20 条最正面的餐厅评价。
SELECT review AS top20
FROM user_reviews
ORDER BY ai.rank(
'Score the following review according to these rules:
(1) Score OF 8 to 10 IF the review says the food IS excellent.
(2) 4 to 7 IF the review says the food is ok.
(3) 1 to 3 IF the review says the food is not good. Here is the review:' || review) DESC
LIMIT 20;