yuxiqian commented on code in PR #4525: URL: https://github.com/apache/flink-cdc/pull/4525#discussion_r3956629959
########## flink-cdc-runtime/src/main/java/org/apache/flink/cdc/runtime/ai/AiModelClientResolver.java: ########## @@ -0,0 +1,42 @@ +/* + * 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.ai; + +import org.apache.flink.cdc.common.annotation.Internal; +import org.apache.flink.cdc.common.model.AiModelClient; +import org.apache.flink.cdc.common.utils.Preconditions; + +import javax.annotation.Nullable; + +import java.util.Map; + +/** Resolves AI model clients by their logical names during expression evaluation. */ +@Internal +public class AiModelClientResolver { Review Comment: Seems this resolver is just wrapping the Map internally. Can we keep it as-is for simplicity? ########## flink-cdc-runtime/src/main/java/org/apache/flink/cdc/runtime/functions/impl/AiFunctions.java: ########## @@ -101,43 +142,88 @@ private static BinaryVariant generateText( } } - public static List<Float> aiEmbed(AiModelClient model, String input) { + public static List<Float> aiEmbed( + String modelName, String input, AiModelClientResolver modelClientResolver) { if (input == null) { return null; } - if (!(model instanceof SupportsEmbedding)) { - throw new UnsupportedOperationException( - "Model " + model.getClass().getName() + " does not support embedding"); - } - float[] embedding = ((SupportsEmbedding) model).embed(input); + SupportsEmbedding model = + resolveModel( + modelClientResolver, + modelName, + "AI_EMBED", + SupportsEmbedding.class, + "embedding"); + float[] embedding = model.embed(input); return embedding == null ? null : Floats.asList(embedding); } /** Dispatches image-to-text AI functions. */ - public static String aiImageComplete(AiModelClient model, byte[] image, String prompt) { + public static String aiImageComplete( + String modelName, + byte[] image, + String prompt, + AiModelClientResolver modelClientResolver) { if (image == null) { return null; } - if (!(model instanceof SupportsImageTextGeneration)) { - throw new UnsupportedOperationException( - "Model " - + model.getClass().getName() - + " does not support image text generation"); - } - return ((SupportsImageTextGeneration) model).generateTextFromImage(image, prompt); + SupportsImageTextGeneration model = + resolveModel( + modelClientResolver, + modelName, + "AI_IMAGE_COMPLETE", + SupportsImageTextGeneration.class, + "image text generation"); + return model.generateTextFromImage(image, prompt); } /** Dispatches image embedding AI functions. */ - public static List<Float> aiImageEmbed(AiModelClient model, byte[] image) { + public static List<Float> aiImageEmbed( + String modelName, byte[] image, AiModelClientResolver modelClientResolver) { if (image == null) { return null; } - if (!(model instanceof SupportsImageEmbedding)) { + SupportsImageEmbedding model = + resolveModel( + modelClientResolver, + modelName, + "AI_IMAGE_EMBED", + SupportsImageEmbedding.class, + "image embedding"); + float[] embedding = model.embedImage(image); + return embedding == null ? null : Floats.asList(embedding); + } + + private static <T> T resolveModel( + AiModelClientResolver modelClientResolver, + String modelName, + String functionName, + Class<T> requiredCapability, + String capabilityName) { Review Comment: Stating the required interface explicitly could be useful: ```suggestion private static <T> T resolveModel( AiModelClientResolver modelClientResolver, String modelName, String functionName, Class<T> requiredCapability) { ``` and ```java throw new UnsupportedOperationException( "Model '" + modelName + "' could not be used in " + functionName + " because it does not implement " + requiredCapability.getSimpleName() + " interface."); ``` -- 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]
