Refactor (2)

This commit is contained in:
2026-02-24 22:08:20 +09:00
parent 0f9eb68262
commit c60eeb4898
3 changed files with 89 additions and 86 deletions

View File

@@ -27,16 +27,21 @@ export async function getModel(model: string) {
return await llama.loadModel({ modelPath });
}
export const grammar = await llama.createGrammarForJsonSchema({
type: "object",
properties: {
text: { type: "string" },
},
required: ["text"],
additionalProperties: false,
});
export const createGrammar = (assistantName: string) =>
llama.createGrammarForJsonSchema({
type: "object",
properties: {
name: { type: "string", enum: [assistantName] },
text: { type: "string" },
},
required: ["text"],
additionalProperties: false,
});
export function parseResponse(text: string) {
export function parseResponse(
grammar: Awaited<ReturnType<typeof createGrammar>>,
text: string,
) {
try {
const res = grammar.parse(text.trim());
return res.text;