-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.js
41 lines (35 loc) · 992 Bytes
/
client.js
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
const request = require('request');
const stream = require('stream');
const pipeline = require('stream').pipeline;
const options = {
'method': 'POST',
'url': 'YOUR_PROJECT_URL',
'headers': {
'Content-Type': 'application/json',
'X-Banana-API-Key': 'YOUR_API_KEY'
},
body: JSON.stringify({ "prompt": "what is your favourite color: ", "fast": true })
};
const readable = request(options);
const writable = new stream.Writable();
let curChunk = "";
writable._write = function(chunk, encoding, next) {
curChunk += chunk.toString();
while (curChunk.includes("\n")) {
const [first, ...rest] = curChunk.split("\n");
// parse first as JSON
const parsed = JSON.parse(first);
// write to stdout without newline
process.stdout.write(parsed.text);
curChunk = rest.join("\n");
}
next();
};
pipeline( readable, writable, function(err) {
if (err) {
console.error('Pipeline failed.', err);
} else {
console.log('Pipeline succeeded.');
}
}
);