-
Notifications
You must be signed in to change notification settings - Fork 608
/
BedrockAi21Jurassic2ChatOptions.java
431 lines (359 loc) · 10.3 KB
/
BedrockAi21Jurassic2ChatOptions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* Copyright 2023 - 2024 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.ai.bedrock.jurassic2;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.ai.chat.prompt.ChatOptions;
/**
* Request body for the /complete endpoint of the Jurassic-2 API.
*
* @author Ahmed Yousri
* @since 1.0.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BedrockAi21Jurassic2ChatOptions implements ChatOptions {
/**
* The text which the model is requested to continue.
*/
@JsonProperty("prompt")
private String prompt;
/**
* Number of completions to sample and return.
*/
@JsonProperty("numResults")
private Integer numResults;
/**
* The maximum number of tokens to generate per result.
*/
@JsonProperty("maxTokens")
private Integer maxTokens;
/**
* The minimum number of tokens to generate per result.
*/
@JsonProperty("minTokens")
private Integer minTokens;
/**
* Modifies the distribution from which tokens are sampled.
*/
@JsonProperty("temperature")
private Float temperature;
/**
* Sample tokens from the corresponding top percentile of probability mass.
*/
@JsonProperty("topP")
private Float topP;
/**
* Return the top-K (topKReturn) alternative tokens.
*/
@JsonProperty("topKReturn")
private Integer topK;
/**
* Stops decoding if any of the strings is generated.
*/
@JsonProperty("stopSequences")
private String[] stopSequences;
/**
* Penalty object for frequency.
*/
@JsonProperty("frequencyPenalty")
private Penalty frequencyPenalty;
/**
* Penalty object for presence.
*/
@JsonProperty("presencePenalty")
private Penalty presencePenalty;
/**
* Penalty object for count.
*/
@JsonProperty("countPenalty")
private Penalty countPenalty;
// Getters and setters
/**
* Gets the prompt text for the model to continue.
* @return The prompt text.
*/
public String getPrompt() {
return prompt;
}
/**
* Sets the prompt text for the model to continue.
* @param prompt The prompt text.
*/
public void setPrompt(String prompt) {
this.prompt = prompt;
}
/**
* Gets the number of completions to sample and return.
* @return The number of results.
*/
public Integer getNumResults() {
return numResults;
}
/**
* Sets the number of completions to sample and return.
* @param numResults The number of results.
*/
public void setNumResults(Integer numResults) {
this.numResults = numResults;
}
/**
* Gets the maximum number of tokens to generate per result.
* @return The maximum number of tokens.
*/
public Integer getMaxTokens() {
return maxTokens;
}
/**
* Sets the maximum number of tokens to generate per result.
* @param maxTokens The maximum number of tokens.
*/
public void setMaxTokens(Integer maxTokens) {
this.maxTokens = maxTokens;
}
/**
* Gets the minimum number of tokens to generate per result.
* @return The minimum number of tokens.
*/
public Integer getMinTokens() {
return minTokens;
}
/**
* Sets the minimum number of tokens to generate per result.
* @param minTokens The minimum number of tokens.
*/
public void setMinTokens(Integer minTokens) {
this.minTokens = minTokens;
}
/**
* Gets the temperature for modifying the token sampling distribution.
* @return The temperature.
*/
public Float getTemperature() {
return temperature;
}
/**
* Sets the temperature for modifying the token sampling distribution.
* @param temperature The temperature.
*/
public void setTemperature(Float temperature) {
this.temperature = temperature;
}
/**
* Gets the topP parameter for sampling tokens from the top percentile of probability
* mass.
* @return The topP parameter.
*/
public Float getTopP() {
return topP;
}
/**
* Sets the topP parameter for sampling tokens from the top percentile of probability
* mass.
* @param topP The topP parameter.
*/
public void setTopP(Float topP) {
this.topP = topP;
}
/**
* Gets the top-K (topKReturn) alternative tokens to return.
* @return The top-K parameter. (topKReturn)
*/
@Override
public Integer getTopK() {
return topK;
}
/**
* Sets the top-K (topKReturn) alternative tokens to return.
* @param topK The top-K parameter (topKReturn).
*/
public void setTopK(Integer topK) {
this.topK = topK;
}
/**
* Gets the stop sequences for stopping decoding if any of the strings is generated.
* @return The stop sequences.
*/
public String[] getStopSequences() {
return stopSequences;
}
/**
* Sets the stop sequences for stopping decoding if any of the strings is generated.
* @param stopSequences The stop sequences.
*/
public void setStopSequences(String[] stopSequences) {
this.stopSequences = stopSequences;
}
/**
* Gets the frequency penalty object.
* @return The frequency penalty object.
*/
public Penalty getFrequencyPenalty() {
return frequencyPenalty;
}
/**
* Sets the frequency penalty object.
* @param frequencyPenalty The frequency penalty object.
*/
public void setFrequencyPenalty(Penalty frequencyPenalty) {
this.frequencyPenalty = frequencyPenalty;
}
/**
* Gets the presence penalty object.
* @return The presence penalty object.
*/
public Penalty getPresencePenalty() {
return presencePenalty;
}
/**
* Sets the presence penalty object.
* @param presencePenalty The presence penalty object.
*/
public void setPresencePenalty(Penalty presencePenalty) {
this.presencePenalty = presencePenalty;
}
/**
* Gets the count penalty object.
* @return The count penalty object.
*/
public Penalty getCountPenalty() {
return countPenalty;
}
/**
* Sets the count penalty object.
* @param countPenalty The count penalty object.
*/
public void setCountPenalty(Penalty countPenalty) {
this.countPenalty = countPenalty;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private final BedrockAi21Jurassic2ChatOptions request = new BedrockAi21Jurassic2ChatOptions();
public Builder withPrompt(String prompt) {
request.setPrompt(prompt);
return this;
}
public Builder withNumResults(Integer numResults) {
request.setNumResults(numResults);
return this;
}
public Builder withMaxTokens(Integer maxTokens) {
request.setMaxTokens(maxTokens);
return this;
}
public Builder withMinTokens(Integer minTokens) {
request.setMinTokens(minTokens);
return this;
}
public Builder withTemperature(Float temperature) {
request.setTemperature(temperature);
return this;
}
public Builder withTopP(Float topP) {
request.setTopP(topP);
return this;
}
public Builder withStopSequences(String[] stopSequences) {
request.setStopSequences(stopSequences);
return this;
}
public Builder withTopK(Integer topKReturn) {
request.setTopK(topKReturn);
return this;
}
public Builder withFrequencyPenalty(BedrockAi21Jurassic2ChatOptions.Penalty frequencyPenalty) {
request.setFrequencyPenalty(frequencyPenalty);
return this;
}
public Builder withPresencePenalty(BedrockAi21Jurassic2ChatOptions.Penalty presencePenalty) {
request.setPresencePenalty(presencePenalty);
return this;
}
public Builder withCountPenalty(BedrockAi21Jurassic2ChatOptions.Penalty countPenalty) {
request.setCountPenalty(countPenalty);
return this;
}
public BedrockAi21Jurassic2ChatOptions build() {
return request;
}
}
/**
* Penalty object for frequency, presence, and count penalties.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public record Penalty(@JsonProperty("scale") Float scale, @JsonProperty("applyToNumbers") Boolean applyToNumbers,
@JsonProperty("applyToPunctuations") Boolean applyToPunctuations,
@JsonProperty("applyToStopwords") Boolean applyToStopwords,
@JsonProperty("applyToWhitespaces") Boolean applyToWhitespaces,
@JsonProperty("applyToEmojis") Boolean applyToEmojis) {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private Float scale;
// can't keep it null due to modelOptionsUtils#mapToClass convert null to
// false
private Boolean applyToNumbers = true;
private Boolean applyToPunctuations = true;
private Boolean applyToStopwords = true;
private Boolean applyToWhitespaces = true;
private Boolean applyToEmojis = true;
public Builder scale(Float scale) {
this.scale = scale;
return this;
}
public Builder applyToNumbers(Boolean applyToNumbers) {
this.applyToNumbers = applyToNumbers;
return this;
}
public Builder applyToPunctuations(Boolean applyToPunctuations) {
this.applyToPunctuations = applyToPunctuations;
return this;
}
public Builder applyToStopwords(Boolean applyToStopwords) {
this.applyToStopwords = applyToStopwords;
return this;
}
public Builder applyToWhitespaces(Boolean applyToWhitespaces) {
this.applyToWhitespaces = applyToWhitespaces;
return this;
}
public Builder applyToEmojis(Boolean applyToEmojis) {
this.applyToEmojis = applyToEmojis;
return this;
}
public Penalty build() {
return new Penalty(scale, applyToNumbers, applyToPunctuations, applyToStopwords, applyToWhitespaces,
applyToEmojis);
}
}
}
public static BedrockAi21Jurassic2ChatOptions fromOptions(BedrockAi21Jurassic2ChatOptions fromOptions) {
return builder().withPrompt(fromOptions.getPrompt())
.withNumResults(fromOptions.getNumResults())
.withMaxTokens(fromOptions.getMaxTokens())
.withMinTokens(fromOptions.getMinTokens())
.withTemperature(fromOptions.getTemperature())
.withTopP(fromOptions.getTopP())
.withTopK(fromOptions.getTopK())
.withStopSequences(fromOptions.getStopSequences())
.withFrequencyPenalty(fromOptions.getFrequencyPenalty())
.withPresencePenalty(fromOptions.getPresencePenalty())
.withCountPenalty(fromOptions.getCountPenalty())
.build();
}
}