Refactored to flow Hub Packaging directory structures
This commit is contained in:
48
basic-messenger-cli/application/src/client/client.js
Normal file
48
basic-messenger-cli/application/src/client/client.js
Normal file
@@ -0,0 +1,48 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
30
basic-messenger-cli/application/src/index.js
Normal file
30
basic-messenger-cli/application/src/index.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const readline = require("readline");
|
||||
const Client = require("./client/client");
|
||||
const dotenv = require("dotenv");
|
||||
|
||||
// dot env config
|
||||
dotenv.config();
|
||||
const END_POINT = process.env.END_POINT;
|
||||
const MODEL = process.env.MODEL;
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
rl.setPrompt("You: ");
|
||||
|
||||
const client = new Client(END_POINT, MODEL);
|
||||
|
||||
const init = async () => {
|
||||
rl.prompt();
|
||||
// Reads user input to send messages in a prompt
|
||||
rl.on("line", async (input) => {
|
||||
if (input.trim().length > 0) {
|
||||
output = await client.send(input);
|
||||
console.log(`Assistant: ${output}`);
|
||||
}
|
||||
rl.prompt();
|
||||
});
|
||||
};
|
||||
|
||||
init();
|
||||
Reference in New Issue
Block a user