Skip to content

Commit 84dfb84

Browse files
committed
Mistral AI: Support fill-in-the-middle API for code completion models (langchain4j#1569)
1 parent 76c53e2 commit 84dfb84

File tree

2 files changed

+114
-43
lines changed

2 files changed

+114
-43
lines changed

langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/MistralAiFimModel.java

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,6 @@ public class MistralAiFimModel implements LanguageModel {
4040
private final List<String> stop;
4141
private final Integer maxRetries;
4242

43-
/**
44-
* Constructs a MistralAiFimModel with the specified parameters.
45-
*
46-
* @param baseUrl the base URL of the Mistral AI API. It uses the default value if not specified.
47-
* @param apiKey the API key for authentication
48-
* @param modelName the name of the Mistral AI model to use
49-
* @param temperature the temperature parameter for generating responses
50-
* @param maxTokens the maximum number of tokens to generate in a response
51-
* @param minTokens the minimum number of tokens to generate in a response
52-
* @param topP the top-p parameter for generating responses
53-
* @param randomSeed the random seed for generating responses
54-
* @param stop a list of tokens at which the model should stop generating tokens
55-
* @param timeout the timeout duration for API requests
56-
* <p>
57-
* The default value is 60 seconds.
58-
* @param logRequests a flag indicating whether to log API requests
59-
* @param logResponses a flag indicating whether to log API responses
60-
* @param maxRetries the maximum number of retries for API requests. It uses the default value 3 if not specified.
61-
*/
6243
public MistralAiFimModel(Builder builder) {
6344
this.client = MistralAiClient.builder()
6445
.baseUrl(getOrDefault(builder.baseUrl, "https://api.mistral.ai/v1"))
@@ -149,71 +130,127 @@ public static class Builder {
149130
public Builder() {
150131
}
151132

133+
/**
134+
* @param baseUrl the base URL of the Mistral AI API. It uses the default value if not specified.
135+
* @return builder
136+
*/
152137
public Builder baseUrl(String baseUrl) {
153138
this.baseUrl = baseUrl;
154139
return this;
155140
}
156141

142+
/**
143+
* @param apiKey the API key for authentication
144+
* @return builder
145+
*/
157146
public Builder apiKey(String apiKey) {
158147
this.apiKey = apiKey;
159148
return this;
160149
}
161150

151+
/**
152+
* @param modelName the name of the Mistral AI model to use
153+
* @return builder
154+
*/
162155
public Builder modelName(String modelName) {
163156
this.modelName = modelName;
164157
return this;
165158
}
166159

160+
/**
161+
* @param modelName the name of the Mistral AI model to use
162+
* @return builder
163+
*/
167164
public Builder modelName(MistralAiFimModelName modelName) {
168165
this.modelName = modelName.toString();
169166
return this;
170167
}
171168

169+
/**
170+
* @param temperature the temperature parameter for generating responses
171+
* @return builder
172+
*/
172173
public Builder temperature(Double temperature) {
173174
this.temperature = temperature;
174175
return this;
175176
}
176177

178+
/**
179+
* @param maxTokens the maximum number of tokens to generate in a response
180+
* @return builder
181+
*/
177182
public Builder maxTokens(Integer maxTokens) {
178183
this.maxTokens = maxTokens;
179184
return this;
180185
}
181186

187+
/**
188+
* @param minTokens the minimum number of tokens to generate in a response
189+
* @return builder
190+
*/
182191
public Builder minTokens(Integer minTokens) {
183192
this.minTokens = minTokens;
184193
return this;
185194
}
186195

196+
/**
197+
* @param topP the top-p parameter for generating responses
198+
* @return builder
199+
*/
187200
public Builder topP(Double topP) {
188201
this.topP = topP;
189202
return this;
190203
}
191204

205+
/**
206+
* @param randomSeed the random seed for generating responses
207+
* @return builder
208+
*/
192209
public Builder randomSeed(Integer randomSeed) {
193210
this.randomSeed = randomSeed;
194211
return this;
195212
}
196213

214+
/**
215+
* @param stop a list of tokens at which the model should stop generating tokens
216+
* @return builder
217+
*/
197218
public Builder stop(List<String> stop) {
198219
this.stop = stop;
199220
return this;
200221
}
201222

223+
/**
224+
* @param timeout the timeout duration for API requests. The default value is 60 seconds.
225+
* @return builder
226+
*/
202227
public Builder timeout(Duration timeout) {
203228
this.timeout = timeout;
204229
return this;
205230
}
206231

232+
/**
233+
* @param logRequests a flag indicating whether to log API requests
234+
* @return builder
235+
*/
207236
public Builder logRequests(Boolean logRequests) {
208237
this.logRequests = logRequests;
209238
return this;
210239
}
211240

241+
/**
242+
* @param logResponses a flag indicating whether to log API responses
243+
* @return builder
244+
*/
212245
public Builder logResponses(Boolean logResponses) {
213246
this.logResponses = logResponses;
214247
return this;
215248
}
216249

250+
/**
251+
* @param maxRetries the maximum number of retries for API requests. It uses the default value 2 if not specified.
252+
* @return builder
253+
*/
217254
public Builder maxRetries(Integer maxRetries) {
218255
this.maxRetries = maxRetries;
219256
return this;

langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/MistralAiStreamingFimModel.java

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,6 @@ public class MistralAiStreamingFimModel implements StreamingLanguageModel {
3434
private final Integer randomSeed;
3535
private final List<String> stop;
3636

37-
/**
38-
* Constructs a MistralAiStreamingFimModel with the specified parameters.
39-
*
40-
* @param baseUrl the base URL of the Mistral AI API. It uses the default value if not specified.
41-
* @param apiKey the API key for authentication
42-
* @param modelName the name of the Mistral AI model to use
43-
* @param temperature the temperature parameter for generating responses
44-
* @param maxTokens the maximum number of tokens to generate in a response
45-
* @param minTokens the minimum number of tokens to generate in a response
46-
* @param topP the top-p parameter for generating responses
47-
* @param randomSeed the random seed for generating responses
48-
* @param stop a list of tokens at which the model should stop generating tokens
49-
* @param logRequests a flag indicating whether to log API requests
50-
* @param logResponses a flag indicating whether to log API responses
51-
* @param timeout the timeout duration for API requests
52-
* <p>
53-
* The default value is 60 seconds.
54-
*/
5537
public MistralAiStreamingFimModel(Builder builder) {
5638
this.client = MistralAiClient.builder()
5739
.baseUrl(getOrDefault(builder.baseUrl, "https://api.mistral.ai/v1"))
@@ -129,78 +111,130 @@ public static class Builder {
129111
private Double topP;
130112
private Integer randomSeed;
131113
private List<String> stop;
114+
private Duration timeout;
132115
private Boolean logRequests;
133116
private Boolean logResponses;
134-
private Duration timeout;
135117

136118
public Builder() {
137119
}
138120

121+
/**
122+
* @param baseUrl the base URL of the Mistral AI API. It uses the default value if not specified.
123+
* @return builder
124+
*/
139125
public Builder baseUrl(String baseUrl) {
140126
this.baseUrl = baseUrl;
141127
return this;
142128
}
143129

130+
/**
131+
* @param apiKey the API key for authentication
132+
* @return builder
133+
*/
144134
public Builder apiKey(String apiKey) {
145135
this.apiKey = apiKey;
146136
return this;
147137
}
148138

139+
/**
140+
* @param modelName the name of the Mistral AI model to use
141+
* @return builder
142+
*/
149143
public Builder modelName(String modelName) {
150144
this.modelName = modelName;
151145
return this;
152146
}
153147

148+
/**
149+
* @param modelName the name of the Mistral AI model to use
150+
* @return builder
151+
*/
154152
public Builder modelName(MistralAiFimModelName modelName) {
155153
this.modelName = modelName.toString();
156154
return this;
157155
}
158156

157+
/**
158+
* @param temperature the temperature parameter for generating responses
159+
* @return builder
160+
*/
159161
public Builder temperature(Double temperature) {
160162
this.temperature = temperature;
161163
return this;
162164
}
163165

166+
/**
167+
* @param maxTokens the maximum number of tokens to generate in a response
168+
* @return builder
169+
*/
164170
public Builder maxTokens(Integer maxTokens) {
165171
this.maxTokens = maxTokens;
166172
return this;
167173
}
168174

175+
/**
176+
* @param minTokens the minimum number of tokens to generate in a response
177+
* @return builder
178+
*/
169179
public Builder minTokens(Integer minTokens) {
170180
this.minTokens = minTokens;
171181
return this;
172182
}
173183

184+
/**
185+
* @param topP the top-p parameter for generating responses
186+
* @return builder
187+
*/
174188
public Builder topP(Double topP) {
175189
this.topP = topP;
176190
return this;
177191
}
178192

193+
/**
194+
* @param randomSeed the random seed for generating responses
195+
* @return builder
196+
*/
179197
public Builder randomSeed(Integer randomSeed) {
180198
this.randomSeed = randomSeed;
181199
return this;
182200
}
183201

202+
/**
203+
* @param stop a list of tokens at which the model should stop generating tokens
204+
* @return builder
205+
*/
184206
public Builder stop(List<String> stop) {
185207
this.stop = stop;
186208
return this;
187209
}
188210

211+
/**
212+
* @param timeout the timeout duration for API requests. The default value is 60 seconds.
213+
* @return builder
214+
*/
215+
public Builder timeout(Duration timeout) {
216+
this.timeout = timeout;
217+
return this;
218+
}
219+
220+
/**
221+
* @param logRequests a flag indicating whether to log API requests
222+
* @return builder
223+
*/
189224
public Builder logRequests(Boolean logRequests) {
190225
this.logRequests = logRequests;
191226
return this;
192227
}
193228

229+
/**
230+
* @param logResponses a flag indicating whether to log API responses
231+
* @return builder
232+
*/
194233
public Builder logResponses(Boolean logResponses) {
195234
this.logResponses = logResponses;
196235
return this;
197236
}
198237

199-
public Builder timeout(Duration timeout) {
200-
this.timeout = timeout;
201-
return this;
202-
}
203-
204238
public MistralAiStreamingFimModel build() {
205239
return new MistralAiStreamingFimModel(this);
206240
}

0 commit comments

Comments
 (0)