alnzng commented on code in PR #695: URL: https://github.com/apache/flink-agents/pull/695#discussion_r3279366901
########## integrations/chat-models/openai/src/main/java/org/apache/flink/agents/integrations/chatmodels/openai/AzureOpenAIChatModelSetup.java: ########## @@ -0,0 +1,112 @@ +/* + * 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 org.apache.flink.agents.api.chat.model.BaseChatModelSetup; +import org.apache.flink.agents.api.resource.ResourceContext; +import org.apache.flink.agents.api.resource.ResourceDescriptor; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +/** + * Setup for Azure OpenAI Chat Completions. + * + * <p>{@code model} (inherited from {@link BaseChatModelSetup}) is the Azure deployment name, not + * the underlying OpenAI model name. The underlying model name can be supplied via {@code + * model_of_azure_deployment} and is used solely for token-metrics tracking. + * + * <p>Example usage: + * + * <pre>{@code + * @ChatModelSetup + * public static ResourceDescriptor azureOpenAIModel() { + * return ResourceDescriptor.Builder.newBuilder(AzureOpenAIChatModelSetup.class.getName()) + * .addInitialArgument("connection", "myAzureOpenAIConnection") + * .addInitialArgument("model", "my-gpt4o-deployment") + * .addInitialArgument("model_of_azure_deployment", "gpt-4o") + * .addInitialArgument("temperature", 0.3d) + * .addInitialArgument("max_tokens", 500) + * .build(); + * } + * }</pre> + */ +public class AzureOpenAIChatModelSetup extends BaseChatModelSetup { + + private final String modelOfAzureDeployment; + private final Double temperature; + private final Integer maxTokens; + private final Boolean logprobs; + private final Map<String, Object> additionalKwargs; + + public AzureOpenAIChatModelSetup( + ResourceDescriptor descriptor, ResourceContext resourceContext) { + super(descriptor, resourceContext); + + this.modelOfAzureDeployment = descriptor.getArgument("model_of_azure_deployment"); + + this.temperature = + Optional.ofNullable(descriptor.<Number>getArgument("temperature")) + .map(Number::doubleValue) + .orElse(null); + if (this.temperature != null && (this.temperature < 0.0 || this.temperature > 2.0)) { + throw new IllegalArgumentException("temperature must be between 0.0 and 2.0"); + } + + this.maxTokens = + Optional.ofNullable(descriptor.<Number>getArgument("max_tokens")) + .map(Number::intValue) + .orElse(null); + if (this.maxTokens != null && this.maxTokens <= 0) { + throw new IllegalArgumentException("max_tokens must be greater than 0"); + } + + this.logprobs = + Optional.ofNullable(descriptor.<Boolean>getArgument("logprobs")).orElse(false); + + Map<String, Object> additional = + Optional.ofNullable( + descriptor.<Map<String, Object>>getArgument("additional_kwargs")) + .map(HashMap::new) + .orElseGet(HashMap::new); + this.additionalKwargs = additional; + } + + @Override + public Map<String, Object> getParameters() { + Map<String, Object> params = new HashMap<>(); + if (model != null) { + params.put("model", model); + } + if (modelOfAzureDeployment != null) { + params.put("model_of_azure_deployment", modelOfAzureDeployment); + } + params.put("logprobs", logprobs); + if (temperature != null) { + params.put("temperature", temperature); + } + if (maxTokens != null) { + params.put("max_tokens", maxTokens); + } + if (additionalKwargs != null && !additionalKwargs.isEmpty()) { + params.putAll(additionalKwargs); Review Comment: Updated and use a consistent logic from OpenAI / Anthropic model to handle `additionalKwargs `. -- 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]
