|
| 1 | +import {describe, expect, test} from "vitest"; |
| 2 | +import {defineChatSessionFunction, LlamaChatSession, QwenChatWrapper} from "../../../src/index.js"; |
| 3 | +import {getModelFile} from "../../utils/modelFiles.js"; |
| 4 | +import {getTestLlama} from "../../utils/getTestLlama.js"; |
| 5 | + |
| 6 | +describe("qwen3.5 0.8b", () => { |
| 7 | + describe("functions", () => { |
| 8 | + test("use checkpoints", {timeout: 1000 * 60 * 60 * 2}, async () => { |
| 9 | + const modelPath = await getModelFile("Qwen3.5-0.8B-Q8_0.gguf"); |
| 10 | + const llama = await getTestLlama(); |
| 11 | + |
| 12 | + const model = await llama.loadModel({ |
| 13 | + modelPath |
| 14 | + }); |
| 15 | + const context = await model.createContext({ |
| 16 | + contextSize: 4096 |
| 17 | + }); |
| 18 | + const chatSession = new LlamaChatSession({ |
| 19 | + contextSequence: context.getSequence() |
| 20 | + }); |
| 21 | + expect(chatSession.chatWrapper).toBeInstanceOf(QwenChatWrapper); |
| 22 | + |
| 23 | + const promptOptions: Parameters<typeof chatSession.prompt>[1] = { |
| 24 | + functions: { |
| 25 | + getNthWord: defineChatSessionFunction({ |
| 26 | + description: "Get an n-th word", |
| 27 | + params: { |
| 28 | + type: "object", |
| 29 | + properties: { |
| 30 | + n: { |
| 31 | + enum: [1, 2, 3, 4] |
| 32 | + } |
| 33 | + } |
| 34 | + }, |
| 35 | + handler(params) { |
| 36 | + return ["very", "secret", "this", "hello"][params.n - 1]; |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | + } as const; |
| 41 | + |
| 42 | + const res = await chatSession.prompt("What is the second word? use the function", promptOptions); |
| 43 | + |
| 44 | + expect(res).to.toMatchInlineSnapshot(` |
| 45 | + " |
| 46 | +
|
| 47 | +
|
| 48 | +
|
| 49 | + The second word is "secret"." |
| 50 | + `); |
| 51 | + expect(chatSession.sequence.tokenMeter.usedInputTokens).toMatchInlineSnapshot("372"); |
| 52 | + expect(chatSession.sequence.lastCheckpointIndex).toMatchInlineSnapshot("393"); |
| 53 | + expect(chatSession.sequence.nextTokenIndex).toMatchInlineSnapshot("405"); |
| 54 | + |
| 55 | + const initialMeterState = chatSession.sequence.tokenMeter.getState(); |
| 56 | + const res2 = await chatSession.prompt("Explain what this word means", { |
| 57 | + ...promptOptions, |
| 58 | + maxTokens: 40 |
| 59 | + }); |
| 60 | + |
| 61 | + const diffMeterState = chatSession.sequence.tokenMeter.diff(initialMeterState); |
| 62 | + expect(res2).to.toMatchInlineSnapshot(` |
| 63 | + " |
| 64 | +
|
| 65 | + The word "secret" means something that is hidden or kept from others. It can also refer to a secret knowledge, a secret weapon, or a secret place." |
| 66 | + `); |
| 67 | + expect(diffMeterState.usedInputTokens).toMatchInlineSnapshot("73"); |
| 68 | + expect(diffMeterState.usedInputTokens).to.be.lessThanOrEqual(80); |
| 69 | + expect(chatSession.sequence.lastCheckpointIndex).toMatchInlineSnapshot("414"); |
| 70 | + expect(chatSession.sequence.nextTokenIndex).toMatchInlineSnapshot("452"); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments