1
- const { execSync } = require ( 'child_process' ) ;
2
- const fs = require ( 'fs' ) ;
1
+ import { execSync } from 'child_process' ;
2
+ import { existsSync } from 'fs' ;
3
+ import assert from 'assert' ;
3
4
4
- const { ChatManager, Role } = require ( '../chatManager' ) ;
5
- const {
5
+ import { ChatManager , Role } from '../src/chat' ;
6
+ import {
6
7
RunInference ,
7
8
LoadModelAsync ,
8
9
CreateContextAsync ,
@@ -11,83 +12,81 @@ const {
11
12
ReleaseModelAsync ,
12
13
SetLogLevel ,
13
14
GetModelToken ,
14
- } = require ( "bindings" ) ( "npm-llama" ) ;
15
+ } from '../src/index' ;
16
+
15
17
16
18
const model = "model.gguf" ;
17
19
const modelUrl = "https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-fp16.gguf?download=true" ;
18
- const system_prompt = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly." ;
19
-
20
+ const systemPrompt = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly." ;
20
21
21
- describe ( 'Node LLaMA Test Suite' , ( ) => {
22
+ describe ( "Llama tests - basic" , ( ) => {
22
23
23
24
beforeAll ( ( ) => {
24
-
25
- if ( ! fs . existsSync ( model ) ) {
25
+ // Setup - Download model if needed
26
+ if ( ! existsSync ( model ) ) {
26
27
execSync ( `npx llama-download -p ${ model } -u ${ modelUrl } ` , { stdio : 'inherit' } ) ;
27
28
} else {
28
29
console . log ( "Model already downloaded" ) ;
29
30
}
30
- } ) ;
31
+ } )
31
32
32
- test ( 'log level works' , ( ) => {
33
+ test ( 'log level works' , async ( ) => {
33
34
SetLogLevel ( 1 ) ; // debug logs
34
- expect ( true ) . toBeTruthy ( ) ;
35
+ assert . ok ( true ) ;
35
36
} ) ;
36
37
37
- test ( 'direct inference works' , ( ) => {
38
- const inference = RunInference ( model , "How old can ducks get?" , system_prompt ) ;
38
+ test ( 'direct inference works' , async ( ) => {
39
+ const inference : string = RunInference ( model , "How old can ducks get?" , systemPrompt ) ;
39
40
console . log ( "Result" , inference ) ;
40
- expect ( inference . includes ( '10 years' ) ) . toBeTruthy ( ) ;
41
+ assert . ok ( inference . includes ( '10 years' ) ) ;
41
42
} ) ;
42
43
43
44
test ( 'async inference works' , async ( ) => {
44
-
45
- const prompts = [
45
+ const prompts : string [ ] = [
46
46
"How old can ducks get?" ,
47
47
"Why are ducks so cool?" ,
48
48
"Is there a limit on number of ducks I can own?"
49
- ]
49
+ ] ;
50
50
51
51
const modelHandle = await LoadModelAsync ( model ) ;
52
52
const ctx = await CreateContextAsync ( modelHandle ) ;
53
53
console . log ( "Model loaded" , model ) ;
54
54
55
55
for ( const prompt of prompts ) {
56
- const inference = await RunInferenceAsync ( modelHandle , ctx , prompt , system_prompt , 64 ) ;
56
+ const inference : string = await RunInferenceAsync ( modelHandle , ctx , prompt , systemPrompt , 64 ) ;
57
57
console . log ( "Reply:" , inference ) ;
58
- expect ( inference . length > 0 ) . toBeTruthy ( ) ;
58
+ assert . ok ( inference . length > 0 ) ;
59
59
}
60
60
61
61
await ReleaseContextAsync ( ctx ) ;
62
62
await ReleaseModelAsync ( modelHandle ) ;
63
63
} ) ;
64
64
65
65
test ( 'custom inference works' , async ( ) => {
66
-
67
66
const user = "How old can ducks get?" ;
68
- const prompt = `"!#<|im_start|>system ${ system_prompt } <|im_end|><|im_start|>user ${ user } <|im_end|><|im_start|>assistant"` ;
67
+ const prompt = `"!#<|im_start|>system ${ systemPrompt } <|im_end|><|im_start|>user ${ user } <|im_end|><|im_start|>assistant"` ;
69
68
70
69
const modelHandle = await LoadModelAsync ( model ) ;
71
70
const context = await CreateContextAsync ( modelHandle ) ;
72
- const result = await RunInferenceAsync ( modelHandle , context , prompt ) ;
71
+ const result : string = await RunInferenceAsync ( modelHandle , context , prompt ) ;
73
72
await ReleaseContextAsync ( context ) ;
74
73
await ReleaseModelAsync ( modelHandle ) ;
75
74
76
75
console . log ( "Result" , result ) ;
77
- expect ( result . length > 1 ) . toBeTruthy ( ) ;
76
+ assert . ok ( result . length > 1 ) ;
78
77
} ) ;
79
78
80
- test ( 'tokens work' , async ( ) => {
81
79
80
+ test ( 'tokens work' , async ( ) => {
82
81
const modelHandle = await LoadModelAsync ( model ) ;
83
82
const ctx = await CreateContextAsync ( modelHandle ) ;
84
83
85
- const eos = GetModelToken ( modelHandle , "EOS" ) ;
86
- const bos = GetModelToken ( modelHandle , "BOS" ) ;
87
- const eot = GetModelToken ( modelHandle , "EOT" ) ;
88
- const sep = GetModelToken ( modelHandle , "SEP" ) ;
89
- const cls = GetModelToken ( modelHandle , "CLS" ) ;
90
- const nl = GetModelToken ( modelHandle , "NL" ) ;
84
+ const eos : string = GetModelToken ( modelHandle , "EOS" ) ;
85
+ const bos : string = GetModelToken ( modelHandle , "BOS" ) ;
86
+ const eot : string = GetModelToken ( modelHandle , "EOT" ) ;
87
+ const sep : string = GetModelToken ( modelHandle , "SEP" ) ;
88
+ const cls : string = GetModelToken ( modelHandle , "CLS" ) ;
89
+ const nl : string = GetModelToken ( modelHandle , "NL" ) ;
91
90
92
91
console . log ( "EOS" , eos ) ;
93
92
console . log ( "BOS" , bos ) ;
@@ -99,17 +98,17 @@ describe('Node LLaMA Test Suite', () => {
99
98
await ReleaseContextAsync ( ctx ) ;
100
99
await ReleaseModelAsync ( modelHandle ) ;
101
100
102
- expect ( eos . length > 1 ) . toBeTruthy ( ) ;
103
- expect ( bos . length > 1 ) . toBeTruthy ( ) ;
104
- } )
101
+ assert . ok ( eos . length > 1 ) ;
102
+ assert . ok ( bos . length > 1 ) ;
103
+ } ) ;
105
104
106
105
test ( 'chat works' , async ( ) => {
107
106
SetLogLevel ( 4 ) ; // warn
108
107
109
108
const modelHandle = await LoadModelAsync ( model ) ;
110
109
const ctx = await CreateContextAsync ( modelHandle ) ;
111
110
112
- const chat = new ChatManager ( system_prompt ) ;
111
+ const chat = new ChatManager ( systemPrompt ) ;
113
112
114
113
let reply = "" ;
115
114
let prompt = chat . getNextPrompt ( "Hello, my name is Duck!" ) ;
@@ -128,8 +127,6 @@ describe('Node LLaMA Test Suite', () => {
128
127
await ReleaseContextAsync ( ctx ) ;
129
128
await ReleaseModelAsync ( modelHandle ) ;
130
129
131
- expect ( reply . includes ( "Duck" ) ) . toBeTruthy ( ) ;
130
+ assert . ok ( reply . includes ( "Duck" ) ) ;
132
131
} ) ;
133
-
134
-
135
132
} ) ;
0 commit comments