@sprucelabs/sprucebot-llm 12.1.0 → 12.3.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.
@@ -1,11 +1,24 @@
1
+ import { Log } from '@sprucelabs/spruce-skill-utils';
1
2
  import OpenAI from 'openai';
3
+ import { ReasoningEffort } from 'openai/resources';
2
4
  import { LlmAdapter, SendMessageOptions, SprucebotLlmBot } from '../../llm.types';
3
5
  export default class OpenAiAdapter implements LlmAdapter {
4
6
  static OpenAI: typeof OpenAI;
5
7
  private api;
6
- private log;
7
- protected constructor(apiKey: string);
8
- static Adapter(apiKey: string): OpenAiAdapter;
8
+ private log?;
9
+ private model;
10
+ private memoryLimit?;
11
+ private reasoningEffort?;
12
+ protected constructor(apiKey: string, options?: OpenAiAdapterOptions);
13
+ static Adapter(apiKey: string, options?: OpenAiAdapterOptions): OpenAiAdapter;
9
14
  sendMessage(bot: SprucebotLlmBot, options?: SendMessageOptions): Promise<string>;
15
+ private getReasoningEffort;
16
+ setModel(model: string): void;
17
+ setMessageMemoryLimit(limit: number): void;
18
+ setReasoningEffort(effort: ReasoningEffort): void;
10
19
  }
11
20
  export declare const MESSAGE_RESPONSE_ERROR_MESSAGE = "Oh no! Something went wrong and I can't talk right now!";
21
+ interface OpenAiAdapterOptions {
22
+ log?: Log;
23
+ }
24
+ export {};
@@ -5,36 +5,51 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.MESSAGE_RESPONSE_ERROR_MESSAGE = void 0;
7
7
  const schema_1 = require("@sprucelabs/schema");
8
- const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
9
8
  const openai_1 = __importDefault(require("openai"));
10
9
  const OpenAiMessageBuilder_1 = __importDefault(require("./OpenAiMessageBuilder"));
