File size: 1,283 Bytes
dae4f76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { resolve } from 'node:path';
import { AutoTokenizer, PreTrainedModel, env } from '@huggingface/transformers';

if (!process.argv[2]) throw new Error('Usage: node example.mjs /path/to/model');
const directory = resolve(process.argv[2]);
env.allowRemoteModels = false;
const tokenizer = await AutoTokenizer.from_pretrained(directory, { local_files_only: true });
const model = await PreTrainedModel.from_pretrained(directory, {
  dtype: 'q8', local_files_only: true, session_options: { enableCpuMemArena: false }
});
try {
  const query = 'Suggest a vegetarian dinner.';
  const documents = ['The user is vegetarian.', 'The user plays the violin.'];
  for (const document of documents) {
    const inputs = tokenizer([query], { text_pair: [document], padding: true, truncation: false });
    const [batch, length] = inputs.input_ids.dims;
    if (batch !== 1 || length < 1 || length > 8192) throw new Error('Unsupported pair length');
    const { logits } = await model(inputs);
    const score = Number(logits.data[0]);
    if (logits.data.length !== 1 || logits.dims.some(n => n !== 1) || !Number.isFinite(score)) {
      throw new Error('Expected one finite raw logit');
    }
    console.log(JSON.stringify({ document, score }));
  }
} finally {
  await model.dispose();
}