Rework algorithm

This commit is contained in:
2025-10-03 00:54:30 +09:00
parent dfb23d88f5
commit 16a902a632
6 changed files with 139 additions and 97 deletions

View File

@@ -3,6 +3,7 @@ import { fileURLToPath } from "node:url";
import {
type ChatHistoryItem,
type LLamaChatPromptOptions,
LlamaChatSession,
type LlamaModel,
createModelDownloader,
@@ -19,7 +20,7 @@ export async function getModel(model: string) {
});
const modelPath = await downloader.download();
const llama = await getLlama({
maxThreads: 2,
maxThreads: 6,
});
return await llama.loadModel({ modelPath });
}
@@ -29,7 +30,11 @@ export type Message = {
text: string;
};
export async function complete(model: LlamaModel, messages: Message[]) {
export async function complete(
model: LlamaModel,
messages: Message[],
options: LLamaChatPromptOptions = {},
) {
if (messages.length < 1) throw new Error("messages are empty");
const init = messages.slice(0, -1);
const last = messages.at(-1) as Message;
@@ -61,14 +66,11 @@ export async function complete(model: LlamaModel, messages: Message[]) {
);
const res = await session.prompt(last.text, {
temperature: 1.0,
repeatPenalty: {
frequencyPenalty: 1,
},
trimWhitespaceSuffix: true,
onResponseChunk(chunk) {
process.stderr.write(chunk.text);
},
maxTokens: 200,
...options,
});
session.dispose();
await context.dispose();