Refactor (2)
This commit is contained in:
23
lib/llm.ts
23
lib/llm.ts
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user