-
Notifications
You must be signed in to change notification settings - Fork 2k
/
template.ts
131 lines (116 loc) Β· 3.98 KB
/
template.ts
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
import { InputValues } from "../schema/index.js";
/**
* Type that specifies the format of a template. Only
* "f-string" is supported currently.
*/
export type TemplateFormat = "f-string";
/**
* Type that represents a node in a parsed format string. It can be either
* a literal text or a variable name.
*/
type ParsedFStringNode =
| { type: "literal"; text: string }
| { type: "variable"; name: string };
export const parseFString = (template: string): ParsedFStringNode[] => {
// Core logic replicated from internals of pythons built in Formatter class.
// https://github.com/python/cpython/blob/135ec7cefbaffd516b77362ad2b2ad1025af462e/Objects/stringlib/unicode_format.h#L700-L706
const chars = template.split("");
const nodes: ParsedFStringNode[] = [];
const nextBracket = (bracket: "}" | "{" | "{}", start: number) => {
for (let i = start; i < chars.length; i += 1) {
if (bracket.includes(chars[i])) {
return i;
}
}
return -1;
};
let i = 0;
while (i < chars.length) {
if (chars[i] === "{" && i + 1 < chars.length && chars[i + 1] === "{") {
nodes.push({ type: "literal", text: "{" });
i += 2;
} else if (
chars[i] === "}" &&
i + 1 < chars.length &&
chars[i + 1] === "}"
) {
nodes.push({ type: "literal", text: "}" });
i += 2;
} else if (chars[i] === "{") {
const j = nextBracket("}", i);
if (j < 0) {
throw new Error("Unclosed '{' in template.");
}
nodes.push({
type: "variable",
name: chars.slice(i + 1, j).join(""),
});
i = j + 1;
} else if (chars[i] === "}") {
throw new Error("Single '}' in template.");
} else {
const next = nextBracket("{}", i);
const text = (next < 0 ? chars.slice(i) : chars.slice(i, next)).join("");
nodes.push({ type: "literal", text });
i = next < 0 ? chars.length : next;
}
}
return nodes;
};
export const interpolateFString = (template: string, values: InputValues) =>
parseFString(template).reduce((res, node) => {
if (node.type === "variable") {
if (node.name in values) {
return res + values[node.name];
}
throw new Error(`Missing value for input ${node.name}`);
}
return res + node.text;
}, "");
/**
* Type that represents a function that takes a template string and a set
* of input values, and returns a string where all variables in the
* template have been replaced with their corresponding values.
*/
type Interpolator = (template: string, values: InputValues) => string;
/**
* Type that represents a function that takes a template string and
* returns an array of `ParsedFStringNode`.
*/
type Parser = (template: string) => ParsedFStringNode[];
export const DEFAULT_FORMATTER_MAPPING: Record<TemplateFormat, Interpolator> = {
"f-string": interpolateFString,
};
export const DEFAULT_PARSER_MAPPING: Record<TemplateFormat, Parser> = {
"f-string": parseFString,
};
export const renderTemplate = (
template: string,
templateFormat: TemplateFormat,
inputValues: InputValues
) => DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);
export const parseTemplate = (
template: string,
templateFormat: TemplateFormat
) => DEFAULT_PARSER_MAPPING[templateFormat](template);
export const checkValidTemplate = (
template: string,
templateFormat: TemplateFormat,
inputVariables: string[]
) => {
if (!(templateFormat in DEFAULT_FORMATTER_MAPPING)) {
const validFormats = Object.keys(DEFAULT_FORMATTER_MAPPING);
throw new Error(`Invalid template format. Got \`${templateFormat}\`;
should be one of ${validFormats}`);
}
try {
const dummyInputs: InputValues = inputVariables.reduce((acc, v) => {
acc[v] = "foo";
return acc;
}, {} as Record<string, string>);
renderTemplate(template, templateFormat, dummyInputs);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
throw new Error(`Invalid prompt schema: ${e.message}`);
}
};