11
10
  class OpenAiAdapter {
12
- constructor(apiKey) {
13
- this.log = (0, spruce_skill_utils_1.buildLog)('SprucebotLLM::OpenAiAdapter');
11
+ constructor(apiKey, options) {
12
+ this.model = 'gpt-4o';
14
13
  (0, schema_1.assertOptions)({ apiKey }, ['apiKey']);
14
+ const { log } = options || {};
15
15
  this.api = new OpenAiAdapter.OpenAI({ apiKey });
16
+ this.log = log;
16
17
  }
17
- static Adapter(apiKey) {
18
- return new this(apiKey);
18
+ static Adapter(apiKey, options) {
19
+ return new this(apiKey, options);
19
20
  }
20
21
  async sendMessage(bot, options) {
21
- const messageBuilder = OpenAiMessageBuilder_1.default.Builder(bot);
22
+ const messageBuilder = OpenAiMessageBuilder_1.default.Builder(bot, {
23
+ memoryLimit: this.memoryLimit,
24
+ });
22
25
  const messages = messageBuilder.buildMessages();
23
- this.log.info('Sending message to OpenAI', JSON.stringify(messages, null, 2));
26
+ this.log?.info('Sending message to OpenAI', JSON.stringify(messages, null, 2));
24
27
  const params = {
25
28
  messages,
26
- model: options?.model ?? 'gpt-4o',
29
+ model: options?.model ?? this.model,
27
30
  };
28
- if (process.env.OPENAI_REASONING_EFFORT) {
29
- params.reasoning_effort = process.env
30
- .OPENAI_REASONING_EFFORT;
31
+ const reasoningEffort = this.getReasoningEffort();
32
+ if (reasoningEffort) {
33
+ params.reasoning_effort = reasoningEffort;
31
34
  }
32
35
  const response = await this.api.chat.completions.create(params);
33
36
  const message = response.choices?.[0]?.message?.content?.trim() ??
34
37
  exports.MESSAGE_RESPONSE_ERROR_MESSAGE;
35
- this.log.info('Received response from OpenAI', message);
38
+ this.log?.info('Received response from OpenAI', message);
36
39
  return message;
37
40
  }
41
+ getReasoningEffort() {
42
+ return (this.reasoningEffort ?? process.env.OPENAI_REASONING_EFFORT);
43
+ }
44
+ setModel(model) {
45
+ this.model = model;
46
+ }
47
+ setMessageMemoryLimit(limit) {
48
+ this.memoryLimit = limit;
49
+ }
50
+ setReasoningEffort(effort) {
51
+ this.reasoningEffort = effort;
52
+ }
38
53
  }
39
54
  OpenAiAdapter.OpenAI = openai_1.default;
40
55
  exports.default = OpenAiAdapter;
@@ -2,8 +2,9 @@ import { ChatCompletionMessageParam } from 'openai/resources';
2
2
  import { SprucebotLlmBot } from '../../llm.types';
3
3
  export default class OpenAiMessageBuilder {
4
4
  private bot;
5
- protected constructor(bot: SprucebotLlmBot);
6
- static Builder(bot: SprucebotLlmBot): OpenAiMessageBuilder;
5
+ private memoryLimit;
6
+ protected constructor(bot: SprucebotLlmBot, options?: BuildMessageOptions);
7
+ static Builder(bot: SprucebotLlmBot, options?: BuildMessageOptions): OpenAiMessageBuilder;
7
8
  buildMessages(): ChatCompletionMessageParam[];
8
9
  private buildChatHistoryMessages;
9
10
  private mapMessageToCompletion;
@@ -18,3 +19,7 @@ export default class OpenAiMessageBuilder {
18
19
  private buildWeAreDoneWhenMessage;
19
20
  private buildStateSchemaMessage;
20
21
  }
22
+ interface BuildMessageOptions {
23
+ memoryLimit?: number;
24
+ }
25
+ export {};
@@ -2,11 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const templates_1 = require("../templates");
4
4
  class OpenAiMessageBuilder {
5
- constructor(bot) {
5
+ constructor(bot, options) {
6
+ const { memoryLimit } = options ?? {};
6
7
  this.bot = bot;
8
+ this.memoryLimit =
9
+ memoryLimit ??
10
+ parseInt(process.env.OPENAI_MESSAGE_MEMORY_LIMIT ?? '0');
7
11
  }
8
- static Builder(bot) {
9
- return new this(bot);
12
+ static Builder(bot, options) {
13
+ return new this(bot, options);
10
14
  }
11
15
  buildMessages() {
12
16
  const values = this.bot.serialize();
@@ -19,9 +23,8 @@ class OpenAiMessageBuilder {
19
23
  }
20
24
  buildChatHistoryMessages(messages) {
21
25
  let messagesBeingConsidered = messages;
22
- const limit = parseInt(process.env.OPENAI_MESSAGE_MEMORY_LIMIT ?? '0');
23
- if (limit > 0) {
24
- messagesBeingConsidered = messages.slice(Math.max(messages.length - limit, 0));
26
+ if (this.memoryLimit > 0) {
27
+ messagesBeingConsidered = messages.slice(Math.max(messages.length - this.memoryLimit, 0));
25
28
  }
26
29
  const total = messagesBeingConsidered.length;
27
30
  return messagesBeingConsidered.map((message, idx) => this.mapMessageToCompletion(message, idx === total - 1));
@@ -1,11 +1,24 @@
1
+ import { Log } from '@sprucelabs/spruce-skill-utils';
1
2
  import OpenAI from 'openai';
3
+ import { ReasoningEffort } from 'openai/resources';
2
4
  import { LlmAdapter, SendMessageOptions, SprucebotLlmBot } from '../../llm.types';
3
5
  export default class OpenAiAdapter implements LlmAdapter {
4
6
  static OpenAI: typeof OpenAI;
5
7
  private api;
6
- private log;
7
- protected constructor(apiKey: string);
8
- static Adapter(apiKey: string): OpenAiAdapter;
8
+ private log?;
9
+ private model;
10
+ private memoryLimit?;
11
+ private reasoningEffort?;
12
+ protected constructor(apiKey: string, options?: OpenAiAdapterOptions);
13
+ static Adapter(apiKey: string, options?: OpenAiAdapterOptions): OpenAiAdapter;
9
14
  sendMessage(bot: SprucebotLlmBot, options?: SendMessageOptions): Promise<string>;
15
+ private getReasoningEffort;
16
+ setModel(model: string): void;
17
+ setMessageMemoryLimit(limit: number): void;
18
+ setReasoningEffort(effort: ReasoningEffort): void;
10
19
  }
11
20
  export declare const MESSAGE_RESPONSE_ERROR_MESSAGE = "Oh no! Something went wrong and I can't talk right now!";
21
+ interface OpenAiAdapterOptions {
22
+ log?: Log;
23
+ }
24
+ export {};
@@ -8,38 +8,54 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { assertOptions } from '@sprucelabs/schema';
11
- import { buildLog } from '@sprucelabs/spruce-skill-utils';
12
11
  import OpenAI from 'openai';
13
12
  import OpenAiMessageBuilder from './OpenAiMessageBuilder.js';
14
13
  class OpenAiAdapter {
15
- constructor(apiKey) {
16
- this.log = buildLog('SprucebotLLM::OpenAiAdapter');
14
+ constructor(apiKey, options) {
15
+ this.model = 'gpt-4o';
17
16
  assertOptions({ apiKey }, ['apiKey']);
17
+ const { log } = options || {};
18
18
  this.api = new OpenAiAdapter.OpenAI({ apiKey });
19
+ this.log = log;
19
20
  }
20
- static Adapter(apiKey) {
21
- return new this(apiKey);
21
+ static Adapter(apiKey, options) {
22
+ return new this(apiKey, options);
22
23
  }
23
24
  sendMessage(bot, options) {
24
25
  return __awaiter(this, void 0, void 0, function* () {
25
- var _a, _b, _c, _d, _e, _f;
26
- const messageBuilder = OpenAiMessageBuilder.Builder(bot);
26
+ var _a, _b, _c, _d, _e, _f, _g, _h;
27
+ const messageBuilder = OpenAiMessageBuilder.Builder(bot, {
28
+ memoryLimit: this.memoryLimit,
29
+ });
27
30
  const messages = messageBuilder.buildMessages();
28
- this.log.info('Sending message to OpenAI', JSON.stringify(messages, null, 2));
31
+ (_a = this.log) === null || _a === void 0 ? void 0 : _a.info('Sending message to OpenAI', JSON.stringify(messages, null, 2));
29
32
  const params = {
30
33
  messages,
31
- model: (_a = options === null || options === void 0 ? void 0 : options.model) !== null && _a !== void 0 ? _a : 'gpt-4o',
34
+ model: (_b = options === null || options === void 0 ? void 0 : options.model) !== null && _b !== void 0 ? _b : this.model,
32
35
  };
33
- if (process.env.OPENAI_REASONING_EFFORT) {
34
- params.reasoning_effort = process.env
35
- .OPENAI_REASONING_EFFORT;
36
+ const reasoningEffort = this.getReasoningEffort();
37
+ if (reasoningEffort) {
38
+ params.reasoning_effort = reasoningEffort;
36
39
  }
37
40
  const response = yield this.api.chat.completions.create(params);
38
- const message = (_f = (_e = (_d = (_c = (_b = response.choices) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.content) === null || _e === void 0 ? void 0 : _e.trim()) !== null && _f !== void 0 ? _f : MESSAGE_RESPONSE_ERROR_MESSAGE;
39
- this.log.info('Received response from OpenAI', message);
41
+ const message = (_g = (_f = (_e = (_d = (_c = response.choices) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.message) === null || _e === void 0 ? void 0 : _e.content) === null || _f === void 0 ? void 0 : _f.trim()) !== null && _g !== void 0 ? _g : MESSAGE_RESPONSE_ERROR_MESSAGE;
42
+ (_h = this.log) === null || _h === void 0 ? void 0 : _h.info('Received response from OpenAI', message);
40
43
  return message;
41
44
  });
42
45
  }
46
+ getReasoningEffort() {
47
+ var _a;
48
+ return ((_a = this.reasoningEffort) !== null && _a !== void 0 ? _a : process.env.OPENAI_REASONING_EFFORT);
49
+ }
50
+ setModel(model) {
51
+ this.model = model;
52
+ }
53
+ setMessageMemoryLimit(limit) {
54
+ this.memoryLimit = limit;
55
+ }
56
+ setReasoningEffort(effort) {
57
+ this.reasoningEffort = effort;
58
+ }
43
59
  }
44
60
  OpenAiAdapter.OpenAI = OpenAI;
45
61
  export default OpenAiAdapter;
@@ -2,8 +2,9 @@ import { ChatCompletionMessageParam } from 'openai/resources';
2
2
  import { SprucebotLlmBot } from '../../llm.types';
3
3
  export default class OpenAiMessageBuilder {
4
4
  private bot;
5
- protected constructor(bot: SprucebotLlmBot);
6
- static Builder(bot: SprucebotLlmBot): OpenAiMessageBuilder;
5
+ private memoryLimit;
6
+ protected constructor(bot: SprucebotLlmBot, options?: BuildMessageOptions);
7
+ static Builder(bot: SprucebotLlmBot, options?: BuildMessageOptions): OpenAiMessageBuilder;
7
8
  buildMessages(): ChatCompletionMessageParam[];
8
9
  private buildChatHistoryMessages;
9
10
  private mapMessageToCompletion;
@@ -18,3 +19,7 @@ export default class OpenAiMessageBuilder {
18
19
  private buildWeAreDoneWhenMessage;
19
20
  private buildStateSchemaMessage;
20
21
  }
22
+ interface BuildMessageOptions {
23
+ memoryLimit?: number;
24
+ }
25
+ export {};
@@ -1,10 +1,14 @@
1
1
  import { DONE_TOKEN, STATE_BOUNDARY } from '../templates.js';
2
2
  export default class OpenAiMessageBuilder {
3
- constructor(bot) {
3
+ constructor(bot, options) {
4
+ var _a;
5
+ const { memoryLimit } = options !== null && options !== void 0 ? options : {};
4
6
  this.bot = bot;
7
+ this.memoryLimit =
8
+ memoryLimit !== null && memoryLimit !== void 0 ? memoryLimit : parseInt((_a = process.env.OPENAI_MESSAGE_MEMORY_LIMIT) !== null && _a !== void 0 ? _a : '0');
5
9
  }
6
- static Builder(bot) {
7
- return new this(bot);
10
+ static Builder(bot, options) {
11
+ return new this(bot, options);
8
12
  }
9
13
  buildMessages() {
10
14
  const values = this.bot.serialize();
@@ -16,11 +20,9 @@ export default class OpenAiMessageBuilder {
16
20
  return allMessages;
17
21
  }
18
22
  buildChatHistoryMessages(messages) {
19
- var _a;
20
23
  let messagesBeingConsidered = messages;
21
- const limit = parseInt((_a = process.env.OPENAI_MESSAGE_MEMORY_LIMIT) !== null && _a !== void 0 ? _a : '0');
22
- if (limit > 0) {
23
- messagesBeingConsidered = messages.slice(Math.max(messages.length - limit, 0));
24
+ if (this.memoryLimit > 0) {
25
+ messagesBeingConsidered = messages.slice(Math.max(messages.length - this.memoryLimit, 0));
24
26
  }
25
27
  const total = messagesBeingConsidered.length;
26
28
  return messagesBeingConsidered.map((message, idx) => this.mapMessageToCompletion(message, idx === total - 1));
@@ -1,5 +1,6 @@
1
1
  import { MercuryEventEmitter } from '@sprucelabs/mercury-types';
2
2
  import { Schema, SchemaValues, SelectFieldDefinition } from '@sprucelabs/schema';
3
+ import { ReasoningEffort } from 'openai/resources';
3
4
  export interface BotOptions<StateSchema extends Schema = Schema, State extends SchemaValues<StateSchema> = SchemaValues<StateSchema>> extends Omit<PromptOptions<StateSchema, State>, 'skill'> {
4
5
  adapter: LlmAdapter;
5
6
  Class?: new (...opts: any[]) => SprucebotLlmBot<Schema, State>;
@@ -83,3 +84,4 @@ export interface SendMessageWithImage {
83
84
  imageDescription: string;
84
85
  imageBase64: string;
85
86
  }
87
+ export type OpenAiReasoningEffort = ReasoningEffort;
@@ -1,5 +1,6 @@
1
1
  import { MercuryEventEmitter } from '@sprucelabs/mercury-types';
2
2
  import { Schema, SchemaValues, SelectFieldDefinition } from '@sprucelabs/schema';
3
+ import { ReasoningEffort } from 'openai/resources';
3
4
  export interface BotOptions<StateSchema extends Schema = Schema, State extends SchemaValues<StateSchema> = SchemaValues<StateSchema>> extends Omit<PromptOptions<StateSchema, State>, 'skill'> {
4
5
  adapter: LlmAdapter;
5
6
  Class?: new (...opts: any[]) => SprucebotLlmBot<Schema, State>;
@@ -83,3 +84,4 @@ export interface SendMessageWithImage {
83
84
  imageDescription: string;
84
85
  imageBase64: string;
85
86
  }
87
+ export type OpenAiReasoningEffort = ReasoningEffort;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "eta"
9
9
  ]
10
10
  },
11
- "version": "12.1.0",
11
+ "version": "12.3.0",
12
12
  "files": [
13
13
  "build"
14
14
  ],