-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalan.mjs
1270 lines (1202 loc) · 49.9 KB
/
alan.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
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { BASE64, BUFFER, DATAURL, MIME_BINARY, STREAM, convert } from './storage.mjs';
import { create as createUoid } from './uoid.mjs';
import { createWavHeader } from './media.mjs';
import { end, loop } from './event.mjs';
import { fileTypeFromBuffer } from 'file-type';
import {
base64Encode, ensureArray, ensureString, extract, ignoreErrFunc,
log as _log, need, parseJson, renderText as _renderText, throwError,
} from './utilitas.mjs';
const _NEED = [
'@anthropic-ai/sdk', '@google/generative-ai', 'js-tiktoken', 'ollama',
'OpenAI',
];
const [
OPENAI, GEMINI, CHATGPT, OPENAI_EMBEDDING, GEMINI_EMEDDING, OPENAI_TRAINING,
OLLAMA, CLAUDE, GPT_4O_MINI, GPT_4O, GPT_O1, GPT_O3_MINI, GEMINI_20_FLASH,
GEMINI_20_FLASH_THINKING, GEMINI_20_PRO, NOVA, EMBEDDING_001, DEEPSEEK_R1,
DEEPSEEK_R1_32B, MD_CODE, CHATGPT_REASONING, TEXT_EMBEDDING_3_SMALL,
TEXT_EMBEDDING_3_LARGE, CLAUDE_35_SONNET, CLAUDE_35_HAIKU, AUDIO, WAV,
CHATGPT_MINI, ATTACHMENTS, CHAT, OPENAI_VOICE, MEDIUM, LOW, HIGH,
GPT_REASONING_EFFORT, THINK, THINK_STR, THINK_END,
] = [
'OPENAI', 'GEMINI', 'CHATGPT', 'OPENAI_EMBEDDING', 'GEMINI_EMEDDING',
'OPENAI_TRAINING', 'OLLAMA', 'CLAUDE', 'gpt-4o-mini', 'gpt-4o', 'o1',
'o3-mini', 'gemini-2.0-flash', 'gemini-2.0-flash-thinking-exp',
'gemini-2.0-pro-exp', 'nova', 'embedding-001', 'deepseek-r1',
'deepseek-r1:32b', '```', 'CHATGPT_REASONING', 'text-embedding-3-small',
'text-embedding-3-large', 'claude-3-5-sonnet-latest',
'claude-3-5-haiku-latest', 'audio', 'wav', 'CHATGPT_MINI',
'[ATTACHMENTS]', 'CHAT', 'OPENAI_VOICE', 'medium', 'low', 'high',
'medium', 'think', '<think>', '</think>',
];
const [
png, jpeg, mov, mpeg, mp4, mpg, avi, wmv, mpegps, flv, gif, webp, pdf, aac,
flac, mp3, m4a, mpga, opus, pcm, wav, webm, tgpp, mimeJson, mimeText, pcm16,
ogg,
] = [
'image/png', 'image/jpeg', 'video/mov', 'video/mpeg', 'video/mp4',
'video/mpg', 'video/avi', 'video/wmv', 'video/mpegps', 'video/x-flv',
'image/gif', 'image/webp', 'application/pdf', 'audio/aac', 'audio/flac',
'audio/mp3', 'audio/m4a', 'audio/mpga', 'audio/opus', 'audio/pcm',
'audio/wav', 'audio/webm', 'video/3gpp', 'application/json',
'text/plain', 'audio/x-wav', 'audio/ogg',
];
const [tool, provider, messages, text] = [
type => ({ type }), provider => ({ provider }),
messages => ({ messages }), text => ({ text }),
];
const [name, user, system, assistant, MODEL, JSON_OBJECT]
= ['Alan', 'user', 'system', 'assistant', 'model', 'json_object'];
const [CODE_INTERPRETER, RETRIEVAL, FUNCTION]
= ['code_interpreter', 'retrieval', 'function'].map(tool);
const [NOT_INIT, INVALID_FILE]
= ['AI engine has not been initialized.', 'Invalid file data.'];
const [silent, instructions] = [true, 'You are a helpful assistant.'];
const chatConfig
= { sessions: new Map(), engines: {}, systemPrompt: instructions };
const [tokenSafeRatio, GPT_QUERY_LIMIT, minsOfDay] = [1.1, 100, 60 * 24];
const tokenSafe = count => Math.ceil(count * tokenSafeRatio);
const clients = {};
const size8k = 7680 * 4320;
const LOG = { log: true };
const OPENAI_BASE_URL = 'https://api.openai.com/v1';
const sessionType = `${name.toUpperCase()}-SESSION`;
const unifyProvider = options => unifyType(options?.provider, 'AI provider');
const unifyEngine = options => unifyType(options?.engine, 'AI engine');
const trimTailing = text => text.replace(/[\.\s]*$/, '');
const newSessionId = () => createUoid({ type: sessionType });
const renderText = (t, o) => _renderText(t, { extraCodeBlock: 0, ...o || {} });
const log = (cnt, opt) => _log(cnt, import.meta.url, { time: 1, ...opt || {} });
const CONTENT_IS_REQUIRED = 'Content is required.';
const assertContent = content => assert(content.length, CONTENT_IS_REQUIRED);
const DEFAULT_MODELS = {
[CHATGPT_MINI]: GPT_4O_MINI,
[CHATGPT_REASONING]: GPT_O3_MINI,
[CHATGPT]: GPT_4O,
[CLAUDE]: CLAUDE_35_SONNET,
[GEMINI_EMEDDING]: EMBEDDING_001,
[GEMINI]: GEMINI_20_FLASH,
[OLLAMA]: DEEPSEEK_R1,
[OPENAI_EMBEDDING]: TEXT_EMBEDDING_3_SMALL,
[OPENAI_TRAINING]: GPT_4O_MINI, // https://platform.openai.com/docs/guides/fine-tuning
[OPENAI_VOICE]: NOVA,
};
DEFAULT_MODELS[CHAT] = DEFAULT_MODELS[GEMINI];
const tokenRatioByWords = Math.min(
100 / 75, // ChatGPT: https://platform.openai.com/tokenizer
Math.min(100 / 60, 100 / 80), // Gemini: https://ai.google.dev/gemini-api/docs/tokens?lang=node
);
const tokenRatioByCharacters = Math.max(
3.5, // Claude: https://docs.anthropic.com/en/docs/resources/glossary
4, // Gemini: https://ai.google.dev/gemini-api/docs/tokens?lang=node
);
// https://platform.openai.com/docs/models/continuous-model-upgrades
// https://platform.openai.com/settings/organization/limits // Tier 3
// https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/gemini
// https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models
const MODELS = {
[GPT_4O_MINI]: {
contextWindow: 128000,
imageCostTokens: 1105,
maxOutputTokens: 16384,
requestLimitsRPM: 5000,
tokenLimitsTPD: 40000000,
tokenLimitsTPM: 4000000,
trainingData: 'Oct 2023',
json: true,
vision: true,
reasoning: false,
audio: 'gpt-4o-mini-audio-preview',
supportedMimeTypes: [
png, jpeg, gif, webp,
],
supportedAudioTypes: [
wav,
],
},
[GPT_4O]: {
contextWindow: 128000,
imageCostTokens: 1105,
maxOutputTokens: 16384,
requestLimitsRPM: 5000,
tokenLimitsTPD: 100000000,
tokenLimitsTPM: 800000,
trainingData: 'Oct 2023',
json: true,
vision: true,
reasoning: false,
audio: 'gpt-4o-audio-preview',
supportedMimeTypes: [
png, jpeg, gif, webp,
],
supportedAudioTypes: [
wav,
],
},
[GPT_O1]: {
contextWindow: 200000,
imageCostTokens: 1105,
maxOutputTokens: 100000,
requestLimitsRPM: 500,
tokenLimitsTPD: 100000000,
tokenLimitsTPM: 800000,
trainingData: 'Oct 2023',
json: true,
reasoning: true,
vision: true,
// audio: 'gpt-4o-audio-preview', // fallback to GPT-4O to support audio
supportedMimeTypes: [
png, jpeg, gif, webp,
],
// supportedAudioTypes: [ // fallback to GPT-4O to support audio
// wav,
// ],
},
[GPT_O3_MINI]: {
contextWindow: 200000,
imageCostTokens: 1105,
maxOutputTokens: 100000,
requestLimitsRPM: 5000,
tokenLimitsTPD: 40000000,
tokenLimitsTPM: 4000000,
trainingData: 'Oct 2023',
json: true,
reasoning: true,
vision: true,
// audio: 'gpt-4o-mini-audio-preview', // fallback to GPT-4O-MINI to support audio
supportedMimeTypes: [
png, jpeg, gif, webp,
],
// supportedAudioTypes: [ // fallback to GPT-4O-MINI to support audio
// wav,
// ],
},
[GEMINI_20_FLASH]: {
// https://ai.google.dev/gemini-api/docs/models/gemini
// https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/send-multimodal-prompts?hl=en#gemini-send-multimodal-samples-pdf-nodejs
// Audio / Video Comming Soon: https://ai.google.dev/gemini-api/docs/models/gemini#gemini-2.0-flash
contextWindow: 1048576,
imageCostTokens: size8k / (768 * 768) * 258,
audioCostTokens: 1000000, // 8.4 hours => 1 million tokens
maxAudioLength: 60 * 60 * 8.4, // 9.5 hours
maxAudioPerPrompt: 1,
maxFileSize: 20 * 1024 * 1024, // 20 MB
maxImagePerPrompt: 3000,
maxImageSize: Infinity,
maxOutputTokens: 1024 * 8,
maxUrlSize: 1024 * 1024 * 1024 * 2, // 2 GB
maxVideoLength: 60 * 50, // 50 minutes
maxVideoLengthWithAudio: 60 * 50, // 50 minutes
maxVideoLengthWithoutAudio: 60 * 60, // 1 hour
maxVideoPerPrompt: 10,
requestLimitsRPM: 2000,
requestLimitsRPD: 1500,
tokenLimitsTPM: 4 * 1000000,
trainingData: 'August 2024',
vision: true,
json: true,
supportedMimeTypes: [
png, jpeg, mov, mpeg, mp4, mpg, avi, wmv, mpegps, flv, pdf, aac,
flac, mp3, m4a, mpga, opus, pcm, wav, webm, tgpp,
],
},
[GEMINI_20_FLASH_THINKING]: {
// https://cloud.google.com/vertex-ai/generative-ai/docs/thinking-mode?hl=en
contextWindow: 1024 * (8 + 32),
imageCostTokens: size8k / (768 * 768) * 258,
maxFileSize: 20 * 1024 * 1024, // 20 MB
maxImagePerPrompt: 3000,
maxImageSize: Infinity,
maxOutputTokens: 1024 * 8,
maxUrlSize: 1024 * 1024 * 1024 * 2, // 2 GB
requestLimitsRPM: 1000,
requestLimitsRPD: 1500,
tokenLimitsTPM: 4 * 1000000,
trainingData: 'August 2024',
json: false,
vision: true,
reasoning: true,
supportedMimeTypes: [
png, jpeg,
],
},
[GEMINI_20_PRO]: {
contextWindow: 2097152,
imageCostTokens: size8k / (768 * 768) * 258,
maxFileSize: 20 * 1024 * 1024, // 20 MB
maxImagePerPrompt: 3000,
maxImageSize: Infinity,
maxOutputTokens: 1024 * 8,
maxUrlSize: 1024 * 1024 * 1024 * 2, // 2 GB
requestLimitsRPM: 1000,
requestLimitsRPD: 1500,
tokenLimitsTPM: 4 * 1000000,
trainingData: 'August 2024',
vision: true,
json: true,
supportedMimeTypes: [
png, jpeg, mov, mpeg, mp4, mpg, avi, wmv, mpegps, flv, pdf, aac,
flac, mp3, m4a, mpga, opus, pcm, wav, webm, tgpp,
],
},
[DEEPSEEK_R1]: {
contextWindow: 128000,
maxOutputTokens: 32768,
requestLimitsRPM: Infinity,
tokenLimitsTPM: Infinity,
json: false,
vision: false,
reasoning: true,
},
[TEXT_EMBEDDING_3_SMALL]: {
contextWindow: 8191,
embedding: true,
outputDimension: 1536,
requestLimitsRPM: 500,
tokenLimitsTPM: 1000000,
trainingData: 'Sep 2021',
},
[TEXT_EMBEDDING_3_LARGE]: {
contextWindow: 8191,
embedding: true,
outputDimension: 3072, // ERROR: column cannot have more than 2000 dimensions for hnsw index
requestLimitsRPM: 500,
tokenLimitsTPM: 1000000,
trainingData: 'Sep 2021',
},
[EMBEDDING_001]: { // https://ai.google.dev/pricing#text-embedding004 FREE!
contextWindow: 3072,
embedding: true,
requestLimitsRPM: 1500,
},
[CLAUDE_35_SONNET]: { // https://docs.anthropic.com/en/docs/about-claude/models
contextWindow: 200 * 1000,
maxOutputTokens: 8192,
imageCostTokens: size8k / 750,
documentCostTokens: 3000 * 100, // 100 pages: https://docs.anthropic.com/en/docs/build-with-claude/pdf-support
maxImagePerPrompt: 5, // https://docs.anthropic.com/en/docs/build-with-claude/vision
maxImageSize: 1092, // by pixels
maxDocumentPages: 100,
maxDocumentFile: 1024 * 1024 * 32, // 32MB
requestLimitsRPM: 50,
tokenLimitsITPM: 40000,
tokenLimitsOTPM: 8000,
trainingData: 'Apr 2024',
supportedMimeTypes: [
png, jpeg, gif, webp, pdf,
],
},
};
MODELS[CLAUDE_35_HAIKU] = MODELS[CLAUDE_35_SONNET];
MODELS[DEEPSEEK_R1_32B] = MODELS[DEEPSEEK_R1];
for (const n in MODELS) {
MODELS[n]['name'] = n;
if (MODELS[n].embedding) {
MODELS[n].maxInputTokens = MODELS[n].contextWindow;
} else {
MODELS[n].supportedMimeTypes = MODELS[n].supportedMimeTypes || [];
MODELS[n].maxOutputTokens = MODELS[n].maxOutputTokens
|| Math.ceil(MODELS[n].contextWindow * 0.4);
MODELS[n].maxInputTokens = MODELS[n].maxInputTokens
|| (MODELS[n].contextWindow - MODELS[n].maxOutputTokens);
MODELS[n].tokenLimitsTPD = MODELS[n].tokenLimitsTPD
|| (MODELS[n].tokenLimitsTPM * minsOfDay);
MODELS[n].requestLimitsRPD = MODELS[n].requestLimitsRPD
|| (MODELS[n].requestLimitsRPM * minsOfDay);
MODELS[n].requestCapacityRPM = Math.ceil(Math.min(
MODELS[n].tokenLimitsTPM / MODELS[n].maxInputTokens,
MODELS[n].requestLimitsRPM, MODELS[n].requestLimitsRPD / minsOfDay
));
}
}
const MAX_INPUT_TOKENS = MODELS[GPT_4O_MINI].maxInputTokens;
const ATTACHMENT_TOKEN_COST = Math.max(MODELS[GPT_4O].imageCostTokens, 5000);
const MAX_TRIM_TRY = MAX_INPUT_TOKENS / 1000;
let tokeniser;
const unifyType = (type, name) => {
const TYPE = ensureString(type, { case: 'UP' });
assert(TYPE, `${name} is required.`);
return TYPE;
};
const init = async (options) => {
const provider = unifyProvider(options);
switch (provider) {
case OPENAI:
if (options?.apiKey) {
const OpenAI = await need('openai');
const openai = new OpenAI(options);
clients[provider] = { client: openai, clientBeta: openai.beta };
}
break;
case GEMINI:
if (options?.apiKey) {
const { GoogleGenerativeAI } = await need('@google/generative-ai');
const genAi = new GoogleGenerativeAI(options.apiKey);
const genModel = options?.model || DEFAULT_MODELS[GEMINI];
const tools = options?.tools || { google: true, code: false };
clients[provider] = {
generative: genAi.getGenerativeModel({
model: genModel,
tools: [
// @todo: https://cloud.google.com/vertex-ai/generative-ai/docs/gemini-v2?hl=en#search-tool
...tools.code ? [{
codeExecution: tools.code === true ? {} : tools.code
}] : [],
...tools.google ? [{
googleSearch: tools.google === true ? {} : tools.code,
}] : [],
],
}),
embedding: genAi.getGenerativeModel({
model: DEFAULT_MODELS[GEMINI_EMEDDING],
}), genModel,
};
}
break;
case CLAUDE:
if (options?.apiKey) {
const Anthropic = await need('@anthropic-ai/sdk');
const anthropic = new Anthropic({ apiKey: options?.apiKey });
clients[provider] = { client: anthropic };
}
break;
case OLLAMA:
clients[provider] || (clients[provider] = {
client: new (await need('ollama', { raw: true })).Ollama(options),
model: options?.model || DEFAULT_MODELS[OLLAMA],
});
break;
default:
throwError(`Invalid AI provider: ${options?.provider || 'null'}`);
}
assert(clients[provider], NOT_INIT);
return clients[provider];
};
const countTokens = async (input, options) => {
input = String.isString(input) ? input : JSON.stringify(input);
if (!options?.fast && !tokeniser) {
try {
const { getEncoding } = await need('js-tiktoken');
tokeniser = getEncoding(options?.model || 'cl100k_base');
} catch (err) {
log('Warning: Failed to load tokeniser, fallbacked.');
}
}
return tokenSafe(
!options?.fast && tokeniser ? tokeniser.encode(input).length : Math.max(
input.split(/[^a-z0-9]/i).length * tokenRatioByWords,
input.length / tokenRatioByCharacters
)
);
};
const selectGptAudioModel = options => {
assert(
MODELS[options.model]?.audio,
`Audio modality is not supported by model: ${options.model}`
);
return MODELS[options.model]?.audio;
};
const buildGptMessage = (content, options) => {
content = content || '';
let alterModel = options?.audioMode && selectGptAudioModel(options);
const attachments = (options?.attachments || []).map(x => {
assert(MODELS[options?.model], 'Model is required.');
if (MODELS[options.model]?.supportedMimeTypes?.includes?.(x.mime_type)) {
return { type: 'image_url', image_url: { url: x.url } };
} else if (MODELS[options.model]?.supportedAudioTypes?.includes?.(x.mime_type)) {
alterModel = selectGptAudioModel(options);
return {
type: 'input_audio',
input_audio: { data: x.data, format: WAV },
};
}
throwError(`Unsupported mime type: '${x.mime_type}'.`);
});
alterModel && (options.model = alterModel);
const message = String.isString(content) ? {
role: options?.role || user,
content: content.length ? [{ type: 'text', text: content }] : [],
} : content;
message.content || (message.content = []);
attachments.map(x => message.content.push(x));
assertContent(message.content);
return message;
};
const buildOllamaMessage = (content, options) => {
const message = String.isString(content) ? {
role: options?.role || user, content,
} : content;
assertContent(message.content);
return message;
};
const buildGeminiParts = (text, attachments) => {
// Gemini API does not allow empty text, even you prompt with attachments.
const message = [...text?.length || attachments?.length ? [{
text: text?.length ? text : ' '
}] : [], ...attachments || []];
assertContent(message);
return message;
};
const buildGeminiMessage = (content, options) => {
content = content || '';
const attachments = (options?.attachments || []).map(x => ({
inlineData: { mimeType: x.mime_type, data: x.data }
}));
return String.isString(content) ? (options?.history ? {
role: options?.role || user,
parts: buildGeminiParts(content, attachments),
} : buildGeminiParts(content, attachments)) : content;
};
const buildClaudeMessage = (text, options) => {
assert(text, 'Text is required.');
const attachments = (options?.attachments || []).map(x => {
let type = '';
if ([pdf].includes(x.mime_type)) {
type = 'document';
} else if ([png, jpeg, gif, webp].includes(x.mime_type)) {
type = 'image';
} else { throwError(`Unsupported mime type: ${x.mime_type}`); }
return {
type, source: {
type: BASE64.toLowerCase(),
media_type: x.mime_type, data: x.data,
},
}
});
return String.isString(text) ? {
role: options?.role || user,
content: [...attachments, { type: 'text', text }],
} : text;
};
const buildGeminiHistory = (text, options) => buildGeminiMessage(
text, { ...options || {}, history: true }
);
const [getOpenAIClient, getGeminiClient, getOllamaClient, getClaudeClient]
= [OPENAI, GEMINI, OLLAMA, CLAUDE].map(
x => async options => await init({ ...provider(x), ...options })
);
const listOpenAIModels = async (options) => {
const { client } = await getOpenAIClient(options);
const resp = await client.models.list();
return options?.raw ? resp : resp.data;
};
const packResp = async (resp, options) => {
let { text: txt, audio, references }
= String.isString(resp) ? { text: resp } : resp;
audio && (audio = Buffer.isBuffer(audio) ? audio : await convert(audio, {
input: BASE64, expected: BUFFER,
})) && audio.length && (audio = Buffer.concat([
createWavHeader(audio.length), audio
])) && (audio = await convert(audio, {
input: BUFFER, expected: BUFFER, ...options || {},
}));
// references debug codes:
// references = {
// "segments": [
// {
// "startIndex": 387,
// "endIndex": 477,
// "text": "It also provides live weather reports from Shanghai weather stations and weather warnings.",
// "indices": [
// 0
// ],
// "confidence": [
// 0.94840443
// ]
// },
// ],
// "links": [
// {
// "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AYygrcRVExzEYZU-23c6gKNSOJjLvSpI4CHtVmYJZaTLKd5N9GF-38GNyC2c9arn689-dmmpMh0Vd85x0kQp0IVY7BQMl1ugEYzy_IlDF-L3wFqf9xWHelAZF4cJa2LnWeUQsjyyTnYFRUs7nhlVoDVu1qYF0uLtVIjdyl5NH0PM92A=",
// "title": "weather-forecast.com"
// },
// ]
// };
let [richText, referencesMarkdown] = [null, null];
if (!options?.jsonMode) {
if (!options?.processing
&& references?.segments?.length && references?.links?.length) {
richText = txt;
for (let i = references.segments.length - 1; i >= 0; i--) {
let idx = richText.indexOf(references.segments[i].text);
if (idx < 0) { continue; }
idx += references.segments[i].text.length;
richText = richText.slice(0, idx)
+ references.segments[i].indices.map(y => ` (${y + 1})`).join('')
+ richText.slice(idx);
}
referencesMarkdown = 'References:\n\n' + references.links.map((x, i) => {
return `${i + 1}. [${x.title}](${x.uri})`;
}).join('\n');
}
// DeepSeek R1 {
let lines = (richText || txt).split('\n');
const indexOfEnd = lines.indexOf(THINK_END);
if (lines[0] === THINK_STR) {
if (indexOfEnd === -1) {
lines.shift();
} else {
lines[0] = MD_CODE + THINK;
lines[indexOfEnd] = MD_CODE;
lines.slice(1, indexOfEnd).join('').trim()
|| (lines = lines.slice(indexOfEnd + 1));
}
richText = lines.join('\n').trim();
}
// }
}
return {
...text(txt), ...options?.jsonMode && !(
options?.delta && options?.processing
) ? { json: parseJson(txt) } : {},
...richText ? { richText } : {},
...references ? { references } : {},
...referencesMarkdown ? { referencesMarkdown } : {},
...audio ? { audio, audioMimeType: options?.audioMimeType } : {},
model: options?.model,
};
};
const packGptResp = async (resp, options) => {
const text = resp?.choices?.[0]?.message?.content // ChatGPT
|| resp?.choices?.[0]?.message?.audio?.transcript // ChatGPT audio mode
|| resp?.text?.() // Gemini
|| resp?.content?.text // Claude
|| resp?.message?.content || ''; // Ollama
const audio = resp?.choices?.[0]?.message?.audio?.data; // ChatGPT audio mode
if (options?.raw) { return resp; }
else if (options?.simple && options?.jsonMode) { return parseJson(text); }
else if (options?.simple && options?.audioMode) { return audio; }
else if (options?.simple && text.substr(0, THINK_STR.length) === THINK_STR) {
return text.substr(text.indexOf(THINK_END) + THINK_END.length).trim();
} else if (options?.simple) { return text; }
return await packResp({ text, audio, references: resp?.references }, options);
};
const promptChatGPT = async (content, options = {}) => {
const { client } = await getOpenAIClient(options);
// https://github.com/openai/openai-node?tab=readme-ov-file#streaming-responses
// custom api endpoint not supported vision apis @todo by @Leask
// Structured Outputs: https://openai.com/index/introducing-structured-outputs-in-the-api/
client.baseURL !== OPENAI_BASE_URL
&& options?.attachments?.length && (options.attachments = []);
if (options?.model) { } else if (options?.reasoning) {
options.model = DEFAULT_MODELS[CHATGPT_REASONING];
} else {
options.model = DEFAULT_MODELS[CHATGPT];
}
options?.reasoning && !options?.reasoning_effort
&& (options.reasoning_effort = GPT_REASONING_EFFORT);
const message = buildGptMessage(content, options);
const modalities = options?.modalities || (
options?.audioMode ? ['text', AUDIO] : undefined
);
assert(!(
options?.jsonMode && !MODELS[options.model]?.json
), `This model does not support JSON output: ${options.model}`);
assert(!(
options?.reasoning && !MODELS[options.model]?.reasoning
), `This model does not support reasoning: ${options.model}`);
let format;
[format, options.audioMimeType, options.suffix]
= options?.stream ? ['pcm16', pcm16, 'pcm.wav'] : [WAV, wav, WAV];
let [resp, resultText, resultAudio, chunk] = [
await client.chat.completions.create({
modalities, audio: options?.audio || (
modalities?.find?.(x => x === AUDIO) && {
voice: DEFAULT_MODELS[OPENAI_VOICE], format
}
), ...messages([...options?.messages || [], message]),
...options?.jsonMode ? {
response_format: { type: JSON_OBJECT }
} : {}, model: options.model, stream: !!options?.stream,
}), '', Buffer.alloc(0), null
];
if (!options?.stream) {
return await packGptResp(resp, options);
}
for await (chunk of resp) {
const deltaText = chunk.choices[0]?.delta?.content
|| chunk.choices[0]?.delta?.audio?.transcript || '';
const deltaAudio = chunk.choices[0]?.delta?.audio?.data ? await convert(
chunk.choices[0].delta.audio.data, { input: BASE64, expected: BUFFER }
) : Buffer.alloc(0);
if (deltaText === '' && !deltaAudio.length) { continue; }
resultText += deltaText;
resultAudio = Buffer.concat([resultAudio, deltaAudio]);
const respAudio = options?.delta ? deltaAudio : resultAudio;
chunk.choices[0].message = {
content: options?.delta ? deltaText : resultText,
...respAudio.length ? { audio: { data: respAudio } } : {},
};
await ignoreErrFunc(async () => await options?.stream?.(
await packGptResp(chunk, { ...options || {}, processing: true })
), LOG);
}
chunk.choices[0].message = {
content: resultText,
...resultAudio.length ? { audio: { data: resultAudio } } : {},
};
return await packGptResp(chunk, options);
};
const promptOllama = async (content, options = {}) => {
const { client, model } = await getOllamaClient(options);
// https://github.com/ollama/ollama-js
// https://github.com/jmorganca/ollama/blob/main/examples/typescript-simplechat/client.ts
options.model = options?.model || model;
const resp = await client.chat({
model: options.model, stream: true,
...messages([...options?.messages || [], buildOllamaMessage(content)]),
})
let [chunk, result] = [null, ''];
for await (chunk of resp) {
const delta = chunk.message.content || '';
if (delta === '') { continue; }
result += delta;
chunk.message.content = options?.delta ? delta : result;
await ignoreErrFunc(async () => await options?.stream?.(
await packGptResp(chunk, { ...options || {}, processing: true })
), LOG);
}
chunk.message.content = result;
return await packGptResp(chunk, options);
};
const promptClaude = async (content, options = {}) => {
const { client } = await getClaudeClient(options);
options.model = options?.model || DEFAULT_MODELS[CLAUDE];
const resp = await client.messages.create({
model: options.model, max_tokens: MODELS[options.model].maxOutputTokens,
messages: [
...options?.messages || [], buildClaudeMessage(content, options)
], stream: !!options?.stream,
});
let [event, result] = [null, ''];
if (options?.stream) {
for await (event of resp) {
const delta = event?.content_block?.text || event?.delta?.text || '';
if (delta === '') { continue; }
result += delta;
event.content = { text: options?.delta ? delta : result };
await ignoreErrFunc(async () => await options.stream(
await packGptResp(event, { ...options || {}, processing: true })
), LOG);
}
event.content = { text: result };
}
return await packGptResp(options?.stream ? event : resp, options);
};
const uploadFile = async (input, options) => {
const { client } = await getOpenAIClient(options);
const { content: file, cleanup } = await convert(input, {
input: options?.input, ...options || {}, expected: STREAM,
errorMessage: INVALID_FILE, suffix: options?.suffix,
withCleanupFunc: true,
});
const resp = await client.files.create({ file, ...options?.params || {} });
await cleanup();
return resp;
};
const uploadFileForFineTuning = async (content, options) => await uploadFile(
content, { suffix: 'jsonl', ...options, params: { purpose: 'fine-tune' } }
);
const listFiles = async (options) => {
const { client } = await getOpenAIClient(options);
const files = [];
const list = await client.files.list(options?.params || {});
for await (const file of list) { files.push(file); }
return files;
};
const deleteFile = async (file_id, options) => {
const { client } = await getOpenAIClient(options);
return await client.files.del(file_id);
};
const generationConfig = options => ({
generationConfig: {
...options?.generationConfig || {},
responseMimeType: options?.jsonMode ? mimeJson : mimeText,
},
});
const packGeminiReferences = (chunks, supports) => {
let references = null;
if (chunks?.length && supports?.length) {
references = { segments: [], links: [] };
supports.map(s => references.segments.push({
...s.segment, indices: s.groundingChunkIndices,
confidence: s.confidenceScores,
}));
chunks.map(c => references.links.push(c.web));
}
return references;
};
const handleGeminiResponse = async (resp, options) => {
const _resp = await resp;
let [result, references] = ['', null];
if (options?.stream) {
for await (const chunk of _resp.stream) {
const delta = chunk?.text?.() || '';
const rfc = packGeminiReferences(
chunk.candidates[0]?.groundingMetadata?.groundingChunks,
chunk.candidates[0]?.groundingMetadata?.groundingSupports
);
if (delta === '' && !rfc) { continue; }
result += delta;
references = rfc;
await ignoreErrFunc(async () => await options.stream(
await packGptResp({
text: () => options?.delta ? delta : result, references,
}, { ...options || {}, processing: true })
), LOG);
}
}
const __resp = await _resp.response;
return await packGptResp(options?.stream ? {
__resp, text: () => result, references
} : {
...__resp, references: packGeminiReferences(
__resp.candidates[0]?.groundingMetadata?.groundingChunks,
__resp.candidates[0]?.groundingMetadata?.groundingSupports
)
}, options);
};
const promptGemini = async (content, options) => {
const { generative, genModel } = await getGeminiClient(options);
// https://github.com/google/generative-ai-js/blob/main/samples/node/advanced-chat.js
// @todo: check this issue similar to Vertex AI:
// Google's bug: history is not allowed while using inline_data?
assert(!(
options?.jsonMode && MODELS[genModel]?.json == false
), `This model does not support JSON output: ${genModel}`);
const chat = generative.startChat({
history: options?.messages && !options?.attachments?.length
? options.messages : [],
...generationConfig(options),
});
const resp = chat[options?.stream ? 'sendMessageStream' : 'sendMessage'](
buildGeminiMessage(content, options)
);
return await handleGeminiResponse(
resp, { ...options || {}, model: genModel }
);
};
const checkEmbeddingInput = async (input, model) => {
assert(input, 'Text is required.', 400);
const arrInput = input.split(' ');
const getInput = () => arrInput.join(' ');
const _model = MODELS[model];
assert(_model, `Invalid model: '${model}'.`);
await trimPrompt(getInput, arrInput.pop, _model.contextWindow);
return getInput();
};
const createOpenAIEmbedding = async (input, options) => {
// args from vertex embedding may be useful uere
// https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings
// task_type Description
// RETRIEVAL_QUERY Specifies the given text is a query in a search/ retrieval setting.
// RETRIEVAL_DOCUMENT Specifies the given text is a document in a search / retrieval setting.
// SEMANTIC_SIMILARITY Specifies the given text will be used for Semantic Textual Similarity(STS).
// CLASSIFICATION Specifies that the embeddings will be used for classification.
// CLUSTERING Specifies that the embeddings will be used for clustering.
const { client } = await getOpenAIClient(options);
const model = options?.model || DEFAULT_MODELS[OPENAI_EMBEDDING];
const resp = await client.embeddings.create({
model, input: await checkEmbeddingInput(input, model),
});
return options?.raw ? resp : resp?.data[0].embedding;
};
const createGeminiEmbedding = async (input, options) => {
const { embedding } = await getGeminiClient(options);
const model = options?.model || DEFAULT_MODELS[GEMINI_EMEDDING];
const resp = await embedding.embedContent(
await checkEmbeddingInput(input, model)
);
return options?.raw ? resp : resp?.embedding.values;
};
const buildGptTrainingCase = (prompt, response, options) => messages([
...options?.systemPrompt ? [buildGptMessage(options.systemPrompt, { role: system })] : [],
buildGptMessage(prompt),
buildGptMessage(response, { role: assistant }),
]);
const buildGptTrainingCases = (cases, opts) => cases.map(x => JSON.stringify(
buildGptTrainingCase(x.prompt, x.response, { ...x.options, ...opts })
)).join('\n');
const createGptFineTuningJob = async (training_file, options) => {
const { client } = await getOpenAIClient(options);
return await client.fineTuning.jobs.create({
training_file, model: options?.model || DEFAULT_MODELS[OPENAI_TRAINING],
})
};
const getGptFineTuningJob = async (job_id, options) => {
const { client } = await getOpenAIClient(options);
// https://platform.openai.com/finetune/[job_id]?filter=all
return await client.fineTuning.jobs.retrieve(job_id);
};
const cancelGptFineTuningJob = async (job_id, options) => {
const { client } = await getOpenAIClient(options);
return await client.fineTuning.jobs.cancel(job_id);
};
const listGptFineTuningJobs = async (options) => {
const { client } = await getOpenAIClient(options);
const resp = await client.fineTuning.jobs.list({
limit: GPT_QUERY_LIMIT, ...options?.params
});
return options?.raw ? resp : resp.data;
};
const listGptFineTuningEvents = async (job_id, options) => {
const { client } = await getOpenAIClient(options);
const resp = await client.fineTuning.jobs.listEvents(job_id, {
limit: GPT_QUERY_LIMIT, ...options?.params,
});
return options?.raw ? resp : resp.data;
};
const tailGptFineTuningEvents = async (job_id, options) => {
assert(job_id, 'Job ID is required.');
const [loopName, listOpts] = [`GPT-${job_id}`, {
...options, params: { ...options?.params, order: 'ascending' }
}];
let lastEvent;
return await loop(async () => {
const resp = await listGptFineTuningEvents(job_id, {
...listOpts, params: {
...listOpts?.params,
...(lastEvent ? { after: lastEvent.id } : {}),
},
});
for (lastEvent of resp) {
lastEvent.message.includes('completed') && await end(loopName);
await options?.stream(lastEvent);
}
}, 3, 2, 1, loopName, { silent, ...options });
};
const initChat = async (options) => {
options = {
engines: options?.engines || { [DEFAULT_MODELS[CHAT]]: {} },
...options || {},
};
if (options?.sessions) {
assert(
options.sessions?.get && options.sessions?.set,
'Invalid session storage provider.'
);
chatConfig.sessions = options.sessions;
}
options?.instructions && (chatConfig.systemPrompt = options.instructions);
for (const i in options.engines) {
const key = ensureString(i, { case: 'UP' });
const model = DEFAULT_MODELS[key];
assert(model, `Invalid chat model: '${i}'.`);
chatConfig.engines[key] = options.engines[i];
chatConfig.engines[key].model = chatConfig.engines[key].model || model;
print(chatConfig.engines[key].model);
const mxPmpt = MODELS[chatConfig.engines[key].model].maxInputTokens / 2;
const pmptTokens = await countTokens([buildGeminiHistory(
chatConfig.systemPrompt, { role: system }
)]); // Use Gemini instead of ChatGPT because of the longer pack
assert(
pmptTokens < mxPmpt,
`System prompt is too long: ${pmptTokens} / ${mxPmpt} tokens.`
);
}
return chatConfig;
};
const defaultSession = session => ({
messages: [], systemPrompt: chatConfig.systemPrompt,
threadId: null, ...session || {},
});
const assertSessionId = sessionId => {
sessionId = ensureString(sessionId, { case: 'UP' });
assert(sessionId, 'Session ID is required.');
return sessionId;
};
const getSession = async (sessionId, options) => {
sessionId = assertSessionId(sessionId);
return defaultSession(await chatConfig.sessions.get(
sessionId, options?.prompt, options
));
};
const setSession = async (sessionId, session, options) => {
sessionId = assertSessionId(sessionId);
return await chatConfig.sessions.set(sessionId, session, options);
};
const resetSession = async (sessionId, options) => {
const session = {
...defaultSession(),
...options?.systemPrompt ? { systemPrompt: options.systemPrompt } : {},
};
return await setSession(sessionId, session);
};
const packResult = resp => {
const result = {
...resp, richText: resp.richText || resp.text, spoken: renderText(
resp.text, { noCode: true, noLink: true }
).replace(/\[\^\d\^\]/ig, ''),
};
log(`Response (${result.model}): ${JSON.stringify(result.text)}`);
// log(result);
return result;
};
const talk = async (input, options) => {
const engine = unifyEngine({
engine: Object.keys(chatConfig.engines)?.[0] || DEFAULT_MODELS[CHAT],
...options,