@sprucelabs/sprucebot-llm 12.1.0 → 12.2.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.
- package/build/bots/adapters/OpenAi.d.ts +8 -0
- package/build/bots/adapters/OpenAi.js +20 -5
- package/build/bots/adapters/OpenAiMessageBuilder.d.ts +7 -2
- package/build/bots/adapters/OpenAiMessageBuilder.js +9 -6
- package/build/esm/bots/adapters/OpenAi.d.ts +8 -0
- package/build/esm/bots/adapters/OpenAi.js +21 -5
- package/build/esm/bots/adapters/OpenAiMessageBuilder.d.ts +7 -2
- package/build/esm/bots/adapters/OpenAiMessageBuilder.js +9 -7
- package/build/esm/llm.types.d.ts +2 -0
- package/build/llm.types.d.ts +2 -0
- package/package.json +1 -1
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import OpenAI from 'openai';
|
|
2
|
+
import { ReasoningEffort } from 'openai/resources';
|
|
2
3
|
import { LlmAdapter, SendMessageOptions, SprucebotLlmBot } from '../../llm.types';
|
|
3
4
|
export default class OpenAiAdapter implements LlmAdapter {
|
|
4
5
|
static OpenAI: typeof OpenAI;
|
|
5
6
|
private api;
|
|
6
7
|
private log;
|
|
8
|
+
private model;
|
|
9
|
+
private memoryLimit?;
|
|
10
|
+
private reasoningEffort?;
|
|
7
11
|
protected constructor(apiKey: string);
|
|
8
12
|
static Adapter(apiKey: string): OpenAiAdapter;
|
|
9
13
|
sendMessage(bot: SprucebotLlmBot, options?: SendMessageOptions): Promise<string>;
|
|
14
|
+
private getReasoningEffort;
|
|
15
|
+
setModel(model: string): void;
|
|
16
|
+
setMessageMemoryLimit(limit: number): void;
|
|
17
|
+
setReasoningEffort(effort: ReasoningEffort): void;
|
|
10
18
|
}
|
|
11
19
|
export declare const MESSAGE_RESPONSE_ERROR_MESSAGE = "Oh no! Something went wrong and I can't talk right now!";
|
|
@@ -11,6 +11,7 @@ const OpenAiMessageBuilder_1 = __importDefault(require("./OpenAiMessageBuilder")
|
|
|
11
11
|
class OpenAiAdapter {
|
|
12
12
|
constructor(apiKey) {
|
|
13
13
|
this.log = (0, spruce_skill_utils_1.buildLog)('SprucebotLLM::OpenAiAdapter');
|
|
14
|
+
this.model = 'gpt-4o';
|
|
14
15
|
(0, schema_1.assertOptions)({ apiKey }, ['apiKey']);
|
|
15
16
|
this.api = new OpenAiAdapter.OpenAI({ apiKey });
|
|
16
17
|
}
|
|
@@ -18,16 +19,18 @@ class OpenAiAdapter {
|
|
|
18
19
|
return new this(apiKey);
|
|
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
26
|
this.log.info('Sending message to OpenAI', JSON.stringify(messages, null, 2));
|
|
24
27
|
const params = {
|
|
25
28
|
messages,
|
|
26
|
-
model: options?.model ??
|
|
29
|
+
model: options?.model ?? this.model,
|
|
27
30
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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() ??
|
|
@@ -35,6 +38,18 @@ class OpenAiAdapter {
|
|
|
35
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
|
-
|
|
6
|
-
|
|
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
|
-
|
|
23
|
-
|
|
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,19 @@
|
|
|
1
1
|
import OpenAI from 'openai';
|
|
2
|
+
import { ReasoningEffort } from 'openai/resources';
|
|
2
3
|
import { LlmAdapter, SendMessageOptions, SprucebotLlmBot } from '../../llm.types';
|
|
3
4
|
export default class OpenAiAdapter implements LlmAdapter {
|
|
4
5
|
static OpenAI: typeof OpenAI;
|
|
5
6
|
private api;
|
|
6
7
|
private log;
|
|
8
|
+
private model;
|
|
9
|
+
private memoryLimit?;
|
|
10
|
+
private reasoningEffort?;
|
|
7
11
|
protected constructor(apiKey: string);
|
|
8
12
|
static Adapter(apiKey: string): OpenAiAdapter;
|
|
9
13
|
sendMessage(bot: SprucebotLlmBot, options?: SendMessageOptions): Promise<string>;
|
|
14
|
+
private getReasoningEffort;
|
|
15
|
+
setModel(model: string): void;
|
|
16
|
+
setMessageMemoryLimit(limit: number): void;
|
|
17
|
+
setReasoningEffort(effort: ReasoningEffort): void;
|
|
10
18
|
}
|
|
11
19
|
export declare const MESSAGE_RESPONSE_ERROR_MESSAGE = "Oh no! Something went wrong and I can't talk right now!";
|
|
@@ -14,6 +14,7 @@ import OpenAiMessageBuilder from './OpenAiMessageBuilder.js';
|
|
|
14
14
|
class OpenAiAdapter {
|
|
15
15
|
constructor(apiKey) {
|
|
16
16
|
this.log = buildLog('SprucebotLLM::OpenAiAdapter');
|
|
17
|
+
this.model = 'gpt-4o';
|
|
17
18
|
assertOptions({ apiKey }, ['apiKey']);
|
|
18
19
|
this.api = new OpenAiAdapter.OpenAI({ apiKey });
|
|
19
20
|
}
|
|
@@ -23,16 +24,18 @@ class OpenAiAdapter {
|
|
|
23
24
|
sendMessage(bot, options) {
|
|
24
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
25
26
|
var _a, _b, _c, _d, _e, _f;
|
|
26
|
-
const messageBuilder = OpenAiMessageBuilder.Builder(bot
|
|
27
|
+
const messageBuilder = OpenAiMessageBuilder.Builder(bot, {
|
|
28
|
+
memoryLimit: this.memoryLimit,
|
|
29
|
+
});
|
|
27
30
|
const messages = messageBuilder.buildMessages();
|
|
28
31
|
this.log.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 :
|
|
34
|
+
model: (_a = options === null || options === void 0 ? void 0 : options.model) !== null && _a !== void 0 ? _a : this.model,
|
|
32
35
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
41
|
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;
|
|
@@ -40,6 +43,19 @@ class OpenAiAdapter {
|
|
|
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
|
-
|
|
6
|
-
|
|
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
|
-
|
|
22
|
-
|
|
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));
|
package/build/esm/llm.types.d.ts
CHANGED
|
@@ -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/build/llm.types.d.ts
CHANGED
|
@@ -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;
|