-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.mjs
363 lines (330 loc) · 11.2 KB
/
index.mjs
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import env from "dotenv";
env.config();
import fs from "node:fs";
import https from "node:https";
import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
import { openaiCompletion } from "./lib/openai.mjs";
import { anthropicCompletion } from "./lib/anthropic.mjs";
import Koa from 'koa';
import serve from 'koa-static';
import Router from 'koa-router';
import yaml from 'yaml';
import JSON5 from 'json5'
import { highlight } from 'cli-highlight';
import chalk from 'chalk';
const shTheme = {
keyword: chalk.white.bold,
string: chalk.magenta.bold,
number: chalk.green.bold,
boolean: chalk.yellow,
null: chalk.grey,
operator: chalk.red.bold,
punctuation: chalk.grey,
comment: chalk.cyan.italic,
regexp: chalk.yellow.bold.italic,
addition: chalk.grey,
deletion: chalk.red.strikethrough
};
import { tools, history, debug, addToHistory, setResponseLimit, scanEmbeddedImages,
fiddleSrc, setPrompt, setRetrievedText, clean } from "./lib/tools.mjs";
import { colour } from "./lib/colour.mjs";
process.exitCode = 1;
const router = new Router();
const MODEL = process.env.MODEL || 'text-davinci-003';
const RESPONSE_LIMIT = parseInt(process.env.RESPONSE_LIMIT,10)||512;
const TEMPERATURE = parseFloat(process.env.TEMPERATURE) || 0.25;
setResponseLimit(RESPONSE_LIMIT);
const agent = new https.Agent({ keepAlive: true, keepAliveMsecs: 120000, scheduling: 'lifo', family: 0, noDelay: false, zonread: { buffer: Buffer.alloc(RESPONSE_LIMIT * 2.75) } });
let completion = "";
let partial = "";
let carry = "{";
let apiServer = "";
let booting = true;
const extractText = (chunk) => {
let json;
try {
json = JSON5.parse(`${carry}${chunk}}`);
carry = "{";
if (parseInt(debug(),10) >= 3) console.log(`${colour.cyan}${chunk}${colour.normal}`);
}
catch (ex) {
carry += chunk;
return;
}
let text;
if (json.data && json.data.choices) {
if (json.data.choices[0].delta) {
text = json.data.choices[0].delta.content;
}
else {
text = json.data.choices[0].text;
}
}
else if (json.data && json.data.stop&& json.data.completion) {
text = json.data.completion;
}
if (text) {
if (!booting) process.stdout.write(text);
completion += text;
}
return text;
}
const app = new Koa();
app.use(serve('.'));
router.get('/', '/', (ctx) => {
ctx.body = fiddleSrc;
});
router.get('/', '/temp.png', (ctx) => {
ctx.body = fs.readFileSync('./temp.png');
});
app
.use(router.routes())
.use(router.allowedMethods());
try {
app.listen(parseInt(process.env.PORT,10)||1337);
}
catch (ex) {
tools.disable.execute('savetext');
tools.disable.execute('savehtml');
tools.disable.execute('savecode');
tools.disable.execute('savecss');
}
let localHistory = [];
try {
localHistory = yaml.parse(fs.readFileSync('./history.yaml','utf8'));
}
catch (ex) {}
process.env.CHAT_QUERIES = localHistory.join(', ');
const rl = readline.createInterface({ input, output, history: localHistory, removeHistoryDuplicates: true });
const promptTemplate = fs.readFileSync("./prompt.txt", "utf8");
const mergeTemplate = fs.readFileSync("./merge.txt", "utf8");
const consume = async (value, chunkNo) => {
const chunks = `${Buffer.from(value).toString()}`.split('\n');
for (let chunk of chunks) {
if (booting && chunkNo % 20 === 1) process.stdout.write('.')
chunk = chunk.replaceAll('[DONE]', '["DONE"]');
extractText(chunk);
}
}
async function fetchStream(url, options) {
let chunkNo = 0;
let response = { ok: false, status: 418 }
try {
response = await fetch(url, options);
}
catch (ex) {
console.warn(`${colour.red}${ex.message}${colour.normal}`);
}
if (response.status !== 200) {
process.stdout.write(`${colour.red}`);
let text = await response.text();
try {
let json = JSON5.parse(partial+text);
if (json.error && json.error.message) {
completion += json.error.message;
return text;
}
else {
partial = "";
}
}
catch (ex) {
partial = text;
}
return text;
}
const reader = response.body.getReader();
const stream = new ReadableStream({
start(controller) {
function push() {
reader.read().then(({ done, value }) => {
if (done) {
controller.close();
return;
}
if (value) consume(value, ++chunkNo);
controller.enqueue(value);
push();
});
}
push();
},
error (err) {
console.log(`${colour.red}(${err.message})${colour.normal}`);
},
end () {
if (debug()) console.log(`${colour.cyan}(End of stream)${colour.normal}`);
}
});
const newResponse = new Response(stream);
const text = await newResponse.text();
return text;
}
const getCompletion = async (prompt) => {
process.stdout.write(colour.grey);
completion = "";
if (process.env.PROVIDER === "anthropic") {
await anthropicCompletion(prompt, fetchStream, agent);
}
else {
await openaiCompletion(prompt, fetchStream, agent);
}
if (!completion.endsWith('\n\n')) {
completion += '\n';
}
return clean(completion);
}
const answerQuestion = async (question) => {
// construct the prompt, with our question and the tools that the chain can use
let prompt = promptTemplate.replace("${question}", question).replace(
"${tools}",
Object.keys(tools)
.map((toolname) => `${toolname}: ${tools[toolname].description}`)
.join("\n"))
.replace("${toolList}", Object.keys(tools).join(", "))
.replace("${user}", process.env.USER)
.replace('${language}',process.env.LANG);
process.env.PROMPT = prompt;
process.env.CHAT_PROMPT = prompt;
if (process.env.PROMPT_OVERRIDE) {
prompt = process.env.PROMPT_OVERRIDE.replaceAll("${question}", question);
}
// allow the LLM to iterate until it finds a final answer
while (true) {
const response = await getCompletion(prompt);
// add this to the prompt
prompt += response;
// display any embedded image URLs
scanEmbeddedImages(response);
if (response.indexOf('Action:') >= 0) {
const action = response.split('Action:').pop().split('\n')[0].toLowerCase().trim();
if (action && tools[action]) {
// execute the action specified by the LLM
let actionInput = response.split('Action Input:').pop().trim();
if (actionInput.indexOf("```") >= 0) {
actionInput = actionInput.replace(/```.+/gi, "```");
actionInput = actionInput.split("```")[1];
}
if (actionInput.indexOf(')()') >= 0) {
actionInput = actionInput.split(')()')[0]+')()'.trim();
}
if (actionInput.indexOf('```') >= 0) {
actionInput = actionInput.split('```\n')[0].trim();
}
else if (actionInput.startsWith('|')) {
actionInput = actionInput.substring(1).trim();
}
else {
actionInput = actionInput.split('\n\n')[0].trim();
}
if (actionInput && !actionInput.startsWith('[')) {
setPrompt(prompt);
if (process.env.SYNTAX) {
actionInput = highlight(actionInput, { language: 'javascript', theme: shTheme, ignoreIllegals: true });
}
if (!booting) console.log(`${colour.cyan}\nCalling '${action}' with "${actionInput}"${colour.normal}`);
const result = await tools[action].execute(clean(actionInput));
prompt += `Observation: ${result||'None'}\n`;
}
}
} else {
if (response.indexOf('Answer:') >= 0) {
let answer = response.split('Answer:').pop();
if (answer) return answer;
}
let answer = response.split('Observation:').pop().trim();
return answer||'No answer'; // sometimes we don't get a "Final Answer"
}
}
};
// merge the chat history with a new question
const mergeHistory = async (question, history) => {
const prompt = mergeTemplate
.replace("${question}", question)
.replace("${history}", history);
return await getCompletion(prompt);
};
process.stdout.write(`${colour.cyan}Initialising built-in tools: `);
let allOk = true;
let first = true;
Object.keys(tools).sort().map((toolname) => {
if (!first) {
process.stdout.write(', ');
}
process.stdout.write(`${colour.magenta}${toolname}`);
const result = (async () => await tools[toolname].init())();
first = false;
if (!result) allOk = false;
return true;
});
console.log(colour.normal);
if (localHistory.length && process.env.SEED_QUERIES) {
booting = true;
const query = `Can you get the CHAT_QUERIES so you can remember the previous questions I have asked? You do not need to list them.`;
process.stdout.write(`${colour.cyan}Please wait, bootstrapping conversation${colour.magenta}`);
const response = await answerQuestion(query);
}
rl.on('history',(history) => {
fs.writeFileSync('./history.yaml',yaml.stringify(history),'utf8');
});
if (!allOk) {
console.log(`\n${colour.cyan}[2] some tools were disabled because of missing API keys or node.js features.${colour.normal}`);
}
// main loop - answer the user's questions
while (true) {
booting = false;
debugger;
let question = await rl.question(`${colour.red}How can I help? >${colour.grey} `);
let questionLC = question.trim().toLowerCase();
questionLC = question.split('please').join('').trim();
let verb = questionLC.split(' ')[0].toLowerCase().trim();
if (tools[verb] || (verb.startsWith(':') && tools[verb.replace(':','')])) {
verb = verb.replace(':','');
console.log(`${colour.magenta}${await tools[verb].execute(clean(questionLC.slice(verb.length+1)))}${colour.normal}`);
question = '';
}
else if (verb.startsWith(':')) {
if (question.startsWith(':q') || question.startsWith(':wq')) {
console.log(`\nSaving history and exiting with 0.`);
process.exit(0);
}
else if (question.startsWith(':syntax')) {
const value = question.split(' ')[1].trim().toLowerCase();
process.env.SYNTAX = (value === 'on' || value === 'true' || value === '1' || value === 'yes') ? 1 : 0;
question = '';
}
else if (question.startsWith(':help')) {
question = "How should a novice user get the best out of this chat experience?";
}
else if (question === (':set')) {
console.log(yaml.stringify(process.env));
question = '';
}
else if (question.startsWith(':set ')) {
question = question.replace('=',' ');
question = question.split(' ').join(' ');
let words = question.split(' ');
let key = words[1]
key = key.toUpperCase();
words.splice(0, 2);
const value = words.join(' ');
process.env[key] = value;
question = '';
}
}
let answer = '';
if (question) answer = await answerQuestion(question);
while (process.env.SYNTAX === '1' && answer.indexOf('```\n') >= 0) {
const sections = answer.split('```');
const preamble = sections[0];
const language = sections[1].split('\n')[0].trim();
const code = sections[1].replace(language + '\n', '');
const tail = sections[2];
answer = preamble + highlight(code, { language, theme: shTheme, ignoreIllegals: true }) + tail;
}
if (question || answer) {
console.log(`\n${colour.green}${answer.trimStart()}${colour.normal}`);
addToHistory(`Q:${question}\nA:${answer}\n`);
}
}