nicechat
Version:
An extensible AI chat framework for OpenAi's models
77 lines (76 loc) • 2.74 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.chat = void 0;
const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
const chalk_1 = __importDefault(require("chalk"));
const nicechat_1 = require("./nicechat");
const MAX_TOKENS = 1024;
function chat(apiKey, model, system) {
return __awaiter(this, void 0, void 0, function* () {
const client = new sdk_1.default({
apiKey,
});
console.log("[" + chalk_1.default.blueBright(system) + "]");
const messages = [];
// initial use input
const input = yield (0, nicechat_1.readLine)();
messages.push(user(input));
while (true) {
const response = yield exchange(client, model, system, messages);
messages.push(assistant(response));
// ask user for next input
console.log("\n");
const input = yield (0, nicechat_1.readLine)();
messages.push(user(input));
}
});
}
exports.chat = chat;
function user(content) {
return {
role: "user",
content,
};
}
function assistant(content) {
return {
role: "assistant",
content,
};
}
function exchange(client, model, system, messages) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve) => {
let msg = "";
client.messages
.stream({
messages,
model,
max_tokens: MAX_TOKENS,
system,
})
.on("text", (p) => {
// collect regular message
if (p) {
process.stdout.write(chalk_1.default.greenBright(p));
msg += p;
}
})
.on("end", () => {
resolve(msg);
});
});
});
}