|
| 1 | +import * as readline from 'node:readline' |
| 2 | +import { stdin, stdout } from 'node:process' |
| 3 | + |
| 4 | +const API_URL = 'http://127.0.0.1:8080' |
| 5 | + |
| 6 | +const chat = [ |
| 7 | + { |
| 8 | + human: "Hello, Assistant.", |
| 9 | + assistant: "Hello. How may I help you today?" |
| 10 | + }, |
| 11 | + { |
| 12 | + human: "Please tell me the largest city in Europe.", |
| 13 | + assistant: "Sure. The largest city in Europe is Moscow, the capital of Russia." |
| 14 | + }, |
| 15 | +] |
| 16 | + |
| 17 | +const instruction = `A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.` |
| 18 | + |
| 19 | +function format_prompt(question) { |
| 20 | + return `${instruction}\n${ |
| 21 | + chat.map(m =>`### Human: ${m.human}\n### Assistant: ${m.assistant}`).join("\n") |
| 22 | + }\n### Human: ${question}\n### Assistant:` |
| 23 | +} |
| 24 | + |
| 25 | +async function tokenize(content) { |
| 26 | + const result = await fetch(`${API_URL}/tokenize`, { |
| 27 | + method: 'POST', |
| 28 | + body: JSON.stringify({ content }) |
| 29 | + }) |
| 30 | + |
| 31 | + if (!result.ok) { |
| 32 | + return [] |
| 33 | + } |
| 34 | + |
| 35 | + return await result.json().tokens |
| 36 | +} |
| 37 | + |
| 38 | +const n_keep = await tokenize(instruction).length |
| 39 | + |
| 40 | +async function chat_completion(question) { |
| 41 | + const result = await fetch(`${API_URL}/completion`, { |
| 42 | + method: 'POST', |
| 43 | + body: JSON.stringify({ |
| 44 | + prompt: format_prompt(question), |
| 45 | + temperature: 0.2, |
| 46 | + top_k: 40, |
| 47 | + top_p: 0.9, |
| 48 | + n_keep: n_keep, |
| 49 | + n_predict: 256, |
| 50 | + stop: ["\n### Human:"], // stop completion after generating this |
| 51 | + stream: true, |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + if (!result.ok) { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + let answer = '' |
| 60 | + |
| 61 | + for await (var chunk of result.body) { |
| 62 | + const t = Buffer.from(chunk).toString('utf8') |
| 63 | + if (t.startsWith('data: ')) { |
| 64 | + const message = JSON.parse(t.substring(6)) |
| 65 | + answer += message.content |
| 66 | + process.stdout.write(message.content) |
| 67 | + if (message.stop) { |
| 68 | + if (message.truncated) { |
| 69 | + chat.shift() |
| 70 | + } |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + process.stdout.write('\n') |
| 77 | + chat.push({ human: question, assistant: answer.trimStart() }) |
| 78 | +} |
| 79 | + |
| 80 | +const rl = readline.createInterface({ input: stdin, output: stdout }); |
| 81 | + |
| 82 | +const readlineQuestion = (rl, query, options) => new Promise((resolve, reject) => { |
| 83 | + rl.question(query, options, resolve) |
| 84 | +}); |
| 85 | + |
| 86 | +while(true) { |
| 87 | + const question = await readlineQuestion(rl, '> ') |
| 88 | + await chat_completion(question) |
| 89 | +} |
0 commit comments