|
30 | 30 | import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
31 | 31 | import com.fasterxml.jackson.annotation.JsonProperty;
|
32 | 32 |
|
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
33 | 36 | import org.springframework.ai.model.ModelOptionsUtils;
|
34 | 37 | import org.springframework.ai.model.tool.ToolCallingChatOptions;
|
35 | 38 | import org.springframework.ai.openai.api.OpenAiApi;
|
|
55 | 58 | @JsonInclude(Include.NON_NULL)
|
56 | 59 | public class OpenAiChatOptions implements ToolCallingChatOptions {
|
57 | 60 |
|
| 61 | + private static final Logger logger = LoggerFactory.getLogger(OpenAiChatOptions.class); |
| 62 | + |
58 | 63 | // @formatter:off
|
59 | 64 | /**
|
60 | 65 | * ID of the model to use.
|
@@ -84,13 +89,31 @@ public class OpenAiChatOptions implements ToolCallingChatOptions {
|
84 | 89 | */
|
85 | 90 | private @JsonProperty("top_logprobs") Integer topLogprobs;
|
86 | 91 | /**
|
87 |
| - * The maximum number of tokens to generate in the chat completion. The total length of input |
88 |
| - * tokens and generated tokens is limited by the model's context length. |
| 92 | + * The maximum number of tokens to generate in the chat completion. |
| 93 | + * The total length of input tokens and generated tokens is limited by the model's context length. |
| 94 | + * |
| 95 | + * <p><strong>Model-specific usage:</strong></p> |
| 96 | + * <ul> |
| 97 | + * <li><strong>Use for non-reasoning models</strong> (e.g., gpt-4o, gpt-3.5-turbo)</li> |
| 98 | + * <li><strong>Cannot be used with reasoning models</strong> (e.g., o1, o3, o4-mini series)</li> |
| 99 | + * </ul> |
| 100 | + * |
| 101 | + * <p><strong>Mutual exclusivity:</strong> This parameter cannot be used together with |
| 102 | + * {@link #maxCompletionTokens}. Setting both will result in an API error.</p> |
89 | 103 | */
|
90 | 104 | private @JsonProperty("max_tokens") Integer maxTokens;
|
91 | 105 | /**
|
92 | 106 | * An upper bound for the number of tokens that can be generated for a completion,
|
93 | 107 | * including visible output tokens and reasoning tokens.
|
| 108 | + * |
| 109 | + * <p><strong>Model-specific usage:</strong></p> |
| 110 | + * <ul> |
| 111 | + * <li><strong>Required for reasoning models</strong> (e.g., o1, o3, o4-mini series)</li> |
| 112 | + * <li><strong>Cannot be used with non-reasoning models</strong> (e.g., gpt-4o, gpt-3.5-turbo)</li> |
| 113 | + * </ul> |
| 114 | + * |
| 115 | + * <p><strong>Mutual exclusivity:</strong> This parameter cannot be used together with |
| 116 | + * {@link #maxTokens}. Setting both will result in an API error.</p> |
94 | 117 | */
|
95 | 118 | private @JsonProperty("max_completion_tokens") Integer maxCompletionTokens;
|
96 | 119 | /**
|
@@ -678,12 +701,72 @@ public Builder topLogprobs(Integer topLogprobs) {
|
678 | 701 | return this;
|
679 | 702 | }
|
680 | 703 |
|
| 704 | + /** |
| 705 | + * Sets the maximum number of tokens to generate in the chat completion. The total |
| 706 | + * length of input tokens and generated tokens is limited by the model's context |
| 707 | + * length. |
| 708 | + * |
| 709 | + * <p> |
| 710 | + * <strong>Model-specific usage:</strong> |
| 711 | + * </p> |
| 712 | + * <ul> |
| 713 | + * <li><strong>Use for non-reasoning models</strong> (e.g., gpt-4o, |
| 714 | + * gpt-3.5-turbo)</li> |
| 715 | + * <li><strong>Cannot be used with reasoning models</strong> (e.g., o1, o3, |
| 716 | + * o4-mini series)</li> |
| 717 | + * </ul> |
| 718 | + * |
| 719 | + * <p> |
| 720 | + * <strong>Mutual exclusivity:</strong> This parameter cannot be used together |
| 721 | + * with {@link #maxCompletionTokens(Integer)}. If both are set, the last one set |
| 722 | + * will be used and the other will be cleared with a warning. |
| 723 | + * </p> |
| 724 | + * @param maxTokens the maximum number of tokens to generate, or null to unset |
| 725 | + * @return this builder instance |
| 726 | + */ |
681 | 727 | public Builder maxTokens(Integer maxTokens) {
|
| 728 | + if (maxTokens != null && this.options.maxCompletionTokens != null) { |
| 729 | + logger |
| 730 | + .warn("Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " |
| 731 | + + "The previously set maxCompletionTokens ({}) will be cleared and maxTokens ({}) will be used.", |
| 732 | + this.options.maxCompletionTokens, maxTokens); |
| 733 | + this.options.maxCompletionTokens = null; |
| 734 | + } |
682 | 735 | this.options.maxTokens = maxTokens;
|
683 | 736 | return this;
|
684 | 737 | }
|
685 | 738 |
|
| 739 | + /** |
| 740 | + * Sets an upper bound for the number of tokens that can be generated for a |
| 741 | + * completion, including visible output tokens and reasoning tokens. |
| 742 | + * |
| 743 | + * <p> |
| 744 | + * <strong>Model-specific usage:</strong> |
| 745 | + * </p> |
| 746 | + * <ul> |
| 747 | + * <li><strong>Required for reasoning models</strong> (e.g., o1, o3, o4-mini |
| 748 | + * series)</li> |
| 749 | + * <li><strong>Cannot be used with non-reasoning models</strong> (e.g., gpt-4o, |
| 750 | + * gpt-3.5-turbo)</li> |
| 751 | + * </ul> |
| 752 | + * |
| 753 | + * <p> |
| 754 | + * <strong>Mutual exclusivity:</strong> This parameter cannot be used together |
| 755 | + * with {@link #maxTokens(Integer)}. If both are set, the last one set will be |
| 756 | + * used and the other will be cleared with a warning. |
| 757 | + * </p> |
| 758 | + * @param maxCompletionTokens the maximum number of completion tokens to generate, |
| 759 | + * or null to unset |
| 760 | + * @return this builder instance |
| 761 | + */ |
686 | 762 | public Builder maxCompletionTokens(Integer maxCompletionTokens) {
|
| 763 | + if (maxCompletionTokens != null && this.options.maxTokens != null) { |
| 764 | + logger |
| 765 | + .warn("Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " |
| 766 | + + "The previously set maxTokens ({}) will be cleared and maxCompletionTokens ({}) will be used.", |
| 767 | + this.options.maxTokens, maxCompletionTokens); |
| 768 | + this.options.maxTokens = null; |
| 769 | + } |
687 | 770 | this.options.maxCompletionTokens = maxCompletionTokens;
|
688 | 771 | return this;
|
689 | 772 | }
|
|
0 commit comments