@proteinjs/conversation 1.5.1 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ /// <reference types="node" />
2
+ import { ChatCompletionMessageToolCall, ChatCompletionChunk } from 'openai/resources/chat';
3
+ import { LogLevel } from '@proteinjs/util';
4
+ import { Stream } from 'openai/streaming';
5
+ import { Readable } from 'stream';
6
+ /**
7
+ * Processes streaming responses from OpenAI's `ChatCompletions` api.
8
+ * - When a tool call is received, it delegates processing to `onToolCalls`; this can happen recursively
9
+ * - When a response to the user is received, it writes to `outputStream`
10
+ */
11
+ export declare class OpenAiStreamProcessor {
12
+ private onToolCalls;
13
+ private logger;
14
+ private accumulatedToolCalls;
15
+ private toolCallsExecuted;
16
+ private currentToolCall;
17
+ private inputStream;
18
+ private controlStream;
19
+ private outputStream;
20
+ constructor(inputStream: Stream<ChatCompletionChunk>, onToolCalls: (toolCalls: ChatCompletionMessageToolCall[], currentFunctionCalls: number) => Promise<Readable>, logLevel: LogLevel);
21
+ /**
22
+ * @returns a `Readable` stream that will receive the assistant's text response to the user
23
+ */
24
+ getOutputStream(): Readable;
25
+ /**
26
+ * @returns a `Transform` that parses the input stream and delegates to tool calls or writes a text response to the user
27
+ */
28
+ private createControlStream;
29
+ /**
30
+ * Destroy all streams used by `OpenAiStreamProcessor`
31
+ */
32
+ private destroyStreams;
33
+ /**
34
+ * Accumulates tool call deltas into complete tool calls.
35
+ * @param toolCallDeltas `ChatCompletionChunk.Choice.Delta.ToolCall` objects that contain part of a complete `ChatCompletionMessageToolCall`
36
+ */
37
+ private handleToolCallDelta;
38
+ /**
39
+ * Delegates `ChatCompletionMessageToolCall`s to `onToolCalls`.
40
+ * - Manages refreshing the `inputStream` and `controlStream`
41
+ * - Manages tool call state (such as keeping track of the number of tool calls made)
42
+ */
43
+ private handleToolCalls;
44
+ }
45
+ //# sourceMappingURL=OpenAiStreamProcessor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OpenAiStreamProcessor.d.ts","sourceRoot":"","sources":["../../src/OpenAiStreamProcessor.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,6BAA6B,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC3F,OAAO,EAAE,QAAQ,EAAU,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAA6C,MAAM,QAAQ,CAAC;AAE7E;;;;GAIG;AACH,qBAAa,qBAAqB;IAW9B,OAAO,CAAC,WAAW;IAVrB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,oBAAoB,CAAgD;IAC5E,OAAO,CAAC,iBAAiB,CAAK;IAC9B,OAAO,CAAC,eAAe,CAAuD;IAC9E,OAAO,CAAC,WAAW,CAAW;IAC9B,OAAO,CAAC,aAAa,CAAY;IACjC,OAAO,CAAC,YAAY,CAAW;gBAG7B,WAAW,EAAE,MAAM,CAAC,mBAAmB,CAAC,EAChC,WAAW,EAAE,CACnB,SAAS,EAAE,6BAA6B,EAAE,EAC1C,oBAAoB,EAAE,MAAM,KACzB,OAAO,CAAC,QAAQ,CAAC,EACtB,QAAQ,EAAE,QAAQ;IASpB;;OAEG;IACH,eAAe,IAAI,QAAQ;IAI3B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAwC3B;;OAEG;IACH,OAAO,CAAC,cAAc;IAOtB;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IA2B3B;;;;OAIG;YACW,eAAe;CA0B9B"}
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.OpenAiStreamProcessor = void 0;
40
+ var util_1 = require("@proteinjs/util");
41
+ var stream_1 = require("stream");
42
+ /**
43
+ * Processes streaming responses from OpenAI's `ChatCompletions` api.
44
+ * - When a tool call is received, it delegates processing to `onToolCalls`; this can happen recursively
45
+ * - When a response to the user is received, it writes to `outputStream`
46
+ */
47
+ var OpenAiStreamProcessor = /** @class */ (function () {
48
+ function OpenAiStreamProcessor(inputStream, onToolCalls, logLevel) {
49
+ this.onToolCalls = onToolCalls;
50
+ this.accumulatedToolCalls = [];
51
+ this.toolCallsExecuted = 0;
52
+ this.currentToolCall = null;
53
+ this.logger = new util_1.Logger(this.constructor.name, logLevel);
54
+ this.inputStream = stream_1.Readable.from(inputStream);
55
+ this.controlStream = this.createControlStream();
56
+ this.outputStream = new stream_1.PassThrough();
57
+ this.inputStream.pipe(this.controlStream);
58
+ }
59
+ /**
60
+ * @returns a `Readable` stream that will receive the assistant's text response to the user
61
+ */
62
+ OpenAiStreamProcessor.prototype.getOutputStream = function () {
63
+ return this.outputStream;
64
+ };
65
+ /**
66
+ * @returns a `Transform` that parses the input stream and delegates to tool calls or writes a text response to the user
67
+ */
68
+ OpenAiStreamProcessor.prototype.createControlStream = function () {
69
+ var _this = this;
70
+ return new stream_1.Transform({
71
+ objectMode: true,
72
+ transform: function (chunk, encoding, callback) {
73
+ var _a, _b, _c, _d, _e, _f, _g, _h;
74
+ try {
75
+ if (_this.outputStream.destroyed) {
76
+ _this.logger.warn("Destroying input and control streams since output stream is destroyed");
77
+ _this.inputStream.destroy();
78
+ _this.controlStream.destroy();
79
+ return;
80
+ }
81
+ if (!chunk || !chunk.choices) {
82
+ throw new Error("Received invalid chunk:\n".concat(JSON.stringify(chunk, null, 2)));
83
+ }
84
+ else if (typeof ((_b = (_a = chunk.choices[0]) === null || _a === void 0 ? void 0 : _a.delta) === null || _b === void 0 ? void 0 : _b.content) === 'string') {
85
+ _this.outputStream.push(chunk.choices[0].delta.content);
86
+ }
87
+ else if ((_d = (_c = chunk.choices[0]) === null || _c === void 0 ? void 0 : _c.delta) === null || _d === void 0 ? void 0 : _d.tool_calls) {
88
+ _this.handleToolCallDelta(chunk.choices[0].delta.tool_calls);
89
+ }
90
+ else if (((_e = chunk.choices[0]) === null || _e === void 0 ? void 0 : _e.finish_reason) === 'tool_calls') {
91
+ _this.handleToolCalls();
92
+ }
93
+ else if (((_f = chunk.choices[0]) === null || _f === void 0 ? void 0 : _f.finish_reason) === 'stop') {
94
+ _this.outputStream.push(null);
95
+ }
96
+ else if (((_g = chunk.choices[0]) === null || _g === void 0 ? void 0 : _g.finish_reason) === 'length') {
97
+ _this.logger.warn("The maximum number of tokens specified in the request was reached");
98
+ _this.outputStream.push(null);
99
+ }
100
+ else if (((_h = chunk.choices[0]) === null || _h === void 0 ? void 0 : _h.finish_reason) === 'content_filter') {
101
+ _this.logger.error("Content was omitted due to a flag from OpenAI's content filters");
102
+ _this.outputStream.push(null);
103
+ }
104
+ else {
105
+ throw new Error("Received invalid finish reason or delta:\n".concat(JSON.stringify(chunk, null, 2)));
106
+ }
107
+ callback();
108
+ }
109
+ catch (error) {
110
+ _this.logger.error('Error tranforming chunk', error);
111
+ _this.destroyStreams(error);
112
+ }
113
+ },
114
+ });
115
+ };
116
+ /**
117
+ * Destroy all streams used by `OpenAiStreamProcessor`
118
+ */
119
+ OpenAiStreamProcessor.prototype.destroyStreams = function (error) {
120
+ this.inputStream.destroy();
121
+ this.controlStream.destroy();
122
+ this.outputStream.emit('error', error);
123
+ this.outputStream.destroy();
124
+ };
125
+ /**
126
+ * Accumulates tool call deltas into complete tool calls.
127
+ * @param toolCallDeltas `ChatCompletionChunk.Choice.Delta.ToolCall` objects that contain part of a complete `ChatCompletionMessageToolCall`
128
+ */
129
+ OpenAiStreamProcessor.prototype.handleToolCallDelta = function (toolCallDeltas) {
130
+ var _a, _b, _c, _d;
131
+ for (var _i = 0, toolCallDeltas_1 = toolCallDeltas; _i < toolCallDeltas_1.length; _i++) {
132
+ var delta = toolCallDeltas_1[_i];
133
+ if (delta.id) {
134
+ // Start of a new tool call
135
+ if (this.currentToolCall) {
136
+ this.accumulatedToolCalls.push(this.currentToolCall);
137
+ }
138
+ this.currentToolCall = {
139
+ id: delta.id,
140
+ type: delta.type || 'function',
141
+ function: {
142
+ name: ((_a = delta.function) === null || _a === void 0 ? void 0 : _a.name) || '',
143
+ arguments: ((_b = delta.function) === null || _b === void 0 ? void 0 : _b.arguments) || '',
144
+ },
145
+ };
146
+ }
147
+ else {
148
+ // Continue building the current tool call
149
+ if ((_c = delta.function) === null || _c === void 0 ? void 0 : _c.name) {
150
+ this.currentToolCall.function.name += delta.function.name;
151
+ }
152
+ if ((_d = delta.function) === null || _d === void 0 ? void 0 : _d.arguments) {
153
+ this.currentToolCall.function.arguments += delta.function.arguments;
154
+ }
155
+ }
156
+ }
157
+ };
158
+ /**
159
+ * Delegates `ChatCompletionMessageToolCall`s to `onToolCalls`.
160
+ * - Manages refreshing the `inputStream` and `controlStream`
161
+ * - Manages tool call state (such as keeping track of the number of tool calls made)
162
+ */
163
+ OpenAiStreamProcessor.prototype.handleToolCalls = function () {
164
+ return __awaiter(this, void 0, void 0, function () {
165
+ var completedToolCalls, _a, error_1;
166
+ var _this = this;
167
+ return __generator(this, function (_b) {
168
+ switch (_b.label) {
169
+ case 0:
170
+ if (this.currentToolCall) {
171
+ this.accumulatedToolCalls.push(this.currentToolCall);
172
+ this.currentToolCall = null;
173
+ }
174
+ completedToolCalls = this.accumulatedToolCalls.filter(function (tc) {
175
+ return tc.id !== undefined && tc.function !== undefined && tc.type !== undefined;
176
+ });
177
+ this.accumulatedToolCalls = [];
178
+ this.inputStream.destroy();
179
+ this.controlStream.destroy();
180
+ this.controlStream = this.createControlStream();
181
+ _b.label = 1;
182
+ case 1:
183
+ _b.trys.push([1, 3, , 4]);
184
+ _a = this;
185
+ return [4 /*yield*/, this.onToolCalls(completedToolCalls, this.toolCallsExecuted)];
186
+ case 2:
187
+ _a.inputStream = _b.sent();
188
+ this.inputStream.on('error', function (error) { return _this.destroyStreams(error); });
189
+ this.inputStream.pipe(this.controlStream);
190
+ this.toolCallsExecuted += completedToolCalls.length;
191
+ return [3 /*break*/, 4];
192
+ case 3:
193
+ error_1 = _b.sent();
194
+ this.logger.error('Error processing tool calls:', error_1);
195
+ this.destroyStreams(error_1);
196
+ return [3 /*break*/, 4];
197
+ case 4: return [2 /*return*/];
198
+ }
199
+ });
200
+ });
201
+ };
202
+ return OpenAiStreamProcessor;
203
+ }());
204
+ exports.OpenAiStreamProcessor = OpenAiStreamProcessor;
205
+ //# sourceMappingURL=OpenAiStreamProcessor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OpenAiStreamProcessor.js","sourceRoot":"","sources":["../../src/OpenAiStreamProcessor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,wCAAmD;AAEnD,iCAA6E;AAE7E;;;;GAIG;AACH;IASE,+BACE,WAAwC,EAChC,WAGc,EACtB,QAAkB;QAJV,gBAAW,GAAX,WAAW,CAGG;QAZhB,yBAAoB,GAA6C,EAAE,CAAC;QACpE,sBAAiB,GAAG,CAAC,CAAC;QACtB,oBAAe,GAAkD,IAAI,CAAC;QAa5E,IAAI,CAAC,MAAM,GAAG,IAAI,aAAM,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC1D,IAAI,CAAC,WAAW,GAAG,iBAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC9C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,mBAAmB,EAAE,CAAC;QAChD,IAAI,CAAC,YAAY,GAAG,IAAI,oBAAW,EAAE,CAAC;QACtC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAC5C,CAAC;IAED;;OAEG;IACH,+CAAe,GAAf;QACE,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED;;OAEG;I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EAAE;4BACD,OAAA,EAAE,CAAC,EAAE,KAAK,SAAS,IAAI,EAAE,CAAC,QAAQ,KAAK,SAAS,IAAI,EAAE,CAAC,IAAI,KAAK,SAAS;wBAAzE,CAAyE,CAC5E,CAAC;wBAEF,IAAI,CAAC,oBAAoB,GAAG,EAAE,CAAC;wBAC/B,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;wBAC3B,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,CAAC;wBAC7B,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,mBAAmB,EAAE,CAAC;;;;wBAG9C,KAAA,IAAI,CAAA;wBAAe,qBAAM,IAAI,CAAC,WAAW,CAAC,kBAAkB,EAAE,IAAI,CAAC,iBAAiB,CAAC,EAAA;;wBAArF,GAAK,WAAW,GAAG,SAAkE,CAAC;wBACtF,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,UAAC,KAAK,IAAK,OAAA,KAAI,CAAC,cAAc,CAAC,KAAK,CAAC,EAA1B,CAA0B,CAAC,CAAC;wBACpE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;wBAC1C,IAAI,CAAC,iBAAiB,IAAI,kBAAkB,CAAC,MAAM,CAAC;;;;wBAEpD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,8BAA8B,EAAE,OAAK,CAAC,CAAC;wBACzD,IAAI,CAAC,cAAc,CAAC,OAAK,CAAC,CAAC;;;;;;KAE9B;IACH,4BAAC;AAAD,CAAC,AAlJD,IAkJC;AAlJY,sDAAqB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proteinjs/conversation",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "publishConfig": {
@@ -35,5 +35,6 @@
35
35
  "readline-sync": "1.4.10",
