xintongsong commented on code in PR #695: URL: https://github.com/apache/flink-agents/pull/695#discussion_r3297152894
########## integrations/chat-models/openai/src/main/java/org/apache/flink/agents/integrations/chatmodels/openai/AzureOpenAIChatModelConnection.java: ########## @@ -0,0 +1,276 @@ +/* + * 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.agents.integrations.chatmodels.openai; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.openai.azure.AzureOpenAIServiceVersion; +import com.openai.azure.AzureUrlPathMode; +import com.openai.azure.credential.AzureApiKeyCredential; +import com.openai.client.OpenAIClient; +import com.openai.client.okhttp.OpenAIOkHttpClient; +import com.openai.core.JsonValue; +import com.openai.models.ChatModel; +import com.openai.models.FunctionDefinition; +import com.openai.models.FunctionParameters; +import com.openai.models.chat.completions.ChatCompletion; +import com.openai.models.chat.completions.ChatCompletionCreateParams; +import com.openai.models.chat.completions.ChatCompletionFunctionTool; +import com.openai.models.chat.completions.ChatCompletionTool; +import org.apache.flink.agents.api.chat.messages.ChatMessage; +import org.apache.flink.agents.api.chat.model.BaseChatModelConnection; +import org.apache.flink.agents.api.resource.ResourceContext; +import org.apache.flink.agents.api.resource.ResourceDescriptor; +import org.apache.flink.agents.api.tools.Tool; +import org.apache.flink.agents.api.tools.ToolMetadata; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Chat model integration for Azure OpenAI Service. Built on the openai-java SDK using its built-in + * Azure support ({@link AzureOpenAIServiceVersion}, {@link AzureApiKeyCredential}). + * + * <p>Required connection arguments: + * + * <ul> + * <li><b>api_key</b>: Azure OpenAI API key + * <li><b>api_version</b>: Azure OpenAI REST API version (e.g., {@code "2024-02-01"}) + * <li><b>azure_endpoint</b>: base URL for the Azure OpenAI deployment — either a direct Azure + * resource (e.g., {@code "https://your-resource.openai.azure.com"}) or a proxy/gateway URL + * that fronts an Azure OpenAI service. Custom gateway hostnames also require setting {@code + * azure_url_path_mode} below. + * </ul> + * + * <p>Optional connection arguments: + * + * <ul> + * <li><b>timeout</b> (Number): seconds before an API call times out (default 60) + * <li><b>max_retries</b> (Number): retry attempts on failure (default 3) + * <li><b>azure_url_path_mode</b> (String): one of {@code "AUTO"}, {@code "LEGACY"}, or {@code + * "UNIFIED"} (default {@code "AUTO"}). Controls how the SDK constructs Azure OpenAI request + * URLs. In {@code AUTO} mode the SDK only treats the endpoint as Azure when its hostname + * matches a known suffix (e.g. {@code .openai.azure.com}); custom gateways that proxy Azure + * OpenAI need {@code LEGACY} to force the {@code /openai/deployments/{model}} path. + * </ul> + * + * <p>Example usage: + * + * <pre>{@code + * @ChatModelConnection + * public static ResourceDescriptor azureOpenAIConnection() { + * return ResourceDescriptor.Builder.newBuilder( + * AzureOpenAIChatModelConnection.class.getName()) + * .addInitialArgument("api_key", System.getenv("AZURE_OPENAI_API_KEY")) + * .addInitialArgument("api_version", "2024-02-01") + * .addInitialArgument("azure_endpoint", "https://my-resource.openai.azure.com") + * .build(); + * } + * }</pre> + */ +public class AzureOpenAIChatModelConnection extends BaseChatModelConnection { + + private static final ObjectMapper mapper = new ObjectMapper(); + + private final OpenAIClient client; + + public AzureOpenAIChatModelConnection( + ResourceDescriptor descriptor, ResourceContext resourceContext) { + super(descriptor, resourceContext); + + String apiKey = descriptor.getArgument("api_key"); + if (apiKey == null || apiKey.isBlank()) { + throw new IllegalArgumentException("api_key should not be null or empty."); + } + + String apiVersion = descriptor.getArgument("api_version"); + if (apiVersion == null || apiVersion.isBlank()) { + throw new IllegalArgumentException("api_version should not be null or empty."); + } + + String azureEndpoint = descriptor.getArgument("azure_endpoint"); + if (azureEndpoint == null || azureEndpoint.isBlank()) { + throw new IllegalArgumentException("azure_endpoint should not be null or empty."); + } + + Integer timeoutSeconds = descriptor.getArgument("timeout"); Review Comment: +1 ########## integrations/chat-models/openai/src/main/java/org/apache/flink/agents/integrations/chatmodels/openai/AzureOpenAIChatModelConnection.java: ########## @@ -0,0 +1,276 @@ +/* + * 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.agents.integrations.chatmodels.openai; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.openai.azure.AzureOpenAIServiceVersion; +import com.openai.azure.AzureUrlPathMode; +import com.openai.azure.credential.AzureApiKeyCredential; +import com.openai.client.OpenAIClient; +import com.openai.client.okhttp.OpenAIOkHttpClient; +import com.openai.core.JsonValue; +import com.openai.models.ChatModel; +import com.openai.models.FunctionDefinition; +import com.openai.models.FunctionParameters; +import com.openai.models.chat.completions.ChatCompletion; +import com.openai.models.chat.completions.ChatCompletionCreateParams; +import com.openai.models.chat.completions.ChatCompletionFunctionTool; +import com.openai.models.chat.completions.ChatCompletionTool; +import org.apache.flink.agents.api.chat.messages.ChatMessage; +import org.apache.flink.agents.api.chat.model.BaseChatModelConnection; +import org.apache.flink.agents.api.resource.ResourceContext; +import org.apache.flink.agents.api.resource.ResourceDescriptor; +import org.apache.flink.agents.api.tools.Tool; +import org.apache.flink.agents.api.tools.ToolMetadata; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Chat model integration for Azure OpenAI Service. Built on the openai-java SDK using its built-in + * Azure support ({@link AzureOpenAIServiceVersion}, {@link AzureApiKeyCredential}). + * + * <p>Required connection arguments: + * + * <ul> + * <li><b>api_key</b>: Azure OpenAI API key + * <li><b>api_version</b>: Azure OpenAI REST API version (e.g., {@code "2024-02-01"}) + * <li><b>azure_endpoint</b>: base URL for the Azure OpenAI deployment — either a direct Azure + * resource (e.g., {@code "https://your-resource.openai.azure.com"}) or a proxy/gateway URL + * that fronts an Azure OpenAI service. Custom gateway hostnames also require setting {@code + * azure_url_path_mode} below. + * </ul> + * + * <p>Optional connection arguments: + * + * <ul> + * <li><b>timeout</b> (Number): seconds before an API call times out (default 60) + * <li><b>max_retries</b> (Number): retry attempts on failure (default 3) + * <li><b>azure_url_path_mode</b> (String): one of {@code "AUTO"}, {@code "LEGACY"}, or {@code + * "UNIFIED"} (default {@code "AUTO"}). Controls how the SDK constructs Azure OpenAI request + * URLs. In {@code AUTO} mode the SDK only treats the endpoint as Azure when its hostname + * matches a known suffix (e.g. {@code .openai.azure.com}); custom gateways that proxy Azure + * OpenAI need {@code LEGACY} to force the {@code /openai/deployments/{model}} path. + * </ul> + * + * <p>Example usage: + * + * <pre>{@code + * @ChatModelConnection + * public static ResourceDescriptor azureOpenAIConnection() { + * return ResourceDescriptor.Builder.newBuilder( + * AzureOpenAIChatModelConnection.class.getName()) + * .addInitialArgument("api_key", System.getenv("AZURE_OPENAI_API_KEY")) + * .addInitialArgument("api_version", "2024-02-01") + * .addInitialArgument("azure_endpoint", "https://my-resource.openai.azure.com") + * .build(); + * } + * }</pre> + */ +public class AzureOpenAIChatModelConnection extends BaseChatModelConnection { + + private static final ObjectMapper mapper = new ObjectMapper(); + + private final OpenAIClient client; + + public AzureOpenAIChatModelConnection( + ResourceDescriptor descriptor, ResourceContext resourceContext) { + super(descriptor, resourceContext); + + String apiKey = descriptor.getArgument("api_key"); + if (apiKey == null || apiKey.isBlank()) { + throw new IllegalArgumentException("api_key should not be null or empty."); + } + + String apiVersion = descriptor.getArgument("api_version"); + if (apiVersion == null || apiVersion.isBlank()) { + throw new IllegalArgumentException("api_version should not be null or empty."); + } + + String azureEndpoint = descriptor.getArgument("azure_endpoint"); + if (azureEndpoint == null || azureEndpoint.isBlank()) { + throw new IllegalArgumentException("azure_endpoint should not be null or empty."); + } + + Integer timeoutSeconds = descriptor.getArgument("timeout"); + if (timeoutSeconds == null) { + timeoutSeconds = 60; + } + + Integer maxRetries = descriptor.getArgument("max_retries"); + if (maxRetries == null) { + maxRetries = 3; + } + + OpenAIOkHttpClient.Builder clientBuilder = + OpenAIOkHttpClient.builder() + .baseUrl(azureEndpoint) + .credential(AzureApiKeyCredential.create(apiKey)) + .azureServiceVersion(AzureOpenAIServiceVersion.fromString(apiVersion)) + .timeout(Duration.ofSeconds(timeoutSeconds)) + .maxRetries(maxRetries); + + String azureUrlPathMode = descriptor.getArgument("azure_url_path_mode"); + if (azureUrlPathMode != null && !azureUrlPathMode.isBlank()) { + try { + clientBuilder.azureUrlPathMode( + AzureUrlPathMode.valueOf(azureUrlPathMode.trim().toUpperCase())); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException( + "azure_url_path_mode must be one of AUTO, LEGACY, or UNIFIED; got: " + + azureUrlPathMode, + e); + } + } + + this.client = clientBuilder.build(); + } + + @Override + public ChatMessage chat( + List<ChatMessage> messages, List<Tool> tools, Map<String, Object> arguments) { + try { + Map<String, Object> mutableArgs = + arguments != null ? new HashMap<>(arguments) : new HashMap<>(); + + String azureDeployment = (String) mutableArgs.remove("model"); + if (azureDeployment == null || azureDeployment.isBlank()) { + throw new IllegalArgumentException("model is required for Azure OpenAI API calls"); + } + String modelOfAzureDeployment = + (String) mutableArgs.remove("model_of_azure_deployment"); + + ChatCompletionCreateParams.Builder builder = + ChatCompletionCreateParams.builder() + .model(ChatModel.of(azureDeployment)) + .messages(OpenAIChatCompletionsUtils.convertToOpenAIMessages(messages)); + + if (tools != null && !tools.isEmpty()) { + builder.tools(convertTools(tools)); + } + + Object temperature = mutableArgs.remove("temperature"); + if (temperature instanceof Number) { + builder.temperature(((Number) temperature).doubleValue()); + } + + Object maxTokens = mutableArgs.remove("max_tokens"); + if (maxTokens instanceof Number) { + builder.maxCompletionTokens(((Number) maxTokens).longValue()); + } + + Object logprobs = mutableArgs.remove("logprobs"); + if (Boolean.TRUE.equals(logprobs)) { + builder.logprobs(true); + } + + // Pass-through: AzureOpenAIChatModelSetup flattens additional_kwargs into the top + // level, so any remaining entries here are user-provided extras that should flow + // through to the OpenAI request body. + for (Map.Entry<String, Object> entry : mutableArgs.entrySet()) { + builder.putAdditionalBodyProperty(entry.getKey(), toJsonValue(entry.getValue())); + } + + ChatCompletion completion = client.chat().completions().create(builder.build()); + + ChatMessage response = + OpenAIChatCompletionsUtils.convertFromOpenAIMessage( + completion.choices().get(0).message(), Map.of()); + + if (modelOfAzureDeployment != null + && !modelOfAzureDeployment.isBlank() + && completion.usage().isPresent()) { + recordTokenMetrics( Review Comment: In addition to the documentation, I think we can also print a warning log if we detect that `modelOfAzureDeployment` is not set during initializing the Setup. ########## integrations/chat-models/openai/src/main/java/org/apache/flink/agents/integrations/chatmodels/openai/OpenAIChatCompletionsUtils.java: ########## @@ -0,0 +1,233 @@ +/* + * 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.agents.integrations.chatmodels.openai; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.openai.core.JsonValue; +import com.openai.models.chat.completions.ChatCompletionAssistantMessageParam; +import com.openai.models.chat.completions.ChatCompletionMessage; +import com.openai.models.chat.completions.ChatCompletionMessageFunctionToolCall; +import com.openai.models.chat.completions.ChatCompletionMessageParam; +import com.openai.models.chat.completions.ChatCompletionMessageToolCall; +import com.openai.models.chat.completions.ChatCompletionSystemMessageParam; +import com.openai.models.chat.completions.ChatCompletionToolMessageParam; +import com.openai.models.chat.completions.ChatCompletionUserMessageParam; +import org.apache.flink.agents.api.chat.messages.ChatMessage; +import org.apache.flink.agents.api.chat.messages.MessageRole; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * Static helpers for converting between Flink Agents {@link ChatMessage} and OpenAI Chat + * Completions API message types. Restricted to message conversion (no tool-definition conversion — + * that stays per-connection). + * + * <p>Used by both {@code OpenAICompletionsConnection} (OpenAI / OpenAI-compatible providers) and + * {@code AzureOpenAIChatModelConnection} (Azure OpenAI). Both rely on the same openai-java SDK + * message types. + */ +final class OpenAIChatCompletionsUtils { + + private OpenAIChatCompletionsUtils() {} + + private static final ObjectMapper mapper = new ObjectMapper(); + private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {}; + + /** Convert a list of Flink Agents ChatMessages to OpenAI ChatCompletionMessageParams. */ + public static List<ChatCompletionMessageParam> convertToOpenAIMessages( + List<ChatMessage> messages) { + return messages.stream() + .map(OpenAIChatCompletionsUtils::convertToOpenAIMessage) + .collect(Collectors.toList()); + } + + /** Convert a single Flink Agents ChatMessage to an OpenAI ChatCompletionMessageParam. */ + public static ChatCompletionMessageParam convertToOpenAIMessage(ChatMessage message) { + MessageRole role = message.getRole(); + String content = Optional.ofNullable(message.getContent()).orElse(""); + + switch (role) { + case SYSTEM: + return ChatCompletionMessageParam.ofSystem( + ChatCompletionSystemMessageParam.builder().content(content).build()); + case USER: + return ChatCompletionMessageParam.ofUser( + ChatCompletionUserMessageParam.builder().content(content).build()); + case ASSISTANT: + ChatCompletionAssistantMessageParam.Builder assistantBuilder = + ChatCompletionAssistantMessageParam.builder(); + if (!content.isEmpty()) { + assistantBuilder.content(content); + } + List<Map<String, Object>> toolCalls = message.getToolCalls(); + if (toolCalls != null && !toolCalls.isEmpty()) { + assistantBuilder.toolCalls(convertAssistantToolCalls(toolCalls)); + } + Object refusal = message.getExtraArgs().get("refusal"); + if (refusal instanceof String) { + assistantBuilder.refusal((String) refusal); + } + return ChatCompletionMessageParam.ofAssistant(assistantBuilder.build()); + case TOOL: + ChatCompletionToolMessageParam.Builder toolBuilder = + ChatCompletionToolMessageParam.builder().content(content); + Object toolCallId = message.getExtraArgs().get("externalId"); + if (toolCallId == null) { + throw new IllegalArgumentException( + "Tool message must have an externalId in extraArgs."); + } + toolBuilder.toolCallId(toolCallId.toString()); + return ChatCompletionMessageParam.ofTool(toolBuilder.build()); + default: + throw new IllegalArgumentException("Unsupported role: " + role); + } + } + + /** + * Convert an OpenAI {@link ChatCompletionMessage} to a Flink Agents {@link ChatMessage}. + * Caller-provided {@code extraArgs} are copied into the returned ChatMessage's own extraArgs + * map (the caller's input is treated as read-only; {@code Map.of()} is safe). Additionally, + * {@code message.refusal()} is written as {@code extraArgs["refusal"]} when present, preserving + * prior Java behavior. + */ + public static ChatMessage convertFromOpenAIMessage( Review Comment: +1 ########## integrations/chat-models/openai/src/main/java/org/apache/flink/agents/integrations/chatmodels/openai/AzureOpenAIChatModelConnection.java: ########## @@ -0,0 +1,279 @@ +/* + * 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.agents.integrations.chatmodels.openai; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.openai.azure.AzureOpenAIServiceVersion; +import com.openai.azure.AzureUrlPathMode; +import com.openai.azure.credential.AzureApiKeyCredential; +import com.openai.client.OpenAIClient; +import com.openai.client.okhttp.OpenAIOkHttpClient; +import com.openai.core.JsonValue; +import com.openai.models.ChatModel; +import com.openai.models.FunctionDefinition; +import com.openai.models.FunctionParameters; +import com.openai.models.chat.completions.ChatCompletion; +import com.openai.models.chat.completions.ChatCompletionCreateParams; +import com.openai.models.chat.completions.ChatCompletionFunctionTool; +import com.openai.models.chat.completions.ChatCompletionTool; +import org.apache.flink.agents.api.chat.messages.ChatMessage; +import org.apache.flink.agents.api.chat.model.BaseChatModelConnection; +import org.apache.flink.agents.api.resource.ResourceContext; +import org.apache.flink.agents.api.resource.ResourceDescriptor; +import org.apache.flink.agents.api.tools.Tool; +import org.apache.flink.agents.api.tools.ToolMetadata; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Chat model integration for Azure OpenAI Service. Built on the openai-java SDK using its built-in + * Azure support ({@link AzureOpenAIServiceVersion}, {@link AzureApiKeyCredential}). + * + * <p>Required connection arguments: + * + * <ul> + * <li><b>api_key</b>: Azure OpenAI API key + * <li><b>api_version</b>: Azure OpenAI REST API version (e.g., {@code "2024-02-01"}) + * <li><b>azure_endpoint</b>: base URL for the Azure OpenAI deployment — either a direct Azure + * resource (e.g., {@code "https://your-resource.openai.azure.com"}) or a proxy/gateway URL + * that fronts an Azure OpenAI service. Custom gateway hostnames also require setting {@code + * azure_url_path_mode} below. + * </ul> + * + * <p>Optional connection arguments: + * + * <ul> + * <li><b>timeout</b> (Number): seconds before an API call times out; must be greater than 0, + * otherwise ignored (SDK default applies) + * <li><b>max_retries</b> (Number): retry attempts on failure; must be non-negative, otherwise + * ignored (SDK default applies) + * <li><b>azure_url_path_mode</b> (String): one of {@code "AUTO"}, {@code "LEGACY"}, or {@code + * "UNIFIED"} (default {@code "AUTO"}). Controls how the SDK constructs Azure OpenAI request + * URLs. In {@code AUTO} mode the SDK only treats the endpoint as Azure when its hostname + * matches a known suffix (e.g. {@code .openai.azure.com}); custom gateways that proxy Azure + * OpenAI need {@code LEGACY} to force the {@code /openai/deployments/{model}} path. + * </ul> + * + * <p>Example usage: + * + * <pre>{@code + * @ChatModelConnection + * public static ResourceDescriptor azureOpenAIConnection() { + * return ResourceDescriptor.Builder.newBuilder( + * AzureOpenAIChatModelConnection.class.getName()) + * .addInitialArgument("api_key", System.getenv("AZURE_OPENAI_API_KEY")) + * .addInitialArgument("api_version", "2024-02-01") + * .addInitialArgument("azure_endpoint", "https://my-resource.openai.azure.com") + * .build(); + * } + * }</pre> + */ +public class AzureOpenAIChatModelConnection extends BaseChatModelConnection { + + private static final ObjectMapper mapper = new ObjectMapper(); + + private final OpenAIClient client; + + public AzureOpenAIChatModelConnection( + ResourceDescriptor descriptor, ResourceContext resourceContext) { + super(descriptor, resourceContext); + + String apiKey = descriptor.getArgument("api_key"); + if (apiKey == null || apiKey.isBlank()) { + throw new IllegalArgumentException("api_key should not be null or empty."); + } + + String apiVersion = descriptor.getArgument("api_version"); + if (apiVersion == null || apiVersion.isBlank()) { + throw new IllegalArgumentException("api_version should not be null or empty."); + } + + String azureEndpoint = descriptor.getArgument("azure_endpoint"); + if (azureEndpoint == null || azureEndpoint.isBlank()) { + throw new IllegalArgumentException("azure_endpoint should not be null or empty."); + } + + OpenAIOkHttpClient.Builder clientBuilder = + OpenAIOkHttpClient.builder() + .baseUrl(azureEndpoint) + .credential(AzureApiKeyCredential.create(apiKey)) + .azureServiceVersion(AzureOpenAIServiceVersion.fromString(apiVersion)); + + Integer timeoutSeconds = descriptor.getArgument("timeout"); + if (timeoutSeconds != null && timeoutSeconds > 0) { + clientBuilder.timeout(Duration.ofSeconds(timeoutSeconds)); + } + + Integer maxRetries = descriptor.getArgument("max_retries"); + if (maxRetries != null && maxRetries >= 0) { + clientBuilder.maxRetries(maxRetries); + } + + String azureUrlPathMode = descriptor.getArgument("azure_url_path_mode"); + if (azureUrlPathMode != null && !azureUrlPathMode.isBlank()) { + try { + clientBuilder.azureUrlPathMode( + AzureUrlPathMode.valueOf(azureUrlPathMode.trim().toUpperCase())); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException( + "azure_url_path_mode must be one of AUTO, LEGACY, or UNIFIED; got: " + + azureUrlPathMode, + e); + } + } + + this.client = clientBuilder.build(); + } + + @Override + public ChatMessage chat( + List<ChatMessage> messages, List<Tool> tools, Map<String, Object> arguments) { + try { + Map<String, Object> mutableArgs = + arguments != null ? new HashMap<>(arguments) : new HashMap<>(); + + String azureDeployment = (String) mutableArgs.remove("model"); + if (azureDeployment == null || azureDeployment.isBlank()) { + throw new IllegalArgumentException("model is required for Azure OpenAI API calls"); + } + String modelOfAzureDeployment = + (String) mutableArgs.remove("model_of_azure_deployment"); + + ChatCompletionCreateParams.Builder builder = + ChatCompletionCreateParams.builder() + .model(ChatModel.of(azureDeployment)) + .messages(OpenAIChatCompletionsUtils.convertToOpenAIMessages(messages)); + + if (tools != null && !tools.isEmpty()) { + builder.tools(convertTools(tools)); + } + + Object temperature = mutableArgs.remove("temperature"); + if (temperature instanceof Number) { + builder.temperature(((Number) temperature).doubleValue()); + } + + Object maxTokens = mutableArgs.remove("max_tokens"); + if (maxTokens instanceof Number) { + builder.maxCompletionTokens(((Number) maxTokens).longValue()); + } + + Object logprobs = mutableArgs.remove("logprobs"); + if (Boolean.TRUE.equals(logprobs)) { + builder.logprobs(true); + } + + @SuppressWarnings("unchecked") + Map<String, Object> additionalKwargs = + (Map<String, Object>) mutableArgs.remove("additional_kwargs"); + if (additionalKwargs != null) { + for (Map.Entry<String, Object> entry : additionalKwargs.entrySet()) { + builder.putAdditionalBodyProperty( Review Comment: +1 for the proposed solution, and for also fixing this in the python side. -- 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]
