Refactor (9)

This commit is contained in:
2026-03-05 22:59:05 +09:00
parent 0d895797f6
commit f61298f9e4
2 changed files with 20 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ import {
type LLamaChatPromptOptions,
LlamaChatSession,
type LlamaModel,
TokenBias,
createModelDownloader,
getLlama,
resolveChatWrapper,
@@ -27,6 +28,15 @@ export async function getModel(model: string) {
return await llama.loadModel({ modelPath });
}
export function createBias(model: LlamaModel) {
const customBias = new TokenBias(model.tokenizer);
for (const token of model.iterateAllTokens()) {
const text = model.detokenize([token]);
if (text === "{") customBias.set(token, -0.9); // suppress JSON string
}
return customBias;
}
export const createGrammar = (assistantName: string) =>
llama.createGrammarForJsonSchema({
type: "object",