49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
const axios = require("axios");
|
|
|
|
module.exports = class Client {
|
|
constructor(endpoint, model) {
|
|
this.endpoint = endpoint;
|
|
this.model = model;
|
|
}
|
|
|
|
async send(input) {
|
|
return new Promise((resolve, reject) => {
|
|
let data = JSON.stringify({
|
|
input: input,
|
|
model: this.model,
|
|
previous_response_id: this.previous_response_id,
|
|
metadata: { channel: "text" },
|
|
});
|
|
|
|
let config = {
|
|
method: "post",
|
|
maxBodyLength: Infinity,
|
|
url: this.endpoint,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
data: data,
|
|
};
|
|
|
|
axios
|
|
.request(config)
|
|
.then((response) => {
|
|
if (response.data && response.data.output) {
|
|
if (
|
|
response.data.output.length > 0 &&
|
|
response.data.output[0].content.length > 0
|
|
) {
|
|
this.previous_response_id = response.data.id;
|
|
resolve(response.data.output[0].content[0].text);
|
|
}
|
|
} else {
|
|
resolve("No output received from server.");
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
};
|