|
1 | | -import { EmbeddingResultType, LLama } from "../index"; |
| 1 | +import { LLama } from "../index"; |
2 | 2 | import path from "path"; |
3 | 3 | import fs from "fs"; |
4 | 4 |
|
5 | 5 | const model = path.resolve(process.cwd(), "../../ggml-alpaca-7b-q4.bin"); |
6 | 6 |
|
7 | 7 | LLama.enableLogger(); |
8 | 8 |
|
9 | | -const llama = LLama.create({ |
10 | | - path: model, |
11 | | - numCtxTokens: 128, |
12 | | -}); |
13 | | - |
14 | | -const getWordEmbeddings = (prompt: string, file: string) => { |
15 | | - llama.getWordEmbeddings( |
16 | | - { |
17 | | - prompt, |
18 | | - numPredict: 128, |
19 | | - temp: 0.2, |
20 | | - topP: 1, |
21 | | - topK: 40, |
22 | | - repeatPenalty: 1, |
23 | | - repeatLastN: 64, |
24 | | - seed: 0, |
25 | | - }, |
26 | | - (response) => { |
27 | | - switch (response.type) { |
28 | | - case EmbeddingResultType.Data: { |
29 | | - fs.writeFileSync( |
30 | | - path.resolve(process.cwd(), file), |
31 | | - JSON.stringify(response.data) |
32 | | - ); |
33 | | - break; |
34 | | - } |
35 | | - case EmbeddingResultType.Error: { |
36 | | - console.log(response); |
37 | | - break; |
38 | | - } |
39 | | - } |
40 | | - } |
| 9 | +const getWordEmbeddings = async ( |
| 10 | + llama: LLama, |
| 11 | + prompt: string, |
| 12 | + file: string |
| 13 | +) => { |
| 14 | + const response = await llama.getWordEmbeddings({ |
| 15 | + prompt, |
| 16 | + numPredict: 128, |
| 17 | + temp: 0.2, |
| 18 | + topP: 1, |
| 19 | + topK: 40, |
| 20 | + repeatPenalty: 1, |
| 21 | + repeatLastN: 64, |
| 22 | + seed: 0, |
| 23 | + }); |
| 24 | + |
| 25 | + fs.writeFileSync( |
| 26 | + path.resolve(process.cwd(), file), |
| 27 | + JSON.stringify(response) |
41 | 28 | ); |
42 | 29 | }; |
43 | 30 |
|
44 | | -const dog1 = `My favourite animal is the dog`; |
45 | | -getWordEmbeddings(dog1, "./example/semantic-compare/dog1.json"); |
| 31 | +const run = async () => { |
| 32 | + const llama = await LLama.create({ |
| 33 | + path: model, |
| 34 | + numCtxTokens: 128, |
| 35 | + }); |
| 36 | + |
| 37 | + const dog1 = `My favourite animal is the dog`; |
| 38 | + getWordEmbeddings(llama, dog1, "./example/semantic-compare/dog1.json"); |
46 | 39 |
|
47 | | -const dog2 = `I have just adopted a cute dog`; |
48 | | -getWordEmbeddings(dog2, "./example/semantic-compare/dog2.json"); |
| 40 | + const dog2 = `I have just adopted a cute dog`; |
| 41 | + getWordEmbeddings(llama, dog2, "./example/semantic-compare/dog2.json"); |
| 42 | + |
| 43 | + const cat1 = `My favourite animal is the cat`; |
| 44 | + getWordEmbeddings(llama, cat1, "./example/semantic-compare/cat1.json"); |
| 45 | +}; |
49 | 46 |
|
50 | | -const cat1 = `My favourite animal is the cat`; |
51 | | -getWordEmbeddings(cat1, "./example/semantic-compare/cat1.json"); |
| 47 | +run(); |
0 commit comments