-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.mjs
145 lines (140 loc) · 5.6 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
import { alan, bot, image, shot, speech, utilitas, vision } from 'utilitas';
import { parse } from 'csv-parse/sync';
await utilitas.locate(utilitas.__(import.meta.url, 'package.json'));
const log = content => utilitas.log(content, 'halbot');
const skillPath = utilitas.__(import.meta.url, 'skills');
const promptSource = new Set([
// 'https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv',
'https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/82f15563c9284c01ca54f0b915ae1aeda5a0fc3a/prompts.csv'
]);
const fetchPrompts = async () => {
const prompts = {};
for (let source of promptSource) {
try {
const resp = (await shot.get(source)).content;
const pmts = parse(resp, { columns: true, skip_empty_lines: true });
assert(pmts?.length, `Failed to load external prompts: ${source}.`);
pmts.filter(x => x.act && x.prompt).map(x => {
const { command, description } = bot.newCommand(x.act, x.act);
prompts[command] = { ...x, command, act: description };
});
} catch (err) { log(err?.message || err); }
}
log(`Awesome ChatGPT Prompts: fetch ${utilitas.countKeys(prompts)} items.`);
return prompts;
};
const init = async (options) => {
assert(options?.telegramToken, 'Telegram Bot API Token is required.');
const [pkg, ai, _speech, speechOptions, engines]
= [await utilitas.which(), {}, {}, { tts: true, stt: true }, {}];
const info = bot.lines([
`[${bot.EMOJI_BOT} ${pkg.title}](${pkg.homepage})`, pkg.description
]);
let embedding;
// init ai engines
if (options?.openaiApiKey || options?.chatGptApiKey) {
await alan.init({
provider: 'OPENAI',
apiKey: options?.openaiApiKey || options?.chatGptApiKey,
...options || {},
});
ai['ChatGPT'] = {
engine: 'CHATGPT', priority: options?.chatGptPriority || 0,
};
engines['CHATGPT'] = {
// only support custom model while prompting
model: options?.chatGptModel,
};
}
if (options?.googleApiKey) {
await alan.init({
provider: 'GEMINI', apiKey: options?.googleApiKey,
model: options?.geminiModel, // only support custom model while initiating
...options || {},
});
ai['Gemini'] = {
engine: 'GEMINI', priority: options?.geminiPriority || 1,
};
engines['GEMINI'] = {
// save for reference not for prompting
model: options?.geminiModel,
};
}
if (options?.claudeApiKey) {
await alan.init({
provider: 'CLAUDE', apiKey: options?.claudeApiKey,
...options || {},
});
ai['Claude'] = {
engine: 'CLAUDE', priority: options?.claudePriority || 2,
};
engines['CLAUDE'] = {
// only support custom model while prompting
model: options?.claudeModel,
};
}
if (options?.mistralEnabled || options?.mistralEndpoint) {
await alan.init({
provider: 'OLLAMA', endpoint: options?.mistralEndpoint,
});
ai['Mistral'] = {
engine: 'OLLAMA', priority: options?.mistralPriority || 3,
};
engines['OLLAMA'] = {
// only support custom model while prompting
model: options?.mistralModel,
};
}
assert(utilitas.countKeys(ai), 'No AI provider is configured.');
await alan.initChat({ engines, sessions: options?.storage });
for (const i in ai) { ai[i].model = engines[ai[i].engine].model; }
// init image, speech, embedding engines
if (options?.openaiApiKey) {
const apiKey = { apiKey: options.openaiApiKey };
await image.init(apiKey);
await speech.init({ ...apiKey, provider: 'OPENAI', ...speechOptions });
embedding = alan.createOpenAIEmbedding;
} else if (options?.googleApiKey) {
const apiKey = { apiKey: options.googleApiKey };
await speech.init({ ...apiKey, provider: 'GOOGLE', ...speechOptions });
embedding = alan.createGeminiEmbedding;
}
// init vision / audio engine
const supportedMimeTypes = new Set(Object.values(engines).map(
x => alan.MODELS[x.model]
).map(x => [
...x.supportedMimeTypes || [], ...x.supportedAudioTypes || [],
]).flat().map(x => x.toLowerCase()));
if (options?.googleApiKey) {
const apiKey = { apiKey: options.googleApiKey };
await vision.init(apiKey);
}
// init bot
const _bot = await bot.init({
args: options?.args,
auth: options?.auth,
botToken: options?.telegramToken,
chatType: options?.chatType,
cmds: options?.cmds,
database: options?.storage?.client && options?.storage,
embedding, supportedMimeTypes,
hello: options?.hello,
help: options?.help,
homeGroup: options?.homeGroup,
info: options?.info || info,
magicWord: options?.magicWord,
private: options?.private,
botProvider: 'telegram',
session: options?.storage,
skillPath: options?.skillPath || skillPath,
speech: (options?.openaiApiKey || options?.googleApiKey) && speech,
vision: options?.googleApiKey && vision,
});
_bot._.ai = ai; // Should be an array of a map of AIs.
_bot._.lang = options?.lang || 'English';
_bot._.image = options?.openaiApiKey && image;
_bot._.prompts = await fetchPrompts();
return _bot;
};
export default init;
export { alan, bot, init, speech, utilitas };