-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (57 loc) · 1.48 KB
/
index.js
File metadata and controls
68 lines (57 loc) · 1.48 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const URL = "https://chat.llmasaservice.io/";
const PROJECT_ID = "379aeb32-8de9-4854-af83-1a0796d1fcd0";
async function callLLMAsAService(
prompt,
messages = [
{
role: "system",
content:
"Answer in plain text formatting (no markdown), in a comical way",
},
]
) {
const responseBody = JSON.stringify({
projectId: PROJECT_ID,
prompt: prompt,
messages: messages,
customer: {}, // Optional { customer_id: "1234", customer_name: "John Doe" }
});
const options = {
method: "POST",
headers: {
"Content-Type": "text/plain",
},
body: responseBody,
};
try {
const response = await fetch(URL, options);
if (!response.ok) {
console.log(
`Error: Network error for service. (${response.status} ${response.statusText})`
);
} else {
const reader = response?.body?.getReader();
const decoder = new TextDecoder("utf-8");
console.log(await readStreamToEnd(reader, decoder));
}
} catch (error) {
console.log(
`Error: Having trouble connecting to chat service. (${error.message})`
);
}
}
async function readStreamToEnd(reader, decoder) {
let result = "";
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
result += decoder.decode(value);
}
return result;
}
// Parse command-line arguments
const prompt = process.argv[2] || "What are 3 modern boy names?";
// Execute the function
callLLMAsAService(prompt);