-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (46 loc) · 1.26 KB
/
index.js
File metadata and controls
53 lines (46 loc) · 1.26 KB
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
const { loadConfig, CONFIG_FILE } = require('./lib/config');
const { OpenAICompatibleClient } = require('./lib/ai');
async function main() {
const command = process.argv[2] || 'chat';
const prompt = process.argv.slice(3).join(' ') || 'Say hello in English and Chinese.';
const config = loadConfig();
if (!config) {
console.error(`Missing config file: ${CONFIG_FILE}`);
console.error('Copy config.example.json to config.json and fill in baseURL/apiKey/model first.');
process.exit(1);
}
const client = new OpenAICompatibleClient(config);
const messages = [
{
role: 'system',
content: 'You are a helpful assistant.'
},
{
role: 'user',
content: prompt
}
];
if (command === 'stream') {
await client.streamChat(messages, {
maxTokens: 1000,
onDelta: async (delta) => {
process.stdout.write(delta);
},
onComplete: async () => {
process.stdout.write('\n');
}
});
return;
}
if (command === 'serve') {
const { startServer } = require('./lib/server');
startServer(config);
return;
}
const reply = await client.chat(messages, { maxTokens: 1000 });
console.log(reply);
}
main().catch((err) => {
console.error(err.message);
process.exit(1);
});