lvyanquan commented on code in PR #4394: URL: https://github.com/apache/flink-cdc/pull/4394#discussion_r3287930382
########## flink-cdc-runtime/src/main/java/org/apache/flink/cdc/runtime/functions/impl/AiFunctions.java: ########## @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.cdc.runtime.functions.impl; + +import org.apache.flink.cdc.common.model.AiModelClient; +import org.apache.flink.cdc.common.model.abilities.SupportsEmbedding; +import org.apache.flink.cdc.common.model.abilities.SupportsTextGeneration; +import org.apache.flink.cdc.common.types.RowType; +import org.apache.flink.cdc.common.types.variant.BinaryVariant; +import org.apache.flink.cdc.common.types.variant.BinaryVariantInternalBuilder; +import org.apache.flink.cdc.runtime.ai.AiTextFunctionDef; + +import org.apache.flink.shaded.guava31.com.google.common.primitives.Floats; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** Built-in AI functions available as static imports in Janino-compiled transform expressions. */ +public class AiFunctions { + + private static final Logger LOG = LoggerFactory.getLogger(AiFunctions.class); + + /** General-purpose text completion with a user-provided system prompt. */ + public static BinaryVariant aiComplete(AiModelClient model, String input, String systemPrompt) { + return invokeTextGeneration(model, AiTextFunctionDef.AI_COMPLETE, input, systemPrompt); + } + + /** Text summarization with a maximum length constraint. */ + public static BinaryVariant aiSummarize(AiModelClient model, String input, int maxLength) { + return invokeTextGeneration(model, AiTextFunctionDef.AI_SUMMARIZE, input, maxLength); + } + + /** Text embedding that converts input text to a vector representation. */ + public static List<Float> aiEmbed(AiModelClient model, String input) { + if (!(model instanceof SupportsEmbedding)) { + throw new UnsupportedOperationException( + "Model " + model.getClass().getName() + " does not support embedding"); + } + + long startTime = 0; + if (LOG.isDebugEnabled()) { + startTime = System.currentTimeMillis(); + } + float[] embeddingResult = ((SupportsEmbedding) model).embed(input); + if (LOG.isDebugEnabled()) { + long endTime = System.currentTimeMillis(); + LOG.debug( + "Generated {}-dim vector in {} ms", + embeddingResult.length, + endTime - startTime); + } + + return Floats.asList(embeddingResult); + } + + private static BinaryVariant invokeTextGeneration( + AiModelClient model, AiTextFunctionDef funcDef, String input, Object... args) { + if (!(model instanceof SupportsTextGeneration)) { + throw new UnsupportedOperationException( + "Model " + model.getClass().getName() + " does not support text generation"); + } + StringBuilder promptBuilder = new StringBuilder(); + promptBuilder.append(funcDef.buildPrompt(args)); + promptBuilder.append("\n").append(buildOutputSchemaHint(funcDef.getOutputType())); + + String systemPrompt = promptBuilder.toString(); + + long startTime = 0; + if (LOG.isDebugEnabled()) { + startTime = System.currentTimeMillis(); + } + String json = ((SupportsTextGeneration) model).generate(systemPrompt, input); + + if (LOG.isDebugEnabled()) { + long endTime = System.currentTimeMillis(); + LOG.debug("Generated {} characters in {} ms", json.length(), endTime - startTime); Review Comment: Is it possible for json to be null, thereby causing an NPE? ########## flink-cdc-runtime/src/main/java/org/apache/flink/cdc/runtime/functions/impl/AiFunctions.java: ########## @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.cdc.runtime.functions.impl; + +import org.apache.flink.cdc.common.model.AiModelClient; +import org.apache.flink.cdc.common.model.abilities.SupportsEmbedding; +import org.apache.flink.cdc.common.model.abilities.SupportsTextGeneration; +import org.apache.flink.cdc.common.types.RowType; +import org.apache.flink.cdc.common.types.variant.BinaryVariant; +import org.apache.flink.cdc.common.types.variant.BinaryVariantInternalBuilder; +import org.apache.flink.cdc.runtime.ai.AiTextFunctionDef; + +import org.apache.flink.shaded.guava31.com.google.common.primitives.Floats; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** Built-in AI functions available as static imports in Janino-compiled transform expressions. */ +public class AiFunctions { + + private static final Logger LOG = LoggerFactory.getLogger(AiFunctions.class); + + /** General-purpose text completion with a user-provided system prompt. */ + public static BinaryVariant aiComplete(AiModelClient model, String input, String systemPrompt) { + return invokeTextGeneration(model, AiTextFunctionDef.AI_COMPLETE, input, systemPrompt); + } + + /** Text summarization with a maximum length constraint. */ + public static BinaryVariant aiSummarize(AiModelClient model, String input, int maxLength) { + return invokeTextGeneration(model, AiTextFunctionDef.AI_SUMMARIZE, input, maxLength); + } + + /** Text embedding that converts input text to a vector representation. */ + public static List<Float> aiEmbed(AiModelClient model, String input) { + if (!(model instanceof SupportsEmbedding)) { + throw new UnsupportedOperationException( + "Model " + model.getClass().getName() + " does not support embedding"); + } + + long startTime = 0; + if (LOG.isDebugEnabled()) { + startTime = System.currentTimeMillis(); + } + float[] embeddingResult = ((SupportsEmbedding) model).embed(input); + if (LOG.isDebugEnabled()) { + long endTime = System.currentTimeMillis(); + LOG.debug( + "Generated {}-dim vector in {} ms", + embeddingResult.length, + endTime - startTime); + } + + return Floats.asList(embeddingResult); + } + + private static BinaryVariant invokeTextGeneration( + AiModelClient model, AiTextFunctionDef funcDef, String input, Object... args) { + if (!(model instanceof SupportsTextGeneration)) { + throw new UnsupportedOperationException( + "Model " + model.getClass().getName() + " does not support text generation"); + } + StringBuilder promptBuilder = new StringBuilder(); + promptBuilder.append(funcDef.buildPrompt(args)); + promptBuilder.append("\n").append(buildOutputSchemaHint(funcDef.getOutputType())); + + String systemPrompt = promptBuilder.toString(); + + long startTime = 0; + if (LOG.isDebugEnabled()) { + startTime = System.currentTimeMillis(); + } + String json = ((SupportsTextGeneration) model).generate(systemPrompt, input); + + if (LOG.isDebugEnabled()) { + long endTime = System.currentTimeMillis(); + LOG.debug("Generated {} characters in {} ms", json.length(), endTime - startTime); Review Comment: Is it possible for json to be null, thereby causing an NPE? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
