forked from b4rtaz/distributed-llama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-api-client.js
More file actions
48 lines (43 loc) · 1.3 KB
/
Copy pathchat-api-client.js
File metadata and controls
48 lines (43 loc) · 1.3 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
// This is a simple client for dllama-api.
//
// Usage:
//
// 1. Start the server, how to do it is described in the `src/apps/dllama-api/README.md` file.
// 2. Run this script: `node examples/chat-api-client.js`
const HOST = process.env.HOST ? process.env.HOST : '127.0.0.1';
const PORT = process.env.PORT ? Number(process.env.PORT) : 9990;
async function chat(messages, maxTokens) {
const response = await fetch(`http://${HOST}:${PORT}/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
messages,
temperature: 0.7,
stop: ['<|eot_id|>'],
max_tokens: maxTokens
}),
});
return await response.json();
}
async function ask(system, user, maxTokens) {
console.log(`> system: ${system}`);
console.log(`> user: ${user}`);
const response = await chat([
{
role: 'system',
content: system
},
{
role: 'user',
content: user
}
], maxTokens);
console.log(`${response.choices[0].message.content}`);
}
async function main() {
await ask('You are an excellent math teacher.', 'What is 1 + 2?', 64);
await ask('You are a romantic.', 'Where is Europe?', 64);
}
main();