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