36
36
  "tiktoken": "1.0.15",
37
37
  "typescript": "5.2.2"
38
- }
39
- }
38
+ },
39
+ "gitHead": "68361629fee0eb5cca527a87c9479fbc85cecf3c"
40
+ }
@@ -58,8 +58,13 @@ export class Conversation {
58
58
  continue;
59
59
  }
60
60
 
61
+ const moduleSystemMessages = module.getSystemMessages();
62
+ const formattedSystemMessages = Array.isArray(moduleSystemMessages)
63
+ ? moduleSystemMessages.join('. ')
64
+ : moduleSystemMessages;
65
+
61
66
  this.addSystemMessagesToHistory([
62
- `The following are instructions from the ${module.getName()} module: ${module.getSystemMessages().join('. ')}`,
67
+ `The following are instructions from the ${module.getName()} module: ${formattedSystemMessages}`,
63
68
  ]);
64
69
  this.addFunctions(module.getName(), module.getFunctions());
65
70
  this.addMessageModerators(module.getMessageModerators());
@@ -204,6 +209,18 @@ export class Conversation {
204
209
  );
205
210
  }
206
211
 
212
+ async generateStreamingResponse(messages: (string | ChatCompletionMessageParam)[], model?: TiktokenModel) {
213
+ await this.enforceTokenLimit(messages, model);
214
+ return await OpenAi.generateStreamingResponse(
215
+ messages,
216
+ model,
217
+ this.history,
218
+ this.functions,
219
+ this.messageModerators,
220
+ this.params.logLevel
221
+ );
222
+ }
223
+
207
224
  async generateCode(description: string[], model?: TiktokenModel) {
208
225
  this.logger.info(`Generating code for description:\n${description.join('\n')}`);
209
226
  const code = await OpenAi.generateCode(
@@ -3,7 +3,8 @@ import { MessageModerator } from './history/MessageModerator';
3
3
 
4
4
  export interface ConversationModule {
5
5
  getName(): string;
6
- getSystemMessages(): string[];
6
+ /** Return array of strings that will be formatted with periods in between or return a preformatted string */
7
+ getSystemMessages(): string[] | string;
7
8
  getFunctions(): Function[];
8
9
  getMessageModerators(): MessageModerator[];
9
10
  }