-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.ts
More file actions
324 lines (295 loc) · 9.46 KB
/
mod.ts
File metadata and controls
324 lines (295 loc) · 9.46 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
import ollama, {
type Options as OllamaOptions,
type Tool as OllamaTool,
} from "npm:ollama@0.5.9";
import zod from "npm:zod@3.23.8";
import { zodToTs, printNode } from "npm:zod-to-ts@1.2.0";
import type { OjjsonAdapter } from "./adapter/adapter.ts";
export * from "./adapter/adapter.ts";
export * from "./adapter/OllamaAdapter.ts";
export * from "./adapter/OpenAIAdapter.ts";
type ReactiveOrStatic<T> = T | (() => T);
/**
* Resolve a reactive or static value
* @param value
*/
function resolveToStatic<T>(value: ReactiveOrStatic<T>): T {
return typeof value === "function" ? (value as () => T)() : value;
}
type _OllamaChatParams = Parameters<typeof ollama.chat>["0"];
type Message = NonNullable<_OllamaChatParams["messages"]>[0];
/**
* Ojjson options
* @template Input The input schema
* @template Output The output schema
*/
export type OjjsonOptions<
// deno-lint-ignore no-explicit-any
Input extends zod.ZodObject<any>,
// deno-lint-ignore no-explicit-any
Output extends zod.ZodObject<any>
> = {
/**
* A description how to convert input to export object that will be added to the prompt. This help is prepended with `A description on how to convert input to export object:`
*/
conversionHelp?: ReactiveOrStatic<string>;
/**
* Example input and output objects that will be added to the chat history
*/
examples?: ReactiveOrStatic<
Array<{
input: zod.infer<Input>;
output: zod.infer<Output>;
}>
>;
/**
* The maximum number of messages to keep in the chat history
*/
maxMessages?: number;
/**
* Log additional information
*/
verbose?: boolean;
/**
* Ollama options
*/
ollamaOptions?: ReactiveOrStatic<OllamaOptions>;
/**
* Ollama tools
*/
ollamaTools?: ReactiveOrStatic<OllamaTool[]>;
/**
* Ollama keep alive
*/
ollamaKeepAlive?: ReactiveOrStatic<string | number | undefined>;
};
/**
* Ojjson generator
* Allows you to pass in schema instances and generate responses from the model
* It will automatically validate the input and output with the schema
* @template Input The input schema
* @template Output The output schema
*/
export class OjjsonGenerator<
// deno-lint-ignore no-explicit-any
Input extends zod.ZodObject<any>,
// deno-lint-ignore no-explicit-any
Output extends zod.ZodObject<any>
> {
/**
* Create a new OjjsonGenerator
* @param model The model name
* @param input The input schema
* @param output The output schema
* @param options Additional options
*/
constructor(
public adapter: OjjsonAdapter,
public input: Input,
public output: Output,
public options: OjjsonOptions<Input, Output> = {}
) {}
/**
* The chat history
*/
previousMessages: Message[][] = [];
/**
* Add a message pair to the chat history
* @param message The message pair
*/
addMessagePair(message: Message[]) {
this.previousMessages.push(message);
if (this.previousMessages.length > (this.options.maxMessages ?? 10)) {
this.previousMessages.shift();
}
}
/**
* Verbose log
* @param args The arguments to log
*/
#log(...args: unknown[]) {
if (this.options.verbose) {
console.log("[OjjsonGenerator]", ...args);
}
}
/**
* Extract the json from a string
* Returns everything between the first `{` and the last `}`
*/
#extractJson(str: string): string {
const start = str.indexOf("{");
const end = str.lastIndexOf("}");
return str.substring(start, end + 1);
}
/**
* Generate a response from the model
* You can specify the number of retries and fixTries
* If the response is a valid json but zod validation fails, it will ask the system to fix the input by providing zod errors, it has `fixTries` attempts to fix the input
* If the response is not a valid json, or the system fails to fix the input, it will retry `retries` times
* If all retries fail, it will throw an error
* @param input The input object
* @param retries The number of retries to attempt
* @param fixTries The number of tries to attempt to fix the input
* @param previousMessages The previous messages to add to the chat history
* @returns The output object
* @throws If all retries fail
*/
async generate(
input: zod.infer<Input>,
retries = 2,
fixTries = 1,
previousMessages?: Message[],
): Promise<zod.infer<Output> | null> {
const examples: Message[] = [];
for (const example of resolveToStatic(this.options.examples ?? [])) {
examples.push({ role: "user", content: JSON.stringify(example.input, null, 1) });
examples.push({
role: "assistant",
content: JSON.stringify(example.output, null, 1),
});
}
const prompt = this.#getPromptText();
const chatMessages = [
{
role: "system",
content: prompt,
},
].concat(
examples,
examples.length > 0 ? [{ role: "system", content: "-- END OF EXAMPLES, REAL TASK STARTS NOW --" }] : [],
typeof previousMessages !== "undefined" ? previousMessages : this.previousMessages.flat(),
typeof previousMessages !== "undefined"
? []
: [
{
role: "user",
content: JSON.stringify(input, null, 1),
},
]
);
const response = await this.adapter.chat(chatMessages);
try {
const out = this.output.parse(
JSON.parse(this.#extractJson(response.content))
);
this.#log("---------------------------------")
this.#log(prompt);
this.#log({ input, output: out, examples, chatMessages });
this.#log("---------------------------------");
this.addMessagePair([
{ role: "user", content: JSON.stringify(input, null, 1) },
response,
]);
return out;
} catch (e) {
if (e instanceof zod.ZodError) {
const exception = e;
for (let i = 0; i < fixTries; i++) {
this.#log(
"zod failed to validate, trying to fix [" + i + "/" + fixTries + "]"
);
let fixPrompt = this.#getErrorMessage(exception);
fixPrompt += `The output you provided was invalid, please provide a valid output that matches the schema. Those issues occurred:\n${fixPrompt}`;
const retry = await this.adapter.chat(examples.concat([
{
role: "system",
content: prompt,
},
{
role: "user",
content: JSON.stringify(input, null, 1),
},
{
role: "assistant",
content: this.#extractJson(response.content),
},
{
role: "system",
content: fixPrompt,
},
]));
try {
const out = this.output.parse(
JSON.parse(this.#extractJson(retry.content))
);
this.addMessagePair([
{ role: "user", content: JSON.stringify(input, null, 1) },
{ role: "assistant", content: this.#extractJson(response.content) },
]);
return out;
} catch {
continue;
}
}
// If not fixed yet, retry whole function
if (retries > 0) {
this.#log(
"Failed to fix, retrying prompt [" + retries + "/" + retries + "]"
);
return this.generate(input, retries - 1, fixTries);
}
throw e;
} else {
throw e;
}
}
}
/**
* Get a human readable error message from a zod exception
* @param exception The zod exception
* @returns The error message
*/
#getErrorMessage(exception: zod.ZodError): string {
let fixPrompt = "";
exception.errors.forEach((error) => {
if (error.code == "invalid_union") {
error.unionErrors.forEach((unionError) => {
unionError.errors.forEach((err) => {
if (err.code == "invalid_type") {
fixPrompt += `* ${err.path.join(".")}: Received ${
err.received
} but expected ${err.expected}, ${err.message}\n`;
} else {
fixPrompt += `* ${err.path.join(".")}: ${err.message}\n`;
}
});
});
} else {
fixPrompt += `* ${error.path.join(".")}: ${error.message}\n`;
}
});
return fixPrompt;
}
/**
* Get the prompt text
*/
#getPromptText() {
const conversionHelp = resolveToStatic(this.options.conversionHelp);
const conversion = resolveToStatic(conversionHelp)
? "\n# A description on how to convert input to export object: " +
conversionHelp +
"\n\n"
: "";
const prompt =
`You are an AI that receives a JSON object and returns only another JSON object.
* Your input will always be a JSON object that matches the following schema:
\`\`\`
${printNode(zodToTs(this.input, undefined, { nativeEnums: "union" }).node)}
\`\`\`
* Your output should be a JSON object that matches the following schema:
\`\`\`
${printNode(zodToTs(this.output, undefined, { nativeEnums: "union" }).node)}
\`\`\`
` +
conversion +
`
# How to treat the task:
* You can assume that the input will always be valid and match the schema.
* You can assume that the input will always be a JSON object except in case you've provided an invalid output.
* I will inform you if the output is invalid and you will have to provide a valid output.
* Strictly follow the schema for the output.
* Never return anything other than a JSON object.
* Do not talk to the user.`;
return prompt;
}
}