UNPKG

@barosell/streamllm

Version:

Support streaming LLM responses using rxjs observables

64 lines (63 loc) 3.41 kB
"use strict"; 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); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AiInterface = void 0; const rxjs_1 = require("rxjs"); class AiInterface { constructor(openai, model) { this.openai = openai; this.model = model; } stream(completions) { const deltas = new rxjs_1.Subject(); (() => __awaiter(this, void 0, void 0, function* () { var _a, e_1, _b, _c; var _d, _e, _f, _g; try { const stream = yield this.openai.chat.completions.create({ messages: completions.map(c => ({ role: c.role.toLowerCase(), content: c.content })), model: this.model, stream: true, }); let content = ''; try { for (var _h = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _a = stream_1_1.done, !_a; _h = true) { _c = stream_1_1.value; _h = false; const part = _c; content += ((_e = (_d = part.choices[0]) === null || _d === void 0 ? void 0 : _d.delta) === null || _e === void 0 ? void 0 : _e.content) || ''; deltas.next(((_g = (_f = part === null || part === void 0 ? void 0 : part.choices[0]) === null || _f === void 0 ? void 0 : _f.delta) === null || _g === void 0 ? void 0 : _g.content) || ''); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_h && !_a && (_b = stream_1.return)) yield _b.call(stream_1); } finally { if (e_1) throw e_1.error; } } deltas.complete(); } catch (error) { deltas.error(error); } }))(); return deltas; } } exports.AiInterface = AiInterface;