-
Notifications
You must be signed in to change notification settings - Fork 149
[integration][openai] Align default parameters for openai chat model between java and python #883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fca1fcb
566b80a
f406363
fb342a3
0a0ded7
bd49d9b
f54f58f
88f03da
82eb320
8e6609b
e8f894a
abd88f5
2d50791
e2e7786
1f97bc1
cf9be98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * A chat model integration for the OpenAI Chat Completions service using the official Java SDK. | ||
|
|
@@ -53,8 +54,8 @@ | |
| * <li><b>api_key</b> (required): OpenAI API key | ||
| * <li><b>api_base_url</b> (optional): Base URL for OpenAI API (defaults to | ||
| * https://api.openai.com/v1) | ||
| * <li><b>timeout</b> (optional): Timeout in seconds for API requests | ||
| * <li><b>max_retries</b> (optional): Maximum number of retry attempts (default: 2) | ||
| * <li><b>timeout</b> (optional): Timeout in seconds for API requests (default: 60) | ||
| * <li><b>max_retries</b> (optional): Maximum number of retry attempts (default: 3) | ||
| * <li><b>default_headers</b> (optional): Map of default headers to include in all requests | ||
| * <li><b>model</b> (optional): Default model to use if not specified in setup | ||
| * </ul> | ||
|
|
@@ -81,6 +82,8 @@ public class OpenAICompletionsConnection extends BaseChatModelConnection { | |
| private static final ObjectMapper mapper = new ObjectMapper(); | ||
| private final OpenAIClient client; | ||
| private final String defaultModel; | ||
| private final int timeoutSeconds; | ||
| private final int maxRetries; | ||
|
|
||
| public OpenAICompletionsConnection( | ||
| ResourceDescriptor descriptor, ResourceContext resourceContext) { | ||
|
|
@@ -98,15 +101,23 @@ public OpenAICompletionsConnection( | |
| builder.baseUrl(apiBaseUrl); | ||
| } | ||
|
|
||
| Integer timeoutSeconds = descriptor.getArgument("timeout"); | ||
| if (timeoutSeconds != null && timeoutSeconds > 0) { | ||
| builder.timeout(Duration.ofSeconds(timeoutSeconds)); | ||
| this.timeoutSeconds = | ||
| Optional.ofNullable(descriptor.<Number>getArgument("timeout")) | ||
| .map(Number::intValue) | ||
| .orElse(OpenAIChatCompletionsUtils.DEFAULT_TIMEOUT_SECONDS); | ||
| if (this.timeoutSeconds < 0) { | ||
| throw new IllegalArgumentException("timeout must be >= 0, got: " + this.timeoutSeconds); | ||
| } | ||
|
|
||
| Integer maxRetries = descriptor.getArgument("max_retries"); | ||
| if (maxRetries != null && maxRetries >= 0) { | ||
| builder.maxRetries(maxRetries); | ||
| builder.timeout(Duration.ofSeconds(this.timeoutSeconds)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The same pattern is also present in |
||
|
|
||
| this.maxRetries = | ||
| Optional.ofNullable(descriptor.<Number>getArgument("max_retries")) | ||
| .map(Number::intValue) | ||
| .orElse(OpenAIChatCompletionsUtils.DEFAULT_MAX_RETRIES); | ||
| if (this.maxRetries < 0) { | ||
| throw new IllegalArgumentException("max_retries must be >= 0, got: " + this.maxRetries); | ||
| } | ||
| builder.maxRetries(this.maxRetries); | ||
|
|
||
| Map<String, String> defaultHeaders = descriptor.getArgument("default_headers"); | ||
| if (defaultHeaders != null && !defaultHeaders.isEmpty()) { | ||
|
|
@@ -119,6 +130,16 @@ public OpenAICompletionsConnection( | |
| this.client = builder.build(); | ||
| } | ||
|
|
||
| // visible for testing | ||
| int getTimeoutSeconds() { | ||
| return timeoutSeconds; | ||
| } | ||
|
|
||
| // visible for testing | ||
| int getMaxRetries() { | ||
| return maxRetries; | ||
| } | ||
|
|
||
| @Override | ||
| public ChatMessage chat( | ||
| List<ChatMessage> messages, List<Tool> tools, Map<String, Object> modelParams) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
Number.intValue()before validation silently truncates or overflows the input. For example,-0.5becomes0and bypasses the negative check, while fractional timeouts lose precision and fractional retry counts differ from Python validation. Please validate the original value first, preserve fractional timeout values, and require an exact bounded integer formax_retries.The same pattern is also present in AzureOpenAIChatModelConnection and OpenAIResponsesModelConnection.