nicechat
Version:
An extensible AI chat framework for OpenAi's models
87 lines (86 loc) • 4.08 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 __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.chatDeepSeek = chatDeepSeek;
const chalk_1 = __importDefault(require("chalk"));
const openai_1 = __importDefault(require("openai"));
const nicechat_1 = require("../nicechat");
const openai_2 = require("./openai");
const BASE_URL = "https://api.deepseek.com";
function chatDeepSeek(apiKey, model, systemMsg) {
return __awaiter(this, void 0, void 0, function* () {
var _a, e_1, _b, _c;
var _d, _e;
const openai = new openai_1.default({ apiKey, baseURL: BASE_URL });
(0, openai_2.printStarter)("deepseek", model, systemMsg);
const messages = [
system(systemMsg),
];
// initial use input
const input = yield (0, nicechat_1.readLine)();
messages.push(user(input));
while (true) {
// starting stream
const stream = yield openai.chat.completions.create({
model,
messages,
stream: true,
});
let msg = "";
try {
for (var _f = true, stream_1 = (e_1 = void 0, __asyncValues(stream)), stream_1_1; stream_1_1 = yield stream_1.next(), _a = stream_1_1.done, !_a; _f = true) {
_c = stream_1_1.value;
_f = false;
const part = _c;
// collect regular message
const p = ((_e = (_d = part.choices[0]) === null || _d === void 0 ? void 0 : _d.delta) === null || _e === void 0 ? void 0 : _e.content) || "";
if (p) {
process.stdout.write(chalk_1.default.greenBright(p));
msg += p;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_f && !_a && (_b = stream_1.return)) yield _b.call(stream_1);
}
finally { if (e_1) throw e_1.error; }
}
stream.controller.abort();
messages.push(assistant(msg));
// ask user for next input
console.log("\n");
const input = yield (0, nicechat_1.readLine)();
messages.push(user(input));
}
});
}
// helpers
function system(content) {
return { role: "system", content };
}
function user(content) {
return { role: "user", content };
}
function assistant(content) {
return { role: "assistant", content };
}