Updated to typescript and added chalk for a bit of color and formatting
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
48
basic-messenger-cli/application/src/client/client.ts
Normal file
48
basic-messenger-cli/application/src/client/client.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
|
||||
export default class Client {
|
||||
private endpoint: string;
|
||||
private model: string;
|
||||
private previous_response_id?: string;
|
||||
|
||||
constructor(endpoint: string, model: string) {
|
||||
this.endpoint = endpoint;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
async send(input: string): Promise<string> {
|
||||
const data = JSON.stringify({
|
||||
input: input,
|
||||
model: this.model,
|
||||
previous_response_id: this.previous_response_id,
|
||||
metadata: { channel: "text" },
|
||||
});
|
||||
|
||||
const config: AxiosRequestConfig = {
|
||||
method: "post",
|
||||
maxBodyLength: Infinity,
|
||||
url: this.endpoint,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
data: data,
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await axios.request(config);
|
||||
if (
|
||||
response.data &&
|
||||
response.data.output &&
|
||||
response.data.output.length > 0 &&
|
||||
response.data.output[0].content.length > 0
|
||||
) {
|
||||
this.previous_response_id = response.data.id;
|
||||
return response.data.output[0].content[0].text;
|
||||
} else {
|
||||
return "No output received from server.";
|
||||
}
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
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();
|
||||
39
basic-messenger-cli/application/src/index.ts
Normal file
39
basic-messenger-cli/application/src/index.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import readline from "readline";
|
||||
import dotenv from "dotenv";
|
||||
import chalk from "chalk";
|
||||
import Client from "./client/client";
|
||||
|
||||
// dot env config
|
||||
dotenv.config();
|
||||
const END_POINT: string | undefined = process.env.END_POINT;
|
||||
const MODEL: string | undefined = process.env.MODEL;
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
rl.setPrompt(chalk.blue("You: "));
|
||||
|
||||
if (!END_POINT || !MODEL) {
|
||||
throw new Error("END_POINT and MODEL environment variables must be defined.");
|
||||
}
|
||||
const client = new Client(END_POINT, MODEL);
|
||||
|
||||
const init = async (): Promise<void> => {
|
||||
rl.prompt();
|
||||
// Reads user input to send messages in a prompt
|
||||
rl.on("line", async (input: string) => {
|
||||
if (input.trim().length > 0) {
|
||||
const output = await client.send(input);
|
||||
console.log(chalk.green("Assistant: ") + boldify(output));
|
||||
}
|
||||
rl.prompt();
|
||||
});
|
||||
};
|
||||
|
||||
// Utility function to replace **text** with chalk.bold(text)
|
||||
function boldify(text: string): string {
|
||||
return text.replace(/\*\*(.*?)\*\*/g, (_, value) => chalk.bold(value));
|
||||
}
|
||||
|
||||
init();
|
||||
Reference in New Issue
Block a user