-
Notifications
You must be signed in to change notification settings - Fork 40
/
Chat.mjs
335 lines (272 loc) · 9.66 KB
/
Chat.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
import fs from 'fs/promises';
import os from 'os';
import path from 'path';
import { OpenAI } from "openai";
import { Anthropic } from '@anthropic-ai/sdk';
import { OpenRouter } from "@openrouter/ai-sdk-provider";
import { GoogleGenerativeAI } from "@google/generative-ai";
import { encode } from "gpt-tokenizer/esm/model/davinci-codex"; // tokenizer
// Map of model shortcodes to full model names
export const MODELS = {
// GPT by OpenAI
gm: 'gpt-4o-mini',
g: 'chatgpt-4o-latest',
//g: 'gpt-4o',
//G: 'gpt-4-32k-0314',
// o1 by OpenAI
om: 'o1-mini',
o: 'o1-preview',
// Claude by Anthropic
cm: 'claude-3-5-haiku-20241022',
c: 'claude-3-5-sonnet-20241022',
C: 'claude-3-5-sonnet-20240620',
//c: 'claude-3-5-sonnet-20240620',
//C: 'claude-3-5-sonnet-20241022', // TODO: temporarily using the new sonnet instead of opus
//C: 'claude-3-opus-20240229',
// Llama by Meta
lm: 'meta-llama/llama-3.1-8b-instruct',
l: 'meta-llama/llama-3.1-70b-instruct',
L: 'meta-llama/llama-3.1-405b-instruct',
// Gemini by Google
i: 'gemini-1.5-flash-latest',
I: 'gemini-1.5-pro-exp-0801'
};
// Factory function to create a stateful OpenAI chat
export function openAIChat(clientClass) {
const messages = [];
let extendFunction = null;
async function ask(userMessage, { system, model, temperature = 0.0, max_tokens = 8192, stream = true, shorten = (x => x), extend = null, predict = null }) {
if (userMessage === null) {
return { messages };
}
model = MODELS[model] || model;
const client = new clientClass({ apiKey: await getToken(clientClass.name.toLowerCase()) });
const is_o1 = model.startsWith("o1");
// FIXME: update when OAI's o1 API flexibilizes
var max_completion_tokens = undefined;
if (is_o1) {
stream = false;
temperature = 1;
max_completion_tokens = max_tokens;
max_tokens = undefined;
}
if (messages.length === 0 && system) {
// FIXME: update when OAI's o1 API flexibilizes
if (is_o1) {
messages.push({ role: "user", content: system });
} else {
messages.push({ role: "system", content: system });
}
}
let extendedUserMessage = extendFunction ? extendFunction(userMessage) : userMessage;
extendFunction = extend; // Set for next call
const messagesCopy = [...messages, { role: "user", content: extendedUserMessage }];
messages.push({ role: "user", content: userMessage });
const prediction = predict && model.indexOf("o1") === -1 ? { type: "content", content: predict } : undefined;
//console.log(prediction);
const params = {
messages: messagesCopy,
model,
temperature,
max_tokens,
max_completion_tokens,
stream,
prediction,
};
let result = "";
const response = await client.chat.completions.create(params);
if (stream) {
for await (const chunk of response) {
const text = chunk.choices[0]?.delta?.content || "";
process.stdout.write(text);
result += text;
}
} else {
const text = response.choices[0]?.message?.content || "";
process.stdout.write(text);
result = text;
}
messages.push({ role: 'assistant', content: await shorten(result) });
return result;
}
return ask;
}
// Factory function to create a stateful Anthropic chat
export function anthropicChat(clientClass, MODEL) {
const messages = [];
async function ask(userMessage, { system, model, temperature = 0.0, max_tokens = 8192, stream = true, system_cacheable = false, shorten = (x => x), extend = null }) {
if (userMessage === null) {
return { messages };
}
model = model || MODEL;
model = MODELS[model] || model;
const client = new clientClass({
apiKey: await getToken(clientClass.name.toLowerCase()),
defaultHeaders: {
"anthropic-beta": "prompt-caching-2024-07-31" // Enable prompt caching
}
});
let extendedUserMessage = extend ? extend(userMessage) : userMessage;
const messagesCopy = [...messages, { role: "user", content: extendedUserMessage }];
messages.push({ role: "user", content: userMessage });
const cached_system = [{ type: "text", text: system, cache_control: { type: "ephemeral" } }];
let prompt_system = system_cacheable ? cached_system : system;
const params = { system: prompt_system, model, temperature, max_tokens, stream };
//console.log("->", extend, JSON.stringify(messagesCopy, null, 2));
let result = "";
const response = client.messages
.stream({ ...params, messages: messagesCopy })
.on('text', (text) => {
if (stream) {
process.stdout.write(text);
}
result += text;
});
await response.finalMessage();
messages.push({ role: 'assistant', content: await shorten(result) });
return result;
}
return ask;
}
export function geminiChat(clientClass) {
const messages = [];
let extendFunction = null;
async function ask(userMessage, { system, model, temperature = 0.0, max_tokens = 4096, stream = true, shorten = (x => x), extend = null }) {
if (userMessage === null) {
return { messages };
}
model = MODELS[model] || model;
const client = new clientClass(await getToken(clientClass.name.toLowerCase()));
const generationConfig = {
maxOutputTokens: max_tokens,
temperature,
};
const safetySettings = [
{
category: "HARM_CATEGORY_HARASSMENT",
threshold: "BLOCK_NONE",
},
{
category: "HARM_CATEGORY_HATE_SPEECH",
threshold: "BLOCK_NONE",
},
{
category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
threshold: "BLOCK_NONE",
},
{
category: "HARM_CATEGORY_DANGEROUS_CONTENT",
threshold: "BLOCK_NONE",
},
];
let extendedUserMessage = extendFunction ? extendFunction(userMessage) : userMessage;
extendFunction = extend; // Set for next call
const messagesCopy = [...messages, { role: "user", parts: [{ text: extendedUserMessage }] }];
messages.push({ role: "user", parts: [{ text: userMessage }] });
const chat = client.getGenerativeModel({ model, generationConfig })
.startChat({
history: messagesCopy,
safetySettings: safetySettings,
});
let result = "";
if (stream) {
const response = await chat.sendMessageStream(extendedUserMessage);
for await (const chunk of response.stream) {
const text = chunk.text();
process.stdout.write(text);
result += text;
}
} else {
const response = await chat.sendMessage(extendedUserMessage);
result = (await response.response).text();
}
messages.push({ role: 'model', parts: [{ text: await shorten(result) }] });
return result;
}
return ask;
}
// Factory function to create a stateful OpenRouter chat
export function openRouterChat(clientClass) {
const messages = [];
let extendFunction = null;
async function ask(userMessage, { system, model, temperature = 0.0, max_tokens = 8192, stream = true, shorten = (x => x), extend = null }) {
if (userMessage === null) {
return { messages };
}
model = MODELS[model] || model;
const openai = new OpenAI({
baseURL: "https://openrouter.ai/api/v1",
apiKey: await getToken('openrouter'),
defaultHeaders: {
"HTTP-Referer": "https://github.com/OpenRouterTeam/openrouter-examples",
},
});
if (messages.length === 0 && system) {
messages.push({ role: "system", content: system });
}
let extendedUserMessage = extendFunction ? extendFunction(userMessage) : userMessage;
extendFunction = extend; // Set for next call
const messagesCopy = [...messages, { role: "user", content: extendedUserMessage }];
messages.push({ role: "user", content: userMessage });
const params = {
messages: messagesCopy,
model,
temperature,
max_tokens,
stream,
};
let result = "";
const response = await openai.chat.completions.create(params);
if (stream) {
for await (const chunk of response) {
const text = chunk.choices[0]?.delta?.content || "";
process.stdout.write(text);
result += text;
}
} else {
const text = response.choices[0]?.message?.content || "";
process.stdout.write(text);
result = text;
}
messages.push({ role: 'assistant', content: await shorten(result) });
return result;
}
return ask;
}
// Generic asker function that dispatches to the correct asker based on the model name
export function chat(model) {
model = MODELS[model] || model;
if (model.startsWith('gpt')) {
return openAIChat(OpenAI, model);
} else if (model.startsWith('o1')) {
return openAIChat(OpenAI, model);
} else if (model.startsWith('chatgpt')) {
return openAIChat(OpenAI, model);
} else if (model.startsWith('claude')) {
return anthropicChat(Anthropic, model);
} else if (model.startsWith('meta')) {
return openRouterChat(OpenRouter, model);
} else if (model.startsWith('gemini')) {
return geminiChat(GoogleGenerativeAI, model);
} else {
throw new Error(`Unsupported model: ${model}`);
}
}
// Utility function to read the API token for a given vendor
async function getToken(vendor) {
const tokenPath = path.join(os.homedir(), '.config', `${vendor}.token`);
try {
return (await fs.readFile(tokenPath, 'utf8')).trim();
} catch (err) {
console.error(`Error reading ${vendor}.token file:`, err.message);
process.exit(1);
}
}
export function tokenCount(inputText) {
// Encode the input string into tokens
const tokens = encode(inputText);
// Get the number of tokens
const numberOfTokens = tokens.length;
// Return the number of tokens
return numberOfTokens;
}