yunfengzhou-hub commented on code in PR #26652: URL: https://github.com/apache/flink/pull/26652#discussion_r2141804884
########## flink-models/flink-model-openai/src/main/java/org/apache/flink/model/openai/OpenAIUtils.java: ########## @@ -0,0 +1,115 @@ +/* + * 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.model.openai; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.util.Preconditions; + +import com.openai.client.OpenAIClientAsync; +import com.openai.client.okhttp.OpenAIOkHttpClientAsync; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +/** Utility class related to Open AI SDK. */ +public class OpenAIUtils { + private static final Logger LOG = LoggerFactory.getLogger(OpenAIUtils.class); + + private static final Object LOCK = new Object(); + + private static final Map<ReferenceKey, ReferenceValue> cache = new ConcurrentHashMap<>(); + + public static OpenAIClientAsync createAsyncClient(String baseUrl, String apiKey, int numRetry) { + synchronized (LOCK) { + ReferenceKey key = new ReferenceKey(baseUrl, apiKey); + ReferenceValue value = cache.get(key); + if (value != null) { + LOG.info("Returning an existing OpenAI client."); + value.referenceCount.incrementAndGet(); + return value.client; + } + + LOG.info("Building a new OpenAI client."); + OpenAIClientAsync client = + OpenAIOkHttpClientAsync.builder() + .apiKey(apiKey) + .baseUrl(baseUrl) + .maxRetries(numRetry) + .build(); + cache.put(key, new ReferenceValue(client)); + return client; + } + } + + public static void releaseAsyncClient(String baseUrl, String apiKey) { + synchronized (LOCK) { + ReferenceKey key = new ReferenceKey(baseUrl, apiKey); + ReferenceValue value = cache.get(key); + Preconditions.checkNotNull( + value, "The creation and release of OpenAI client does not match."); + int count = value.referenceCount.decrementAndGet(); + if (count == 0) { + LOG.info("Closing the OpenAI client."); + cache.remove(key); + value.client.close(); Review Comment: The OpenAI SDK's javadoc did not mention the possible exceptions that could be thrown when the close failed, nor have I encountered such failures during my local tests that connect to actual online model platforms. Thus I think it should be OK if we do not add exception handling logic here. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org