View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.llm;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  /**
24   * Request object for LLM chat completion.
25   *
26   * @author FessProject
27   */
28  public class LlmChatRequest {
29  
30      private List<LlmMessage> messages = new ArrayList<>();
31      private String model;
32      private Integer maxTokens;
33      private Double temperature;
34      private boolean stream;
35      private Integer thinkingBudget;
36      private Map<String, String> extraParams;
37  
38      /**
39       * Default constructor.
40       */
41      public LlmChatRequest() {
42      }
43  
44      /**
45       * Adds a message to the request.
46       *
47       * @param message the message to add
48       * @return this request for method chaining
49       */
50      public LlmChatRequest addMessage(final LlmMessage message) {
51          messages.add(message);
52          return this;
53      }
54  
55      /**
56       * Adds a system message to the request.
57       *
58       * @param content the message content
59       * @return this request for method chaining
60       */
61      public LlmChatRequest addSystemMessage(final String content) {
62          messages.add(LlmMessage.system(content));
63          return this;
64      }
65  
66      /**
67       * Adds a user message to the request.
68       *
69       * @param content the message content
70       * @return this request for method chaining
71       */
72      public LlmChatRequest addUserMessage(final String content) {
73          messages.add(LlmMessage.user(content));
74          return this;
75      }
76  
77      /**
78       * Adds an assistant message to the request.
79       *
80       * @param content the message content
81       * @return this request for method chaining
82       */
83      public LlmChatRequest addAssistantMessage(final String content) {
84          messages.add(LlmMessage.assistant(content));
85          return this;
86      }
87  
88      /**
89       * Gets the messages in this request.
90       *
91       * @return the list of messages
92       */
93      public List<LlmMessage> getMessages() {
94          return messages;
95      }
96  
97      /**
98       * Sets the messages in this request.
99       *
100      * @param messages the list of messages
101      */
102     public void setMessages(final List<LlmMessage> messages) {
103         this.messages = messages;
104     }
105 
106     /**
107      * Gets the model name.
108      *
109      * @return the model name
110      */
111     public String getModel() {
112         return model;
113     }
114 
115     /**
116      * Sets the model name.
117      *
118      * @param model the model name
119      * @return this request for method chaining
120      */
121     public LlmChatRequest setModel(final String model) {
122         this.model = model;
123         return this;
124     }
125 
126     /**
127      * Gets the maximum tokens for the response.
128      *
129      * @return the maximum tokens
130      */
131     public Integer getMaxTokens() {
132         return maxTokens;
133     }
134 
135     /**
136      * Sets the maximum tokens for the response.
137      *
138      * @param maxTokens the maximum tokens
139      * @return this request for method chaining
140      */
141     public LlmChatRequest setMaxTokens(final Integer maxTokens) {
142         this.maxTokens = maxTokens;
143         return this;
144     }
145 
146     /**
147      * Gets the temperature parameter.
148      *
149      * @return the temperature
150      */
151     public Double getTemperature() {
152         return temperature;
153     }
154 
155     /**
156      * Sets the temperature parameter.
157      *
158      * @param temperature the temperature
159      * @return this request for method chaining
160      */
161     public LlmChatRequest setTemperature(final Double temperature) {
162         this.temperature = temperature;
163         return this;
164     }
165 
166     /**
167      * Checks if streaming is enabled.
168      *
169      * @return true if streaming is enabled
170      */
171     public boolean isStream() {
172         return stream;
173     }
174 
175     /**
176      * Sets whether streaming is enabled.
177      *
178      * @param stream true to enable streaming
179      * @return this request for method chaining
180      */
181     public LlmChatRequest setStream(final boolean stream) {
182         this.stream = stream;
183         return this;
184     }
185 
186     /**
187      * Gets the thinking budget for models that support thinking tokens.
188      * {@code null} means use the model default, {@code 0} disables thinking.
189      *
190      * @return the thinking budget
191      */
192     public Integer getThinkingBudget() {
193         return thinkingBudget;
194     }
195 
196     /**
197      * Sets the thinking budget for models that support thinking tokens.
198      * {@code null} means use the model default, {@code 0} disables thinking.
199      *
200      * @param thinkingBudget the thinking budget
201      * @return this request for method chaining
202      */
203     public LlmChatRequest setThinkingBudget(final Integer thinkingBudget) {
204         this.thinkingBudget = thinkingBudget;
205         return this;
206     }
207 
208     /**
209      * Gets the extra parameters for provider-specific settings.
210      *
211      * @return the extra parameters map, or {@code null} if not set
212      */
213     public Map<String, String> getExtraParams() {
214         return extraParams;
215     }
216 
217     /**
218      * Sets the extra parameters for provider-specific settings.
219      *
220      * @param extraParams the extra parameters map
221      * @return this request for method chaining
222      */
223     public LlmChatRequest setExtraParams(final Map<String, String> extraParams) {
224         this.extraParams = extraParams;
225         return this;
226     }
227 
228     /**
229      * Adds a single extra parameter for provider-specific settings.
230      *
231      * @param key the parameter key
232      * @param value the parameter value
233      * @return this request for method chaining
234      */
235     public LlmChatRequest putExtraParam(final String key, final String value) {
236         if (extraParams == null) {
237             extraParams = new HashMap<>();
238         }
239         extraParams.put(key, value);
240         return this;
241     }
242 
243     /**
244      * Gets a single extra parameter value.
245      *
246      * @param key the parameter key
247      * @return the parameter value, or {@code null} if not set
248      */
249     public String getExtraParam(final String key) {
250         return extraParams != null ? extraParams.get(key) : null;
251     }
252 }