docs/content.zh/docs/connectors/models/openai.md
OpenAI模型函数允许Flink SQL调用OpenAI API执行推理任务。
该函数支持通过Flink SQL调用远程的OpenAI模型服务进行预测/推理任务。目前支持以下任务类型:
以下示例创建了一个聊天补全模型,并使用它对电影评论进行情感标签预测。
首先,使用如下SQL语句创建聊天补全模型:
CREATE MODEL ai_analyze_sentiment
INPUT (`input` STRING)
OUTPUT (`content` STRING)
WITH (
'provider'='openai',
'endpoint'='https://api.openai.com/v1/chat/completions',
'api-key' = '<YOUR KEY>',
'model'='gpt-3.5-turbo',
'system-prompt' = 'Classify the text below into one of the following labels: [positive, negative, neutral, mixed]. Output only the label.'
);
假设如下数据存储在名为 movie_comment 的表中,预测结果需要存储到名为 print_sink 的表中:
CREATE TEMPORARY VIEW movie_comment(id, movie_name, user_comment, actual_label)
AS VALUES
(1, '好东西', '最爱小孩子猜声音那段,算得上看过的电影里相当浪漫的叙事了。很温和也很有爱。', 'positive');
CREATE TEMPORARY TABLE print_sink(
id BIGINT,
movie_name VARCHAR,
predicit_label VARCHAR,
actual_label VARCHAR
) WITH (
'connector' = 'print'
);
然后就可以使用如下SQL语句对电影评论进行情感标签预测。
INSERT INTO print_sink
SELECT id, movie_name, content as predicit_label, actual_label
FROM ML_PREDICT(
TABLE movie_comment,
MODEL ai_analyze_sentiment,
DESCRIPTOR(user_comment));
{{< generated/model_openai_common_section >}}
{{< generated/model_openai_chat_section >}}
{{< generated/model_openai_embedding_section >}}
当配置 error-handling-strategy 为 ignore 时,您可以选择额外指定以下元数据列,将故障信息展示到您的输出流中。
如果您在Output Schema中定义了这些元数据列,但调用未失败,则这些列将填充为null值。