-
Notifications
You must be signed in to change notification settings - Fork 11
/
mod.ts
829 lines (727 loc) · 20.5 KB
/
mod.ts
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
import { parse } from "https://deno.land/std@0.181.0/flags/mod.ts";
import { aiConfig, getChatResponse_stream, getImageResponse, StreamResponse, ChatCompletionRequest, CreateImageRequest, Message, EmbeddingRequest, getEmbeddingResponse } from "./lib/ai.ts";
import {
AUTO_UPDATE_PROBABILITY,
Config,
DEFAULT_CONFIG,
getChat,
getHistory,
getOrCreateConfigFile,
loadConfig,
saveConfig,
writeChat,
} from "./lib/data.ts";
import { printCtrlSequence, pullCharacter } from "./lib/lib.ts";
import {
setCodeCmdParamsForChat,
setExecutableCmdParamsForChat,
setLanguageForChat,
} from "./lib/prompts.ts";
import { exec as shExec } from "./lib/shell.ts";
import { install, isLatestVersion } from "./lib/update.ts";
const args = parse(Deno.args, {
boolean: [
// Instructions for this script
"help",
// Runs through persistent configuration
"config",
// Continuation (continue last conversation)
"continue",
"cont",
"c",
// Exec (run as a shell command)
"exec",
"x",
// Retry (re-generate the last assistant message)
"retry",
"r",
// Rewrite (reword the last user message)
"rewrite",
"rw",
"w",
// Pop (remove the last message in the conversation)
"pop",
// Print (print the last message in the conversation)
"print",
"p",
// Slice (remove the first message in the conversation)
"slice",
"s",
// History (list chat history)
"history",
"h",
"desc",
// Dump (dump the entire chat history)
"dump",
"d",
// Fast (use GPT-3.5-turbo)
"fast",
"f",
// Update (update ShellGPT)
"update",
"u",
// REPL (continuous conversation)
"repl",
// Code mode (output code instead of text)
"code",
// Debug (print debug info)
"debug",
// System (primary input treated as system prompt)
"as-system",
"as-sys",
// Image
"image",
"img",
"i",
// Embedding
"embed"
],
string: [
// Name (select a conversation from history to use)
"name",
"n",
// System (set a system prompt / context)
"system",
"sys",
// Temperature (creativity)
"temperature",
"temp",
"t",
// Limit max tokens to output
"max_tokens",
"max",
// WPM (words per minute, speed of typing output)
"wpm",
// Model (manually use a different OpenAI model)
"model",
"m",
// Language to use for programming or text generation
'lang',
// Dimensions for embedding
"dims",
],
});
// --- Parse Args ---
const DEFAULT_MODEL = "gpt-4-turbo-preview";
const DEFAULT_WPM = 1200;
const AVG_CHARS_PER_WORD = 4.8;
const help = args.help;
const name = args.name || args.n;
const fast = args.f || args.fast;
const updateConfig = args.config;
const update = args.update || args.u;
const model = fast ? "gpt-3.5-turbo-0125" as const : (args.model ?? args.m);
const temp = args.t || args.temp || args.temperature;
const exec = args.x || args.exec;
const retry = args.r || args.retry;
const rewrite = args.w || args.rw || args.rewrite;
const pop = args.pop;
const print = args.p || args.print;
const slice = args.s || args.slice;
const dump = args.dump || args.d;
const cont = slice || pop || retry || rewrite || print || dump ||
(Boolean(args.c || args.cont || args.continue));
const history = args.h || args.history;
const descending = args.desc
const system = args.sys || args.system;
const maxTokens = args.max || args.max_tokens;
const readStdin = args._.at(0) === "-" || args._.at(-1) === "-";
const repl = args.repl;
const code = args.code;
const debug = args.debug;
const asSys = args["as-sys"] || args["as-system"];
const bumpSys = args["as-sys"] || args["as-system"];
const lang = args.lang
const img = args.img || args.i
const embed = args.embed
const dims = args.dims
// --- END Parse Args ---
let config = await loadConfig();
const gptCommand = config?.command ?? DEFAULT_CONFIG.command;
const wpm = args.wpm ? Number(args.wpm) : config?.wpm || DEFAULT_WPM;
const configWasEmpty = Object.keys(config ?? {}).length === 0;
const messageWasEmpty = args._.length === 0;
const shouldAutoUpdate = config?.autoUpdate !== "never" &&
Math.random() < AUTO_UPDATE_PROBABILITY;
const messageContent = args._.join(" ");
const message: Message = {
role: asSys ? "system" : "user",
content: messageContent,
};
const stock: ChatCompletionRequest = {
model: model ?? config?.model ?? DEFAULT_MODEL,
messages: [],
};
const req: ChatCompletionRequest = (cont || name)
? ((await getChat(name)) ?? stock)
: stock;
const helpMessage = `
Usage: ${gptCommand} [OPTIONS] [MESSAGE]
Options:
--help Show this help message
--config Runs configuration
--update Updates ShellGPT
--debug Output debug information with each request
- Read message from stdin
-c, --continue Continue the last conversation
-x, --exec Run the generated response as a shell command
-r, --retry Re-generate the last assistant message
-w, --rewrite Reword the last user message
-s, --slice Remove the first message in the conversation
--pop Remove the last message in the conversation
-p, --print Print the last message in the conversation
-h, --history List chat history
--desc List chat history in descending order. Defaults to ascending order.
-d, --dump Dump the entire chat history
-f, --fast Use GPT-3.5-turbo model (faster)
--repl Start a continuous conversation
--code Output code instead of chat text
--lang LANG Set the language for programming or text generation
--bump-sys Bump the most recent system prompt/context to front
-i, --img Output an image instead of text
--embed Output a vector embedding of the input
--dims Set the dimensions of the embedding
-n, --name NAME Select a conversation from history to use
--sys[tem] Set a system prompt/context
--as-sys[tem] Treat the primary input as a system prompt
-t, --temp Set the creativity temperature
--wpm WPM Set the words per minute
--max MAX_TOKENS Set the maximum number of tokens
-m, --model MODEL Manually use a different OpenAI model
Examples:
${gptCommand} "What is the capital of France?"
${gptCommand} -c "Tell me more about Paris."
${gptCommand} -x "Create a new file called 'test.txt' and write 'Hello World!' to it."
cat test.txt | ${gptCommand} - "Invert the capitalization of this text."
`;
// --- HANDLE ARGS ---
if (debug) {
aiConfig.debug = 'verbose';
}
if (embed) {
const newReq: EmbeddingRequest = {
input: messageContent,
model: model ?? 'text-embedding-3-small',
...(dims ? {dimensions: Number(dims)} : {})
};
const response = await getEmbeddingResponse(newReq);
if (response) {
console.log(JSON.stringify(response));
} else {
console.log("(Failed to generate embedding)");
}
Deno.exit();
}
if (img) {
const newReq: CreateImageRequest = {
quality: "hd",
response_format: "url",
size: "1024x1024",
prompt: messageContent ?? img,
model: "dall-e-3",
};
const response = await getImageResponse(newReq);
if (response) {
console.log(response);
} else {
console.log("(Failed to generate image)");
}
Deno.exit();
}
if (pop) {
const lastMessage = req.messages.pop();
if (lastMessage) {
console.log(`(Removing last message from ${lastMessage!.role})`);
await writeChat(req, false);
} else {
console.log("(Found no messages)");
}
Deno.exit();
}
if (slice) {
if (req.messages.length > 1) {
console.log(`(Removing first message)`);
req.messages = req.messages.slice(1);
await writeChat(req, false);
} else {
console.log("(Found no messages)");
}
Deno.exit();
}
if (update || shouldAutoUpdate) {
const isLatest = await isLatestVersion();
if (shouldAutoUpdate && isLatest) {
// do nothing in this case
} else {
const newConfig = config ?? DEFAULT_CONFIG;
const updateInfo = await install(newConfig, true);
saveConfig(newConfig);
if (updateInfo.result === "updated") {
console.log("\n%c> Successfully updated!", "font-weight: bold;");
Deno.exit();
} else if (updateInfo.result === "error") {
console.log(
"\n%c> Encountered error while updating.",
"font-weight: bold;",
);
Deno.exit();
} else {
console.log("\n%c> No updates found.", "font-weight: bold;");
Deno.exit();
}
}
}
if (updateConfig || configWasEmpty) {
if (configWasEmpty) {
console.log("(No config found. Running initial setup...)\n");
}
const newConfig: Config = {
...(config ?? DEFAULT_CONFIG),
};
const updateInfo = await install(newConfig, true);
if (updateInfo.result === "updated") {
console.log(
"\n%c> Successfully updated! Please re-run `gpt --config`",
"font-weight: bold;",
);
Deno.exit();
}
const currentModel = config?.model || DEFAULT_MODEL;
console.log(
"\n%c> Which OpenAI ChatGPT model would you like to use?",
"font-weight: bold;",
);
console.log();
const model = window.prompt(
`You can enter "gpt-4" or "gpt-3.5-turbo". (Leave empty for ${currentModel}):`,
);
if (model) {
newConfig.model = model ?? currentModel;
}
console.log(
"\n%c> Would you like to set a custom system prompt to attach to each session?",
"font-weight: bold;",
);
if (config?.systemPrompt) {
console.log(`Current system prompt: ${config.systemPrompt}`);
console.log();
const newPrompt = window.prompt(
`Enter new prompt (empty to keep, "clear" to remove)`,
);
if (newPrompt === "clear") {
newConfig.systemPrompt = undefined;
} else if (newPrompt) {
newConfig.systemPrompt = newPrompt;
} else {
newConfig.systemPrompt = config.systemPrompt;
}
} else {
console.log();
const newPrompt = window.prompt(
`Press enter to skip, or type a new prompt:`,
);
if (newPrompt) {
newConfig.systemPrompt = newPrompt;
}
}
if (!Deno.env.get("OPENAI_API_KEY")) {
console.log(
`\n%c> No API key detected. It's recommended to configure using the OPENAI_API_KEY environment variable, but you can also paste one here.`,
"font-weight: bold;",
);
console.log(
"\n%c> You can get an API key here: %chttps://platform.openai.com/account/api-keys",
"font-weight: bold;",
"font-weight: normal;",
);
if (config?.openAiApiKey) {
console.log(`Current API key: ${config.openAiApiKey}`);
}
console.log();
const newPrompt = window.prompt(
config?.openAiApiKey
? `Enter new API key (empty to keep, "clear" to remove)`
: `Enter API key (empty to skip)`,
);
if (newPrompt === "clear") {
newConfig.openAiApiKey = undefined;
} else if (newPrompt) {
newConfig.openAiApiKey = String(newPrompt);
} else {
// do nothing
}
}
try {
await saveConfig(newConfig);
console.log(
`%cUpdated config file: %c${await getOrCreateConfigFile()}`,
"color: green; font-weight: bold;",
"color: green;",
);
if (updateConfig) {
// Exit only if the user manually requested config update
Deno.exit();
} else {
// Otherwise, continue with the rest of the script
config = newConfig;
req.model = model ?? config.model ?? DEFAULT_MODEL;
}
} catch (e) {
console.error(
`Failed to update config file at: ${await getOrCreateConfigFile()}`,
);
console.error(e);
Deno.exit(1);
}
}
if (help) {
console.log(helpMessage);
Deno.exit();
}
if (readStdin) {
message.content += "\n";
const decoder = new TextDecoder();
for await (const chunk of Deno.stdin.readable) {
const textChunk = decoder.decode(chunk);
message.content += textChunk;
}
}
if (print) {
// print last message
const lastMessage = req.messages.pop();
if (lastMessage) {
console.log(lastMessage.content);
} else {
console.log("(Found no messages)");
}
Deno.exit();
}
if (dump) {
for (const message of req.messages) {
if (message.role === "user") {
console.log("---\n");
console.log(`${message.content}\n`);
console.log("---\n");
} else {
console.log(`${message.content}\n`);
}
}
Deno.exit();
}
if (system || (config?.systemPrompt && !cont)) {
// Add system prompt if set for this message, or is a new conversation
req.messages.push({
role: "system",
content: system ?? config!.systemPrompt!,
});
}
let empty = false;
if (!message.content && !retry && !pop && !history) {
empty = true;
if (!repl) {
console.log("(No message passed)\n");
}
}
if (temp) {
req.temperature = Number(temp);
}
if (maxTokens) {
req.max_tokens = Number(maxTokens);
}
if (model) {
// @ts-ignore Allow any string as model for now
req.model = model;
}
if (retry) {
// remove last assistant message
req.messages.pop();
}
if (rewrite) {
// remove last assistant AND user messages
req.messages.pop();
req.messages.pop();
}
if (!retry && !empty) {
req.messages.push(message);
}
if (exec) {
setExecutableCmdParamsForChat(req);
} else if (code) {
setCodeCmdParamsForChat(req, lang);
} else if (lang) {
setLanguageForChat(req, lang)
}
if (history) {
const files = await getHistory(descending);
for (const file of files) {
const hasSnippet = file.snippet && file.snippet.length > 0;
if (hasSnippet) {
console.log(
`%c${file.name}\t\t%c${file.snippet}`,
"color: blue",
"color: gray",
);
} else {
console.log(`%c${file.name}`, "color: blue");
}
}
Deno.exit();
}
if (bumpSys) {
let mostRecentSystemIndex = -1;
for (let i = req.messages.length - 1; i > 0; i--) {
if (req.messages[i].role === "system") {
mostRecentSystemIndex = i;
break;
}
}
// splice+push it
if (mostRecentSystemIndex !== -1) {
const systemMessage = req.messages.splice(mostRecentSystemIndex, 1)[0];
req.messages.push(systemMessage);
}
}
// --- END HANDLE ARGS ---
let streamResponse: AsyncIterableIterator<StreamResponse> | null = null;
const doStreamResponse = async () => {
try {
streamResponse = await getChatResponse_stream(req);
} catch (e) {
console.error("Unhandled error", e);
Deno.exit();
}
};
// STATE
type DoneType = "none" | "with_net" | "with_write" | "with_print";
let done: DoneType = "none"; // none -> with_net -> with_write -> with_print -> [done]
let responseStr = "";
let intermediateStr = "";
let printStr = "";
if (repl && messageWasEmpty) {
done = "with_net";
} else {
await doStreamResponse();
}
let nextBuffer = "";
async function readInput() {
let strBuffer = "";
let arrayBuffer = new Uint8Array(1); // Buffer to hold byte data from stdin
let done = false;
let newline = false;
const updateFn = async () => {
// read from stdin
const n = await Deno.stdin.read(arrayBuffer)
let curBuffer = ""
const wasDone = done
if (n != null && n > 0) {
const text = new TextDecoder().decode(arrayBuffer)
for (const char of text) {
const code = char.charCodeAt(0);
if (code === 13 || code === 10) {
// carriage return or newline
newline = true
curBuffer += "\n";
await Deno.stdout.write(new TextEncoder().encode("\n"));
}
else if (code === 127) {
// backspace
const wasEmpty = strBuffer.length === 0;
curBuffer = curBuffer.slice(0, -1);
strBuffer = strBuffer.slice(0, -1);
if (!wasEmpty) {
await Deno.stdout.write(new TextEncoder().encode("\b \b"));
}
}
else {
// any other character
// console.warn("Z", char)
await Deno.stdout.write(arrayBuffer);
curBuffer += char;
newline = false;
}
}
if (wasDone) {
// We already finished reading this stream, so push to next reader
nextBuffer = curBuffer
}
else {
strBuffer += curBuffer
}
}
else {
done = true;
newline = true;
return;
}
};
nextBuffer = ""
Deno.stdin.setRaw(false)
Deno.stdin.setRaw(true, { cbreak: true })
await new Promise((resolve) => {
(
async () => {
strBuffer = ""
arrayBuffer = new Uint8Array(1)
while (!done) {
if (nextBuffer) {
// HACK: there may be another stdin reader which we want to push results from
// Use this instead of AbortController for now
strBuffer = `${nextBuffer}${strBuffer}`
nextBuffer = ""
}
const wasNewline = newline;
if (wasNewline) {
// wait for a moment for typing
setTimeout(() => {
if (!newline) {
return
}
done = true
resolve(true)
}, 250)
}
await updateFn();
if (wasNewline && newline) {
done = true;
}
}
resolve(true)
}
)()
})
Deno.stdin.setRaw(false)
return strBuffer
}
// Done, write it out
const flush = async () => {
streamResponse = null;
const text = new TextEncoder().encode("\n");
await Deno.stdout.write(text);
if (responseStr) {
req.messages.push({
content: responseStr,
role: "assistant",
});
}
await writeChat(req, cont ? false : (name || true));
if (exec && !readStdin) {
console.log(
`\n%cAre you SURE you wish to run the above command? (y/N):`,
"color: red; font-weight: bold;",
);
const promptValue = prompt("> ");
if (["y", "yes"].includes(promptValue?.toLowerCase() ?? "")) {
// do it
await shExec(responseStr);
} else {
console.log("(will not exec command)");
}
} else if (exec && readStdin) {
console.log("(exec not currently supported when reading from stdin)");
}
if (repl) {
responseStr = "";
await printCtrlSequence("blue");
await printCtrlSequence("bold");
await Deno.stdout.write(new TextEncoder().encode(`\n> `));
const promptValue = await readInput();
// print reset ctrl sequence
await printCtrlSequence("reset");
if (promptValue) {
// do it
req.messages.push({
content: promptValue,
role: "user",
});
done = "none";
await doStreamResponse();
}
} else {
Deno.exit();
}
};
// Push strings
{
(async () => {
while (true) {
if (done !== "none" || !streamResponse) {
// Spin wait
await new Promise((resolve) => setTimeout(resolve));
continue;
}
try {
const iterator = streamResponse as AsyncIterableIterator<StreamResponse>;
while (true) {
const response = await iterator.next();
if (response.value.delta) {
responseStr += response.value.delta;
if (wpm >= 0) {
// Write to a buffer
intermediateStr += response.value.delta;
}
else {
// Print immediately
printStr += response.value.delta;
}
}
if (response.done) {
break;
}
}
} catch (e) {
console.error("Unhandled error", e);
Deno.exit(1);
}
if (wpm >= 0) {
done = "with_net";
} else {
done = "with_write";
}
}
})();
}
if (wpm >= 0) {
// Writes to a buffer so we can print at a desired WPM
let startTime = -1;
const targetCps = (AVG_CHARS_PER_WORD * wpm) / 60;
(async () => {
while (true) {
if (done === "with_write" || (done as DoneType) === "with_print") {
// Spin wait
await new Promise((resolve) => setTimeout(resolve));
continue;
}
// Go through characters one-by-one and write
while ((done as DoneType) !== "with_net" || intermediateStr.length > 0) {
if (startTime < 0) {
startTime = Date.now();
}
const { char, str } = pullCharacter(intermediateStr);
printStr += char;
intermediateStr = str;
await new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, 1000 / targetCps);
});
}
done = "with_write";
}
})();
}
// Pull strings to write as soon as available
{
const consumeFn = async () => {
const latest = printStr;
printStr = "";
if (!latest && done === "with_write") {
await flush();
}
if (latest) {
const text = new TextEncoder().encode(latest);
await Deno.stdout.write(text);
}
setTimeout(consumeFn);
};
setTimeout(consumeFn);
}