Repository navigation
Expand file tree
/
Copy pathPromptTemplateConfig.java
More file actions
515 lines (473 loc) · 16.6 KB
/
Copy pathPromptTemplateConfig.java
File metadata and controls
515 lines (473 loc) · 16.6 KB
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.semanticfunctions;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.orchestration.PromptExecutionSettings;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Nullable;
/**
* Metadata for a prompt template.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class PromptTemplateConfig {
/**
* The current prompt template config schema version.
*/
public static final int CURRENT_SCHEMA = 1;
/**
* The default name for a prompt template config.
*/
public static final String DEFAULT_CONFIG_NAME = "default";
/**
* The default template format for a prompt template config.
*/
public static final String SEMANTIC_KERNEL_TEMPLATE_FORMAT = "semantic-kernel";
private final int schema;
@Nullable
private final String name;
@Nullable
private final String template;
private final String templateFormat;
private final Set<PromptTemplateOption> promptTemplateOptions;
@Nullable
private final String description;
private final List<InputVariable> inputVariables;
@Nullable
private final OutputVariable outputVariable;
private final Map<String, PromptExecutionSettings> executionSettings;
/**
* Constructor for a prompt template config
*
* @param template Template string
*/
protected PromptTemplateConfig(String template) {
this(
CURRENT_SCHEMA,
DEFAULT_CONFIG_NAME,
template,
SEMANTIC_KERNEL_TEMPLATE_FORMAT,
Collections.emptySet(),
"",
Collections.emptyList(),
new OutputVariable(String.class.getName(), "out"),
Collections.emptyMap());
}
/**
* Constructor for a prompt template config
*
* @param schema Schema version
* @param name Name of the template
* @param template Template string
* @param templateFormat Template format
* @param promptTemplateOptions Prompt template options
* @param description Description of the template
* @param inputVariables Input variables
* @param outputVariable Output variable
* @param executionSettings Execution settings
*/
@JsonCreator
public PromptTemplateConfig(
@JsonProperty("schema") int schema,
@Nullable @JsonProperty("name") String name,
@Nullable @JsonProperty("template") String template,
@Nullable @JsonProperty(value = "template_format", defaultValue = SEMANTIC_KERNEL_TEMPLATE_FORMAT) String templateFormat,
@Nullable @JsonProperty(value = "prompt_template_options") Set<PromptTemplateOption> promptTemplateOptions,
@Nullable @JsonProperty("description") String description,
@Nullable @JsonProperty("input_variables") List<InputVariable> inputVariables,
@Nullable @JsonProperty("output_variable") OutputVariable outputVariable,
@Nullable @JsonProperty("execution_settings") Map<String, PromptExecutionSettings> executionSettings) {
this.schema = schema;
this.name = name;
this.template = template;
if (templateFormat == null) {
templateFormat = SEMANTIC_KERNEL_TEMPLATE_FORMAT;
}
this.templateFormat = templateFormat;
if (promptTemplateOptions == null) {
promptTemplateOptions = new HashSet<>();
}
this.promptTemplateOptions = promptTemplateOptions;
this.description = description;
if (inputVariables == null) {
this.inputVariables = new ArrayList<>();
} else {
this.inputVariables = new ArrayList<>(inputVariables);
}
this.outputVariable = outputVariable != null
? outputVariable
: new OutputVariable(String.class.getName(), "out");
if (executionSettings == null) {
this.executionSettings = new HashMap<>();
} else {
this.executionSettings = new HashMap<>(executionSettings);
}
}
/**
* Constructor for a prompt template config
*
* @param name Name of the template
* @param template Template string
* @param templateFormat Template format
* @param description Description of the template
* @param inputVariables Input variables
* @param outputVariable Output variable
* @param executionSettings Execution settings
*/
protected PromptTemplateConfig(
@Nullable String name,
@Nullable String template,
@Nullable String templateFormat,
@Nullable Set<PromptTemplateOption> promptTemplateOptions,
@Nullable String description,
@Nullable List<InputVariable> inputVariables,
@Nullable OutputVariable outputVariable,
@Nullable Map<String, PromptExecutionSettings> executionSettings) {
this(
CURRENT_SCHEMA,
name,
template,
templateFormat,
promptTemplateOptions,
description,
inputVariables,
outputVariable,
executionSettings);
}
/**
* Copy constructor.
*
* @param promptTemplate The prompt template to copy.
*/
public PromptTemplateConfig(PromptTemplateConfig promptTemplate) {
this(
promptTemplate.name,
promptTemplate.template,
promptTemplate.templateFormat,
promptTemplate.promptTemplateOptions,
promptTemplate.description,
promptTemplate.inputVariables,
promptTemplate.outputVariable,
promptTemplate.executionSettings);
}
/**
* Deserialize the JSON string to a PromptTemplateConfig.
*
* @param json The JSON string to parse
* @return The PromptTemplateConfig object
* @throws SKException If the prompt template config cannot be deserialized.
*/
public static PromptTemplateConfig parseFromJson(String json) throws SKException {
try {
return new ObjectMapper().readValue(json, PromptTemplateConfig.class);
} catch (JsonProcessingException e) {
throw new SKException("Unable to parse prompt template config", e);
}
}
/**
* Create a builder for a prompt template config.
*
* @return The prompt template config builder.
*/
public static Builder builder() {
return new Builder();
}
/**
* Create a builder for a prompt template config, where the constructed template will be
* considered the default to be used if no other config is selected.
*
* @return The default prompt template config.
*/
public static Builder defaultTemplateBuilder() {
return new Builder()
.withName(DEFAULT_CONFIG_NAME);
}
/**
* Get the parameters metadata.
*
* @return The parameters metadata.
*/
public List<InputVariable> getKernelParametersMetadata() {
if (inputVariables == null) {
return Collections.emptyList();
}
return Collections.unmodifiableList(inputVariables);
}
/**
* Get the return parameter metadata.
*
* @return The return parameter metadata.
*/
public OutputVariable<?> getKernelReturnParameterMetadata() {
if (outputVariable == null) {
return new OutputVariable<>("", String.class);
}
return new OutputVariable<>(
outputVariable.getDescription(),
outputVariable.getType());
}
/**
* Get the name of the prompt template config.
*
* @return The name of the prompt template config.
*/
@Nullable
public String getName() {
return name;
}
/**
* Get the template of the prompt template config.
*
* @return The template of the prompt template config.
*/
@Nullable
public String getTemplate() {
return template;
}
/**
* Get the description of the prompt template config.
*
* @return The description of the prompt template config.
*/
@Nullable
public String getDescription() {
return description;
}
/**
* Get the inputVariables of the prompt template config.
*
* @return The input variables of the prompt template config.
*/
public List<InputVariable> getInputVariables() {
return Collections.unmodifiableList(inputVariables);
}
/**
* Get the output variable of the prompt template config.
*
* @return The output variable of the prompt template config.
*/
@Nullable
public OutputVariable getOutputVariable() {
return outputVariable;
}
/**
* Get the prompt execution settings of the prompt template config.
*
* @return The prompt execution settings of the prompt template config.
*/
@Nullable
public Map<String, PromptExecutionSettings> getExecutionSettings() {
if (executionSettings != null) {
return Collections.unmodifiableMap(executionSettings);
}
return null;
}
/**
* Get the template format of the prompt template config.
*
* @return The template format of the prompt template config.
*/
public String getTemplateFormat() {
return templateFormat;
}
/**
* Get the schema version of the prompt template config.
*
* @return The schema version of the prompt template config.
*/
public int getSchema() {
return schema;
}
/**
* Get the prompt template options of the prompt template config.
*
* @return The prompt template options of the prompt template config.
*/
public Set<PromptTemplateOption> getPromptTemplateOptions() {
return Collections.unmodifiableSet(promptTemplateOptions);
}
/**
* Create a builder for a prompt template config which is a clone of the current object.
*
* @return The prompt template config builder.
*/
public Builder copy() {
return new Builder(this);
}
@Override
public int hashCode() {
return Objects.hash(name, template, templateFormat, description, inputVariables,
outputVariable, executionSettings);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!getClass().isInstance(obj)) {
return false;
}
final PromptTemplateConfig other = (PromptTemplateConfig) obj;
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (!Objects.equals(this.template, other.template)) {
return false;
}
if (!Objects.equals(this.description, other.description)) {
return false;
}
if (!Objects.equals(this.templateFormat, other.templateFormat)) {
return false;
}
if (!Objects.equals(this.inputVariables, other.inputVariables)) {
return false;
}
if (!Objects.equals(this.outputVariable, other.outputVariable)) {
return false;
}
return Objects.equals(this.executionSettings, other.executionSettings);
}
/**
* Builder for a prompt template config.
*/
public static class Builder {
@Nullable
private String name;
@Nullable
private String template;
private String templateFormat = SEMANTIC_KERNEL_TEMPLATE_FORMAT;
private final Set<PromptTemplateOption> promptTemplateOptions = new HashSet<>();
@Nullable
private String description = null;
private List<InputVariable> inputVariables = new ArrayList<>();
@Nullable
private OutputVariable outputVariable;
private Map<String, PromptExecutionSettings> executionSettings = new HashMap<>();
private Builder() {
}
private Builder(PromptTemplateConfig promptTemplateConfig) {
this.name = promptTemplateConfig.name;
this.template = promptTemplateConfig.template;
this.templateFormat = promptTemplateConfig.templateFormat;
this.description = promptTemplateConfig.description;
this.inputVariables = new ArrayList<>(promptTemplateConfig.inputVariables);
this.outputVariable = promptTemplateConfig.outputVariable;
this.executionSettings = new HashMap<>(promptTemplateConfig.executionSettings);
}
/**
* Set the name of the prompt template config.
*
* @param name The name of the prompt template config.
* @return {@code this} prompt template config.
*/
public Builder withName(String name) {
this.name = name;
return this;
}
/**
* Add an input variable to the prompt template config.
*
* @param inputVariable The input variable to add.
* @return {@code this} prompt template config.
*/
public Builder addInputVariable(InputVariable inputVariable) {
inputVariables.add(inputVariable);
return this;
}
/**
* Set the template of the prompt template config.
*
* @param template The template of the prompt template config.
* @return {@code this} prompt template config.
*/
public Builder withTemplate(String template) {
this.template = template;
return this;
}
/**
* Set the description of the prompt template config.
*
* @param description The description of the prompt template config.
* @return {@code this} prompt template config.
*/
public Builder withDescription(String description) {
this.description = description;
return this;
}
/**
* Set the template format of the prompt template config.
*
* @param templateFormat The template format of the prompt template config.
* @return {@code this} prompt template config.
*/
public Builder withTemplateFormat(String templateFormat) {
this.templateFormat = templateFormat;
return this;
}
public Builder addPromptTemplateOption(PromptTemplateOption option) {
promptTemplateOptions.add(option);
return this;
}
/**
* Set the inputVariables of the prompt template config.
*
* @param inputVariables The input variables of the prompt template config.
* @return {@code this} prompt template config.
*/
public Builder withInputVariables(List<InputVariable> inputVariables) {
this.inputVariables = new ArrayList<>(inputVariables);
return this;
}
/**
* Set the output variable of the prompt template config.
*
* @param outputVariable The output variable of the prompt template config.
* @return {@code this} prompt template config.
*/
public Builder withOutputVariable(OutputVariable<?> outputVariable) {
this.outputVariable = outputVariable;
return this;
}
/**
* Set the prompt execution settings of the prompt template config.
*
* @param executionSettings The prompt execution settings of the prompt template config.
* @return {@code this} prompt template config.
*/
public Builder withExecutionSettings(
Map<String, PromptExecutionSettings> executionSettings) {
this.executionSettings = new HashMap<>(executionSettings);
return this;
}
/**
* Build the prompt template config.
*
* @return The prompt template config.
*/
public PromptTemplateConfig build() {
return new PromptTemplateConfig(
name,
template,
templateFormat,
promptTemplateOptions,
description,
inputVariables,
outputVariable,
executionSettings);
}
}
}