Skip to content

Commit 344e7e3

Browse files
authored
[FLINK-37797][doc] Add model documentation (#26694)
1 parent 2b13a56 commit 344e7e3

File tree

12 files changed

+651
-1
lines changed

12 files changed

+651
-1
lines changed

docs/content.zh/docs/dev/table/sql/alter.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Flink SQL 目前支持以下 ALTER 语句:
3737
- ALTER DATABASE
3838
- ALTER FUNCTION
3939
- ALTER CATALOG
40+
- ALTER MODEL
4041

4142
## 执行 ALTER 语句
4243

@@ -604,3 +605,51 @@ ALTER CATALOG cat2 COMMENT 'comment for catalog ''cat2''';
604605
```
605606

606607
{{< top >}}
608+
609+
## ALTER MODEL
610+
611+
```sql
612+
ALTER MODEL [IF EXISTS] [catalog_name.][db_name.]model_name
613+
SET (key1=val1, key2=val2, ...)
614+
| RESET (key1, key2, ...)
615+
| RENAME TO new_model_name
616+
```
617+
618+
**IF EXISTS**
619+
620+
若模型不存在,则不进行任何操作。
621+
622+
### SET
623+
624+
为指定的模型设置一个或多个属性。若个别属性已经存在,则使用新值覆盖旧值。
625+
626+
`SET` 语句示例如下。
627+
628+
```sql
629+
-- 设置模型的属性
630+
ALTER MODEL MyModel SET ('model-type'='linear', 'version'='2.0');
631+
```
632+
633+
### RESET
634+
635+
为指定的模型重置一个或多个属性。
636+
637+
`RESET` 语句示例如下。
638+
639+
```sql
640+
-- 重置模型的属性
641+
ALTER MODEL MyModel RESET ('model-type', 'version');
642+
```
643+
644+
### RENAME TO
645+
646+
将模型重命名为新的名称。
647+
648+
`RENAME TO` 语句示例如下。
649+
650+
```sql
651+
-- 重命名模型
652+
ALTER MODEL MyModel RENAME TO NewModel;
653+
```
654+
655+
{{< top >}}

docs/content.zh/docs/dev/table/sql/create.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ CREATE 语句用于向当前或指定的 [Catalog]({{< ref "docs/dev/table/catal
3838
- CREATE DATABASE
3939
- CREATE VIEW
4040
- CREATE FUNCTION
41+
- CREATE MODEL
4142

4243
## 执行 CREATE 语句
4344

@@ -893,3 +894,64 @@ Language tag 用于指定 Flink runtime 如何执行这个函数。目前,只
893894
指定包含该函数的实现及其依赖的 jar 资源列表。该 jar 应该位于 Flink 当前支持的本地或远程[文件系统]({{< ref "docs/deployment/filesystems/overview" >}}) 中,比如 hdfs/s3/oss。
894895

895896
<span class="label label-danger">注意</span> 目前只有 JAVA、SCALA 语言支持 USING 子句。
897+
898+
## CREATE MODEL
899+
```sql
900+
CREATE [TEMPORARY] MODEL [IF NOT EXISTS] [catalog_name.][db_name.]model_name
901+
[(
902+
{ <input_column_definition> }[ , ...n]
903+
{ <output_column_definition> }[ , ...n]
904+
)]
905+
[COMMENT model_comment]
906+
WITH (key1=val1, key2=val2, ...)
907+
908+
<input_column_definition>:
909+
column_name column_type [COMMENT column_comment]
910+
911+
<output_column_definition>:
912+
column_name column_type [COMMENT column_comment]
913+
```
914+
915+
创建一个有 catalog 和数据库命名空间的模型。若 catalog 中已经存在同名模型,则无法注册。
916+
917+
**TEMPORARY**
918+
919+
创建一个有 catalog 和数据库命名空间的临时模型,并覆盖原有的模型。
920+
921+
**IF NOT EXISTS**
922+
923+
若该模型已经存在,则不会进行任何操作。
924+
925+
**Input/Output**
926+
927+
输入列定义了将用于模型推理的特征。输出列定义了模型将产生的预测结果。每个列必须有名称和数据类型。
928+
929+
**COMMENT**
930+
931+
为模型添加注释。
932+
933+
**WITH OPTIONS**
934+
935+
用于存储与此模型相关的额外信息的模型属性。这些属性通常用于查找和创建底层模型提供者。表达式 `key1=val1` 中的键和值都应该是字符串字面量。
936+
937+
**注意:** 模型属性和支持的模型类型可能因底层模型提供者而异。
938+
939+
### 示例
940+
941+
以下示例展示了 `CREATE MODEL` 语句的使用方法:
942+
943+
```sql
944+
CREATE MODEL sentiment_analysis_model
945+
INPUT (text STRING COMMENT '用于情感分析的输入文本')
946+
OUTPUT (sentiment STRING COMMENT '预测的情感(positive/negative/neutral/mixed)')
947+
COMMENT '用于文本情感分析的模型'
948+
WITH (
949+
'provider' = 'openai',
950+
'endpoint' = 'https://api.openai.com/v1/chat/completions',
951+
'api-key' = '<YOUR KEY>',
952+
'model'='gpt-3.5-turbo',
953+
'system-prompt' = 'Classify the text below into one of the following labels: [positive, negative, neutral, mixed]. Output only the label.'
954+
);
955+
```
956+
957+
{{< top >}}

docs/content.zh/docs/dev/table/sql/describe.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,10 @@ Flink SQL> DESC JOB '228d70913eab60dda85c5e7f78b5782c';
486486
```
487487

488488
<span class="label label-danger">Attention</span> DESCRIBE JOB 语句仅适用于 [SQL CLI]({{< ref "docs/dev/table/sqlClient" >}}) 或者 [SQL Gateway]({{< ref "docs/dev/table/sql-gateway/overview" >}}).
489+
490+
### DESCRIBE MODEL
491+
```sql
492+
{ DESCRIBE | DESC } [catalog_name.][db_name.]model_name
493+
```
494+
495+
{{< top >}}

docs/content.zh/docs/dev/table/sql/drop.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Flink SQL 目前支持以下 DROP 语句:
3737
- DROP DATABASE
3838
- DROP VIEW
3939
- DROP FUNCTION
40+
- DROP MODEL
4041

4142
## 执行 DROP 语句
4243

@@ -229,3 +230,19 @@ DROP [TEMPORARY|TEMPORARY SYSTEM] FUNCTION [IF EXISTS] [catalog_name.][db_name.]
229230
**IF EXISTS**
230231

231232
若函数不存在,则不会进行任何操作。
233+
234+
## DROP MODEL
235+
236+
```sql
237+
DROP [TEMPORARY] MODEL [IF EXISTS] [catalog_name.][db_name.]model_name
238+
```
239+
240+
删除一个有 catalog 和数据库命名空间的模型。若需要删除的模型不存在并且没有用 `IF EXISTS`,则会产生异常。
241+
242+
**TEMPORARY**
243+
244+
删除一个有 catalog 和数据库命名空间的临时模型。
245+
246+
**IF EXISTS**
247+
248+
若模型不存在,则不会进行任何操作。
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: "模型推理"
3+
weight: 7
4+
type: docs
5+
---
6+
<!--
7+
Licensed to the Apache Software Foundation (ASF) under one
8+
or more contributor license agreements. See the NOTICE file
9+
distributed with this work for additional information
10+
regarding copyright ownership. The ASF licenses this file
11+
to you under the Apache License, Version 2.0 (the
12+
"License"); you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing,
18+
software distributed under the License is distributed on an
19+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
KIND, either express or implied. See the License for the
21+
specific language governing permissions and limitations
22+
under the License.
23+
-->
24+
25+
# 模型推理
26+
27+
{{< label Streaming >}}
28+
29+
Flink SQL 提供了 `ML_PREDICT` 表值函数(TVF)来在 SQL 查询中执行模型推理。该函数允许您直接在 SQL 中对数据流应用机器学习模型。
30+
请参阅[模型创建]({{< ref "docs/dev/table/sql/create#create-model" >}})了解如何创建模型。
31+
32+
## ML_PREDICT 函数
33+
34+
`ML_PREDICT` 函数接收一个表输入,对其应用模型,并返回一个包含模型预测结果的新表。当底层模型允许时,该函数提供同步/异步推理模式支持。
35+
36+
### 语法
37+
38+
```sql
39+
SELECT * FROM
40+
ML_PREDICT(
41+
TABLE input_table,
42+
MODEL model_name,
43+
DESCRIPTOR(feature_columns),
44+
[CONFIG => MAP['key', 'value']]
45+
)
46+
```
47+
48+
### 参数
49+
50+
- `input_table`:包含待处理数据的输入表
51+
- `model_name`:用于推理的模型名称
52+
- `feature_columns`:指定输入表中哪些列应作为模型特征的描述符
53+
- `config`:(可选)模型推理的配置选项映射
54+
55+
### 配置选项
56+
57+
以下配置选项可以在配置映射中指定:
58+
59+
{{< generated/ml_predict_runtime_config_configuration >}}
60+
61+
### 示例
62+
63+
```sql
64+
-- 基本用法
65+
SELECT * FROM ML_PREDICT(
66+
TABLE input_table,
67+
MODEL my_model,
68+
DESCRIPTOR(feature1, feature2)
69+
);
70+
71+
-- 使用配置选项
72+
SELECT * FROM ML_PREDICT(
73+
TABLE input_table,
74+
MODEL my_model,
75+
DESCRIPTOR(feature1, feature2),
76+
MAP['async', 'true', 'timeout', '100s']
77+
);
78+
79+
-- 使用命名参数
80+
SELECT * FROM ML_PREDICT(
81+
INPUT => TABLE input_table,
82+
MODEL => MODEL my_model,
83+
ARGS => DESCRIPTOR(feature1, feature2),
84+
CONFIG => MAP['async', 'true']
85+
);
86+
```
87+
88+
### 输出
89+
90+
输出表包含输入表的所有列以及模型的预测列。预测列根据模型的输出模式添加。
91+
92+
### 注意事项
93+
94+
1. 在使用 `ML_PREDICT` 之前,模型必须在目录中注册。
95+
2. 描述符中指定的特征列数量必须与模型的输入模式匹配。
96+
3. 如果输出中的列名与输入表中的现有列名冲突,将在输出列名中添加索引以避免冲突。例如,如果输出列名为 `prediction`,且输入表中已存在同名列,则输出列将被重命名为 `prediction0`
97+
4. 对于异步推理,模型提供者必须支持 `AsyncPredictRuntimeProvider` 接口。
98+
5. `ML_PREDICT` 仅支持仅追加表。不支持 CDC(变更数据捕获)表,因为 `ML_PREDICT` 的结果是非确定性的。
99+
100+
### 模型提供者
101+
102+
`ML_PREDICT` 函数使用 `ModelProvider` 执行实际的模型推理。提供者根据注册模型时指定的提供者标识符进行查找。有两种类型的模型提供者:
103+
104+
1. `PredictRuntimeProvider`:用于同步模型推理
105+
- 实现 `createPredictFunction` 方法以创建同步预测函数
106+
- 当配置中 `async` 设置为 `false` 时使用
107+
108+
2. `AsyncPredictRuntimeProvider`:用于异步模型推理
109+
- 实现 `createAsyncPredictFunction` 方法以创建异步预测函数
110+
- 当配置中 `async` 设置为 `true` 时使用
111+
- 需要额外的超时和缓冲区容量配置
112+
113+
如果配置中未设置 `async`,系统将选择同步或异步模型提供者,如果两者都存在,则优先使用异步模型提供者。
114+
115+
### 错误处理
116+
117+
在以下情况下,函数将抛出异常:
118+
- 模型中不存在于目录中
119+
- 特征列数量与模型的输入模式不匹配
120+
- 缺少模型参数
121+
- 提供的参数过少或过多
122+
123+
### 性能考虑
124+
125+
1. 对于高吞吐量场景,考虑使用异步推理模式。
126+
2. 为异步推理配置适当的超时和缓冲区容量值。
127+
3. 函数的性能取决于底层模型提供者的实现。
128+
129+
### 相关语句
130+
131+
- [模型创建]({{< ref "docs/dev/table/sql/create#create-model" >}})
132+
- [模型修改]({{< ref "docs/dev/table/sql/alter#alter-model" >}})
133+
134+
{{< top >}}

docs/content.zh/docs/dev/table/sql/show.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ SHOW CREATE 语句用于打印给定对象的创建 DDL 语句。当前的 SHOW
4949
- SHOW FULL MODULES
5050
- SHOW JARS
5151
- SHOW JOBS
52+
- SHOW MODELS
53+
- SHOW CREATE MODEL
5254

5355

5456
## 执行 SHOW 语句
@@ -1028,4 +1030,59 @@ SHOW JOBS
10281030

10291031
<span class="label label-danger">Attention</span> 当前 SHOW JOBS 命令只能在 [SQL CLI]({{< ref "docs/dev/table/sqlClient" >}}) 或者 [SQL Gateway]({{< ref "docs/dev/table/sql-gateway/overview" >}}) 中使用.
10301032

1033+
## SHOW MODELS
1034+
1035+
```sql
1036+
SHOW MODELS [ ( FROM | IN ) [catalog_name.]database_name ] [ [NOT] (LIKE | ILIKE) <sql_like_pattern> ]
1037+
```
1038+
1039+
展示指定 catalog 和 database 下的所有模型。
1040+
如果没有指定 catalog 和 database,则将使用当前 catalog 和当前 database。另外可以用 `<sql_like_pattern>` 来过滤要返回的模型。
1041+
1042+
**LIKE**
1043+
根据可选的 `LIKE` 语句与 `<sql_like_pattern>` 是否模糊匹配的所有模型。
1044+
1045+
`LIKE` 子句中 SQL 正则式的语法与 `MySQL` 方言中的语法相同。
1046+
* `%` 匹配任意数量的字符, 也包括0数量字符, `\%` 匹配一个 `%` 字符.
1047+
* `_` 只匹配一个字符, `\_` 匹配一个 `_` 字符.
1048+
1049+
**ILIKE**
1050+
它的行为和 LIKE 相同,只是对于大小写是不敏感的。
1051+
1052+
## SHOW CREATE MODEL
1053+
1054+
```sql
1055+
SHOW CREATE MODEL [[catalog_name.]db_name.]model_name
1056+
```
1057+
1058+
展示创建指定模型的 create 语句。
1059+
1060+
该语句的输出内容包括模型名称、模型输入和输出、模型参数和配置信息,使您可以直观地了解相应模型的元数据。
1061+
1062+
假设 `model1` 是按如下方式创建的:
1063+
```sql
1064+
CREATE MODEL my_model
1065+
INPUT(text STRING)
1066+
OUTPUT(response STRING)
1067+
WITH (
1068+
'provider' = 'openai',
1069+
);
1070+
```
1071+
1072+
展示模型创建语句:
1073+
```sql
1074+
show create model model1;
1075+
+---------------------------------------------------------------------------------------------+
1076+
| result |
1077+
+---------------------------------------------------------------------------------------------+
1078+
| CREATE MODEL `default_catalog`.`default_database`.`my_model`
1079+
INPUT (`text` STRING)
1080+
OUTPUT (`response` STRING) WITH (
1081+
'provider' = 'openai'
1082+
)
1083+
|
1084+
+---------------------------------------------------------------------------------------------+
1085+
1 row in set
1086+
```
1087+
10311088
{{< top >}}

0 commit comments

Comments
 (0)