@elizaos/plugin-openai 1.0.0-beta.4 → 1.0.0-beta.41
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/LICENSE +1 -1
- package/README.md +8 -2
- package/dist/index.js +1211 -228
- package/dist/index.js.map +1 -1
- package/package.json +8 -7
package/dist/index.js
CHANGED
|
@@ -1,11 +1,584 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { createOpenAI } from "@ai-sdk/openai";
|
|
3
|
+
import { getProviderBaseURL } from "@elizaos/core";
|
|
3
4
|
import {
|
|
5
|
+
EventType,
|
|
6
|
+
logger,
|
|
4
7
|
ModelType,
|
|
5
|
-
|
|
8
|
+
VECTOR_DIMS,
|
|
9
|
+
safeReplacer,
|
|
10
|
+
ServiceType
|
|
6
11
|
} from "@elizaos/core";
|
|
7
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
generateObject,
|
|
14
|
+
generateText,
|
|
15
|
+
JSONParseError
|
|
16
|
+
} from "ai";
|
|
8
17
|
import { encodingForModel } from "js-tiktoken";
|
|
18
|
+
import { fetch, FormData } from "undici";
|
|
19
|
+
|
|
20
|
+
// ../../node_modules/@opentelemetry/api/build/esm/platform/node/globalThis.js
|
|
21
|
+
var _globalThis = typeof globalThis === "object" ? globalThis : global;
|
|
22
|
+
|
|
23
|
+
// ../../node_modules/@opentelemetry/api/build/esm/version.js
|
|
24
|
+
var VERSION = "1.9.0";
|
|
25
|
+
|
|
26
|
+
// ../../node_modules/@opentelemetry/api/build/esm/internal/semver.js
|
|
27
|
+
var re = /^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;
|
|
28
|
+
function _makeCompatibilityCheck(ownVersion) {
|
|
29
|
+
var acceptedVersions = /* @__PURE__ */ new Set([ownVersion]);
|
|
30
|
+
var rejectedVersions = /* @__PURE__ */ new Set();
|
|
31
|
+
var myVersionMatch = ownVersion.match(re);
|
|
32
|
+
if (!myVersionMatch) {
|
|
33
|
+
return function() {
|
|
34
|
+
return false;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
var ownVersionParsed = {
|
|
38
|
+
major: +myVersionMatch[1],
|
|
39
|
+
minor: +myVersionMatch[2],
|
|
40
|
+
patch: +myVersionMatch[3],
|
|
41
|
+
prerelease: myVersionMatch[4]
|
|
42
|
+
};
|
|
43
|
+
if (ownVersionParsed.prerelease != null) {
|
|
44
|
+
return function isExactmatch(globalVersion) {
|
|
45
|
+
return globalVersion === ownVersion;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function _reject(v) {
|
|
49
|
+
rejectedVersions.add(v);
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
function _accept(v) {
|
|
53
|
+
acceptedVersions.add(v);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return function isCompatible2(globalVersion) {
|
|
57
|
+
if (acceptedVersions.has(globalVersion)) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
if (rejectedVersions.has(globalVersion)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
var globalVersionMatch = globalVersion.match(re);
|
|
64
|
+
if (!globalVersionMatch) {
|
|
65
|
+
return _reject(globalVersion);
|
|
66
|
+
}
|
|
67
|
+
var globalVersionParsed = {
|
|
68
|
+
major: +globalVersionMatch[1],
|
|
69
|
+
minor: +globalVersionMatch[2],
|
|
70
|
+
patch: +globalVersionMatch[3],
|
|
71
|
+
prerelease: globalVersionMatch[4]
|
|
72
|
+
};
|
|
73
|
+
if (globalVersionParsed.prerelease != null) {
|
|
74
|
+
return _reject(globalVersion);
|
|
75
|
+
}
|
|
76
|
+
if (ownVersionParsed.major !== globalVersionParsed.major) {
|
|
77
|
+
return _reject(globalVersion);
|
|
78
|
+
}
|
|
79
|
+
if (ownVersionParsed.major === 0) {
|
|
80
|
+
if (ownVersionParsed.minor === globalVersionParsed.minor && ownVersionParsed.patch <= globalVersionParsed.patch) {
|
|
81
|
+
return _accept(globalVersion);
|
|
82
|
+
}
|
|
83
|
+
return _reject(globalVersion);
|
|
84
|
+
}
|
|
85
|
+
if (ownVersionParsed.minor <= globalVersionParsed.minor) {
|
|
86
|
+
return _accept(globalVersion);
|
|
87
|
+
}
|
|
88
|
+
return _reject(globalVersion);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
var isCompatible = _makeCompatibilityCheck(VERSION);
|
|
92
|
+
|
|
93
|
+
// ../../node_modules/@opentelemetry/api/build/esm/internal/global-utils.js
|
|
94
|
+
var major = VERSION.split(".")[0];
|
|
95
|
+
var GLOBAL_OPENTELEMETRY_API_KEY = Symbol.for("opentelemetry.js.api." + major);
|
|
96
|
+
var _global = _globalThis;
|
|
97
|
+
function registerGlobal(type, instance, diag, allowOverride) {
|
|
98
|
+
var _a;
|
|
99
|
+
if (allowOverride === void 0) {
|
|
100
|
+
allowOverride = false;
|
|
101
|
+
}
|
|
102
|
+
var api = _global[GLOBAL_OPENTELEMETRY_API_KEY] = (_a = _global[GLOBAL_OPENTELEMETRY_API_KEY]) !== null && _a !== void 0 ? _a : {
|
|
103
|
+
version: VERSION
|
|
104
|
+
};
|
|
105
|
+
if (!allowOverride && api[type]) {
|
|
106
|
+
var err = new Error("@opentelemetry/api: Attempted duplicate registration of API: " + type);
|
|
107
|
+
diag.error(err.stack || err.message);
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (api.version !== VERSION) {
|
|
111
|
+
var err = new Error("@opentelemetry/api: Registration of version v" + api.version + " for " + type + " does not match previously registered API v" + VERSION);
|
|
112
|
+
diag.error(err.stack || err.message);
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
api[type] = instance;
|
|
116
|
+
diag.debug("@opentelemetry/api: Registered a global for " + type + " v" + VERSION + ".");
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
function getGlobal(type) {
|
|
120
|
+
var _a, _b;
|
|
121
|
+
var globalVersion = (_a = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _a === void 0 ? void 0 : _a.version;
|
|
122
|
+
if (!globalVersion || !isCompatible(globalVersion)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
return (_b = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _b === void 0 ? void 0 : _b[type];
|
|
126
|
+
}
|
|
127
|
+
function unregisterGlobal(type, diag) {
|
|
128
|
+
diag.debug("@opentelemetry/api: Unregistering a global for " + type + " v" + VERSION + ".");
|
|
129
|
+
var api = _global[GLOBAL_OPENTELEMETRY_API_KEY];
|
|
130
|
+
if (api) {
|
|
131
|
+
delete api[type];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// ../../node_modules/@opentelemetry/api/build/esm/diag/ComponentLogger.js
|
|
136
|
+
var __read = function(o, n) {
|
|
137
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
138
|
+
if (!m) return o;
|
|
139
|
+
var i = m.call(o), r, ar = [], e;
|
|
140
|
+
try {
|
|
141
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
e = { error };
|
|
144
|
+
} finally {
|
|
145
|
+
try {
|
|
146
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
147
|
+
} finally {
|
|
148
|
+
if (e) throw e.error;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return ar;
|
|
152
|
+
};
|
|
153
|
+
var __spreadArray = function(to, from, pack) {
|
|
154
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
155
|
+
if (ar || !(i in from)) {
|
|
156
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
157
|
+
ar[i] = from[i];
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
161
|
+
};
|
|
162
|
+
var DiagComponentLogger = (
|
|
163
|
+
/** @class */
|
|
164
|
+
function() {
|
|
165
|
+
function DiagComponentLogger2(props) {
|
|
166
|
+
this._namespace = props.namespace || "DiagComponentLogger";
|
|
167
|
+
}
|
|
168
|
+
DiagComponentLogger2.prototype.debug = function() {
|
|
169
|
+
var args = [];
|
|
170
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
171
|
+
args[_i] = arguments[_i];
|
|
172
|
+
}
|
|
173
|
+
return logProxy("debug", this._namespace, args);
|
|
174
|
+
};
|
|
175
|
+
DiagComponentLogger2.prototype.error = function() {
|
|
176
|
+
var args = [];
|
|
177
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
178
|
+
args[_i] = arguments[_i];
|
|
179
|
+
}
|
|
180
|
+
return logProxy("error", this._namespace, args);
|
|
181
|
+
};
|
|
182
|
+
DiagComponentLogger2.prototype.info = function() {
|
|
183
|
+
var args = [];
|
|
184
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
185
|
+
args[_i] = arguments[_i];
|
|
186
|
+
}
|
|
187
|
+
return logProxy("info", this._namespace, args);
|
|
188
|
+
};
|
|
189
|
+
DiagComponentLogger2.prototype.warn = function() {
|
|
190
|
+
var args = [];
|
|
191
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
192
|
+
args[_i] = arguments[_i];
|
|
193
|
+
}
|
|
194
|
+
return logProxy("warn", this._namespace, args);
|
|
195
|
+
};
|
|
196
|
+
DiagComponentLogger2.prototype.verbose = function() {
|
|
197
|
+
var args = [];
|
|
198
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
199
|
+
args[_i] = arguments[_i];
|
|
200
|
+
}
|
|
201
|
+
return logProxy("verbose", this._namespace, args);
|
|
202
|
+
};
|
|
203
|
+
return DiagComponentLogger2;
|
|
204
|
+
}()
|
|
205
|
+
);
|
|
206
|
+
function logProxy(funcName, namespace, args) {
|
|
207
|
+
var logger2 = getGlobal("diag");
|
|
208
|
+
if (!logger2) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
args.unshift(namespace);
|
|
212
|
+
return logger2[funcName].apply(logger2, __spreadArray([], __read(args), false));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ../../node_modules/@opentelemetry/api/build/esm/diag/types.js
|
|
216
|
+
var DiagLogLevel;
|
|
217
|
+
(function(DiagLogLevel2) {
|
|
218
|
+
DiagLogLevel2[DiagLogLevel2["NONE"] = 0] = "NONE";
|
|
219
|
+
DiagLogLevel2[DiagLogLevel2["ERROR"] = 30] = "ERROR";
|
|
220
|
+
DiagLogLevel2[DiagLogLevel2["WARN"] = 50] = "WARN";
|
|
221
|
+
DiagLogLevel2[DiagLogLevel2["INFO"] = 60] = "INFO";
|
|
222
|
+
DiagLogLevel2[DiagLogLevel2["DEBUG"] = 70] = "DEBUG";
|
|
223
|
+
DiagLogLevel2[DiagLogLevel2["VERBOSE"] = 80] = "VERBOSE";
|
|
224
|
+
DiagLogLevel2[DiagLogLevel2["ALL"] = 9999] = "ALL";
|
|
225
|
+
})(DiagLogLevel || (DiagLogLevel = {}));
|
|
226
|
+
|
|
227
|
+
// ../../node_modules/@opentelemetry/api/build/esm/diag/internal/logLevelLogger.js
|
|
228
|
+
function createLogLevelDiagLogger(maxLevel, logger2) {
|
|
229
|
+
if (maxLevel < DiagLogLevel.NONE) {
|
|
230
|
+
maxLevel = DiagLogLevel.NONE;
|
|
231
|
+
} else if (maxLevel > DiagLogLevel.ALL) {
|
|
232
|
+
maxLevel = DiagLogLevel.ALL;
|
|
233
|
+
}
|
|
234
|
+
logger2 = logger2 || {};
|
|
235
|
+
function _filterFunc(funcName, theLevel) {
|
|
236
|
+
var theFunc = logger2[funcName];
|
|
237
|
+
if (typeof theFunc === "function" && maxLevel >= theLevel) {
|
|
238
|
+
return theFunc.bind(logger2);
|
|
239
|
+
}
|
|
240
|
+
return function() {
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
error: _filterFunc("error", DiagLogLevel.ERROR),
|
|
245
|
+
warn: _filterFunc("warn", DiagLogLevel.WARN),
|
|
246
|
+
info: _filterFunc("info", DiagLogLevel.INFO),
|
|
247
|
+
debug: _filterFunc("debug", DiagLogLevel.DEBUG),
|
|
248
|
+
verbose: _filterFunc("verbose", DiagLogLevel.VERBOSE)
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ../../node_modules/@opentelemetry/api/build/esm/api/diag.js
|
|
253
|
+
var __read2 = function(o, n) {
|
|
254
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
255
|
+
if (!m) return o;
|
|
256
|
+
var i = m.call(o), r, ar = [], e;
|
|
257
|
+
try {
|
|
258
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
259
|
+
} catch (error) {
|
|
260
|
+
e = { error };
|
|
261
|
+
} finally {
|
|
262
|
+
try {
|
|
263
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
264
|
+
} finally {
|
|
265
|
+
if (e) throw e.error;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return ar;
|
|
269
|
+
};
|
|
270
|
+
var __spreadArray2 = function(to, from, pack) {
|
|
271
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
272
|
+
if (ar || !(i in from)) {
|
|
273
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
274
|
+
ar[i] = from[i];
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
278
|
+
};
|
|
279
|
+
var API_NAME = "diag";
|
|
280
|
+
var DiagAPI = (
|
|
281
|
+
/** @class */
|
|
282
|
+
function() {
|
|
283
|
+
function DiagAPI2() {
|
|
284
|
+
function _logProxy(funcName) {
|
|
285
|
+
return function() {
|
|
286
|
+
var args = [];
|
|
287
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
288
|
+
args[_i] = arguments[_i];
|
|
289
|
+
}
|
|
290
|
+
var logger2 = getGlobal("diag");
|
|
291
|
+
if (!logger2)
|
|
292
|
+
return;
|
|
293
|
+
return logger2[funcName].apply(logger2, __spreadArray2([], __read2(args), false));
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
var self = this;
|
|
297
|
+
var setLogger = function(logger2, optionsOrLogLevel) {
|
|
298
|
+
var _a, _b, _c;
|
|
299
|
+
if (optionsOrLogLevel === void 0) {
|
|
300
|
+
optionsOrLogLevel = { logLevel: DiagLogLevel.INFO };
|
|
301
|
+
}
|
|
302
|
+
if (logger2 === self) {
|
|
303
|
+
var err = new Error("Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation");
|
|
304
|
+
self.error((_a = err.stack) !== null && _a !== void 0 ? _a : err.message);
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
if (typeof optionsOrLogLevel === "number") {
|
|
308
|
+
optionsOrLogLevel = {
|
|
309
|
+
logLevel: optionsOrLogLevel
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
var oldLogger = getGlobal("diag");
|
|
313
|
+
var newLogger = createLogLevelDiagLogger((_b = optionsOrLogLevel.logLevel) !== null && _b !== void 0 ? _b : DiagLogLevel.INFO, logger2);
|
|
314
|
+
if (oldLogger && !optionsOrLogLevel.suppressOverrideMessage) {
|
|
315
|
+
var stack = (_c = new Error().stack) !== null && _c !== void 0 ? _c : "<failed to generate stacktrace>";
|
|
316
|
+
oldLogger.warn("Current logger will be overwritten from " + stack);
|
|
317
|
+
newLogger.warn("Current logger will overwrite one already registered from " + stack);
|
|
318
|
+
}
|
|
319
|
+
return registerGlobal("diag", newLogger, self, true);
|
|
320
|
+
};
|
|
321
|
+
self.setLogger = setLogger;
|
|
322
|
+
self.disable = function() {
|
|
323
|
+
unregisterGlobal(API_NAME, self);
|
|
324
|
+
};
|
|
325
|
+
self.createComponentLogger = function(options) {
|
|
326
|
+
return new DiagComponentLogger(options);
|
|
327
|
+
};
|
|
328
|
+
self.verbose = _logProxy("verbose");
|
|
329
|
+
self.debug = _logProxy("debug");
|
|
330
|
+
self.info = _logProxy("info");
|
|
331
|
+
self.warn = _logProxy("warn");
|
|
332
|
+
self.error = _logProxy("error");
|
|
333
|
+
}
|
|
334
|
+
DiagAPI2.instance = function() {
|
|
335
|
+
if (!this._instance) {
|
|
336
|
+
this._instance = new DiagAPI2();
|
|
337
|
+
}
|
|
338
|
+
return this._instance;
|
|
339
|
+
};
|
|
340
|
+
return DiagAPI2;
|
|
341
|
+
}()
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
// ../../node_modules/@opentelemetry/api/build/esm/context/context.js
|
|
345
|
+
var BaseContext = (
|
|
346
|
+
/** @class */
|
|
347
|
+
/* @__PURE__ */ function() {
|
|
348
|
+
function BaseContext2(parentContext) {
|
|
349
|
+
var self = this;
|
|
350
|
+
self._currentContext = parentContext ? new Map(parentContext) : /* @__PURE__ */ new Map();
|
|
351
|
+
self.getValue = function(key) {
|
|
352
|
+
return self._currentContext.get(key);
|
|
353
|
+
};
|
|
354
|
+
self.setValue = function(key, value) {
|
|
355
|
+
var context2 = new BaseContext2(self._currentContext);
|
|
356
|
+
context2._currentContext.set(key, value);
|
|
357
|
+
return context2;
|
|
358
|
+
};
|
|
359
|
+
self.deleteValue = function(key) {
|
|
360
|
+
var context2 = new BaseContext2(self._currentContext);
|
|
361
|
+
context2._currentContext.delete(key);
|
|
362
|
+
return context2;
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
return BaseContext2;
|
|
366
|
+
}()
|
|
367
|
+
);
|
|
368
|
+
var ROOT_CONTEXT = new BaseContext();
|
|
369
|
+
|
|
370
|
+
// ../../node_modules/@opentelemetry/api/build/esm/context/NoopContextManager.js
|
|
371
|
+
var __read3 = function(o, n) {
|
|
372
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
373
|
+
if (!m) return o;
|
|
374
|
+
var i = m.call(o), r, ar = [], e;
|
|
375
|
+
try {
|
|
376
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
377
|
+
} catch (error) {
|
|
378
|
+
e = { error };
|
|
379
|
+
} finally {
|
|
380
|
+
try {
|
|
381
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
382
|
+
} finally {
|
|
383
|
+
if (e) throw e.error;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return ar;
|
|
387
|
+
};
|
|
388
|
+
var __spreadArray3 = function(to, from, pack) {
|
|
389
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
390
|
+
if (ar || !(i in from)) {
|
|
391
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
392
|
+
ar[i] = from[i];
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
396
|
+
};
|
|
397
|
+
var NoopContextManager = (
|
|
398
|
+
/** @class */
|
|
399
|
+
function() {
|
|
400
|
+
function NoopContextManager2() {
|
|
401
|
+
}
|
|
402
|
+
NoopContextManager2.prototype.active = function() {
|
|
403
|
+
return ROOT_CONTEXT;
|
|
404
|
+
};
|
|
405
|
+
NoopContextManager2.prototype.with = function(_context, fn, thisArg) {
|
|
406
|
+
var args = [];
|
|
407
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
408
|
+
args[_i - 3] = arguments[_i];
|
|
409
|
+
}
|
|
410
|
+
return fn.call.apply(fn, __spreadArray3([thisArg], __read3(args), false));
|
|
411
|
+
};
|
|
412
|
+
NoopContextManager2.prototype.bind = function(_context, target) {
|
|
413
|
+
return target;
|
|
414
|
+
};
|
|
415
|
+
NoopContextManager2.prototype.enable = function() {
|
|
416
|
+
return this;
|
|
417
|
+
};
|
|
418
|
+
NoopContextManager2.prototype.disable = function() {
|
|
419
|
+
return this;
|
|
420
|
+
};
|
|
421
|
+
return NoopContextManager2;
|
|
422
|
+
}()
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
// ../../node_modules/@opentelemetry/api/build/esm/api/context.js
|
|
426
|
+
var __read4 = function(o, n) {
|
|
427
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
428
|
+
if (!m) return o;
|
|
429
|
+
var i = m.call(o), r, ar = [], e;
|
|
430
|
+
try {
|
|
431
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
432
|
+
} catch (error) {
|
|
433
|
+
e = { error };
|
|
434
|
+
} finally {
|
|
435
|
+
try {
|
|
436
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
437
|
+
} finally {
|
|
438
|
+
if (e) throw e.error;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return ar;
|
|
442
|
+
};
|
|
443
|
+
var __spreadArray4 = function(to, from, pack) {
|
|
444
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
445
|
+
if (ar || !(i in from)) {
|
|
446
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
447
|
+
ar[i] = from[i];
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
451
|
+
};
|
|
452
|
+
var API_NAME2 = "context";
|
|
453
|
+
var NOOP_CONTEXT_MANAGER = new NoopContextManager();
|
|
454
|
+
var ContextAPI = (
|
|
455
|
+
/** @class */
|
|
456
|
+
function() {
|
|
457
|
+
function ContextAPI2() {
|
|
458
|
+
}
|
|
459
|
+
ContextAPI2.getInstance = function() {
|
|
460
|
+
if (!this._instance) {
|
|
461
|
+
this._instance = new ContextAPI2();
|
|
462
|
+
}
|
|
463
|
+
return this._instance;
|
|
464
|
+
};
|
|
465
|
+
ContextAPI2.prototype.setGlobalContextManager = function(contextManager) {
|
|
466
|
+
return registerGlobal(API_NAME2, contextManager, DiagAPI.instance());
|
|
467
|
+
};
|
|
468
|
+
ContextAPI2.prototype.active = function() {
|
|
469
|
+
return this._getContextManager().active();
|
|
470
|
+
};
|
|
471
|
+
ContextAPI2.prototype.with = function(context2, fn, thisArg) {
|
|
472
|
+
var _a;
|
|
473
|
+
var args = [];
|
|
474
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
475
|
+
args[_i - 3] = arguments[_i];
|
|
476
|
+
}
|
|
477
|
+
return (_a = this._getContextManager()).with.apply(_a, __spreadArray4([context2, fn, thisArg], __read4(args), false));
|
|
478
|
+
};
|
|
479
|
+
ContextAPI2.prototype.bind = function(context2, target) {
|
|
480
|
+
return this._getContextManager().bind(context2, target);
|
|
481
|
+
};
|
|
482
|
+
ContextAPI2.prototype._getContextManager = function() {
|
|
483
|
+
return getGlobal(API_NAME2) || NOOP_CONTEXT_MANAGER;
|
|
484
|
+
};
|
|
485
|
+
ContextAPI2.prototype.disable = function() {
|
|
486
|
+
this._getContextManager().disable();
|
|
487
|
+
unregisterGlobal(API_NAME2, DiagAPI.instance());
|
|
488
|
+
};
|
|
489
|
+
return ContextAPI2;
|
|
490
|
+
}()
|
|
491
|
+
);
|
|
492
|
+
|
|
493
|
+
// ../../node_modules/@opentelemetry/api/build/esm/trace/status.js
|
|
494
|
+
var SpanStatusCode;
|
|
495
|
+
(function(SpanStatusCode2) {
|
|
496
|
+
SpanStatusCode2[SpanStatusCode2["UNSET"] = 0] = "UNSET";
|
|
497
|
+
SpanStatusCode2[SpanStatusCode2["OK"] = 1] = "OK";
|
|
498
|
+
SpanStatusCode2[SpanStatusCode2["ERROR"] = 2] = "ERROR";
|
|
499
|
+
})(SpanStatusCode || (SpanStatusCode = {}));
|
|
500
|
+
|
|
501
|
+
// ../../node_modules/@opentelemetry/api/build/esm/context-api.js
|
|
502
|
+
var context = ContextAPI.getInstance();
|
|
503
|
+
|
|
504
|
+
// src/index.ts
|
|
505
|
+
function getTracer(runtime) {
|
|
506
|
+
const availableServices = Array.from(runtime.getAllServices().keys());
|
|
507
|
+
logger.debug(`[getTracer] Available services: ${JSON.stringify(availableServices)}`);
|
|
508
|
+
logger.debug(`[getTracer] Attempting to get service with key: ${ServiceType.INSTRUMENTATION}`);
|
|
509
|
+
const instrumentationService = runtime.getService(
|
|
510
|
+
ServiceType.INSTRUMENTATION
|
|
511
|
+
);
|
|
512
|
+
if (!instrumentationService) {
|
|
513
|
+
logger.warn(`[getTracer] Service ${ServiceType.INSTRUMENTATION} not found in runtime.`);
|
|
514
|
+
return null;
|
|
515
|
+
}
|
|
516
|
+
if (!instrumentationService.isEnabled()) {
|
|
517
|
+
logger.debug("[getTracer] Instrumentation service found but is disabled.");
|
|
518
|
+
return null;
|
|
519
|
+
}
|
|
520
|
+
logger.debug("[getTracer] Successfully retrieved enabled instrumentation service.");
|
|
521
|
+
return instrumentationService.getTracer("eliza.llm.openai");
|
|
522
|
+
}
|
|
523
|
+
async function startLlmSpan(runtime, spanName, attributes, fn) {
|
|
524
|
+
const tracer = getTracer(runtime);
|
|
525
|
+
if (!tracer) {
|
|
526
|
+
const dummySpan = {
|
|
527
|
+
setAttribute: () => {
|
|
528
|
+
},
|
|
529
|
+
setAttributes: () => {
|
|
530
|
+
},
|
|
531
|
+
addEvent: () => {
|
|
532
|
+
},
|
|
533
|
+
recordException: () => {
|
|
534
|
+
},
|
|
535
|
+
setStatus: () => {
|
|
536
|
+
},
|
|
537
|
+
end: () => {
|
|
538
|
+
},
|
|
539
|
+
spanContext: () => ({ traceId: "", spanId: "", traceFlags: 0 })
|
|
540
|
+
};
|
|
541
|
+
return fn(dummySpan);
|
|
542
|
+
}
|
|
543
|
+
const activeContext = context.active();
|
|
544
|
+
return tracer.startActiveSpan(spanName, { attributes }, activeContext, async (span) => {
|
|
545
|
+
try {
|
|
546
|
+
const result = await fn(span);
|
|
547
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
548
|
+
span.end();
|
|
549
|
+
return result;
|
|
550
|
+
} catch (error) {
|
|
551
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
552
|
+
span.recordException(error);
|
|
553
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
554
|
+
span.end();
|
|
555
|
+
throw error;
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
function getSetting(runtime, key, defaultValue) {
|
|
560
|
+
return runtime.getSetting(key) ?? process.env[key] ?? defaultValue;
|
|
561
|
+
}
|
|
562
|
+
function getBaseURL(runtime) {
|
|
563
|
+
const defaultBaseURL = getSetting(runtime, "OPENAI_BASE_URL", "https://api.openai.com/v1");
|
|
564
|
+
logger.debug(`[OpenAI] Default base URL: ${defaultBaseURL}`);
|
|
565
|
+
return getProviderBaseURL(runtime, "openai", defaultBaseURL);
|
|
566
|
+
}
|
|
567
|
+
function getApiKey(runtime) {
|
|
568
|
+
return getSetting(runtime, "OPENAI_API_KEY");
|
|
569
|
+
}
|
|
570
|
+
function getSmallModel(runtime) {
|
|
571
|
+
return getSetting(runtime, "OPENAI_SMALL_MODEL") ?? getSetting(runtime, "SMALL_MODEL", "gpt-4o-mini");
|
|
572
|
+
}
|
|
573
|
+
function getLargeModel(runtime) {
|
|
574
|
+
return getSetting(runtime, "OPENAI_LARGE_MODEL") ?? getSetting(runtime, "LARGE_MODEL", "gpt-4o");
|
|
575
|
+
}
|
|
576
|
+
function createOpenAIClient(runtime) {
|
|
577
|
+
return createOpenAI({
|
|
578
|
+
apiKey: getApiKey(runtime),
|
|
579
|
+
baseURL: getBaseURL(runtime)
|
|
580
|
+
});
|
|
581
|
+
}
|
|
9
582
|
async function tokenizeText(model, prompt) {
|
|
10
583
|
const modelName = model === ModelType.TEXT_SMALL ? process.env.OPENAI_SMALL_MODEL ?? process.env.SMALL_MODEL ?? "gpt-4o-mini" : process.env.LARGE_MODEL ?? "gpt-4o";
|
|
11
584
|
const encoding = encodingForModel(modelName);
|
|
@@ -17,6 +590,171 @@ async function detokenizeText(model, tokens) {
|
|
|
17
590
|
const encoding = encodingForModel(modelName);
|
|
18
591
|
return encoding.decode(tokens);
|
|
19
592
|
}
|
|
593
|
+
async function generateObjectByModelType(runtime, params, modelType, getModelFn) {
|
|
594
|
+
const openai = createOpenAIClient(runtime);
|
|
595
|
+
const modelName = getModelFn(runtime);
|
|
596
|
+
logger.log(`[OpenAI] Using ${modelType} model: ${modelName}`);
|
|
597
|
+
const temperature = params.temperature ?? 0;
|
|
598
|
+
const schemaPresent = !!params.schema;
|
|
599
|
+
const attributes = {
|
|
600
|
+
"llm.vendor": "OpenAI",
|
|
601
|
+
"llm.request.type": "object_generation",
|
|
602
|
+
"llm.request.model": modelName,
|
|
603
|
+
"llm.request.temperature": temperature,
|
|
604
|
+
"llm.request.schema_present": schemaPresent
|
|
605
|
+
};
|
|
606
|
+
return startLlmSpan(runtime, "LLM.generateObject", attributes, async (span) => {
|
|
607
|
+
span.addEvent("llm.prompt", { "prompt.content": params.prompt });
|
|
608
|
+
if (schemaPresent) {
|
|
609
|
+
span.addEvent("llm.request.schema", {
|
|
610
|
+
schema: JSON.stringify(params.schema, safeReplacer())
|
|
611
|
+
});
|
|
612
|
+
logger.info(
|
|
613
|
+
`Using ${modelType} without schema validation (schema provided but output=no-schema)`
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
try {
|
|
617
|
+
const { object, usage } = await generateObject({
|
|
618
|
+
model: openai.languageModel(modelName),
|
|
619
|
+
output: "no-schema",
|
|
620
|
+
prompt: params.prompt,
|
|
621
|
+
temperature,
|
|
622
|
+
experimental_repairText: getJsonRepairFunction()
|
|
623
|
+
});
|
|
624
|
+
span.addEvent("llm.response.processed", {
|
|
625
|
+
"response.object": JSON.stringify(object, safeReplacer())
|
|
626
|
+
});
|
|
627
|
+
if (usage) {
|
|
628
|
+
span.setAttributes({
|
|
629
|
+
"llm.usage.prompt_tokens": usage.promptTokens,
|
|
630
|
+
"llm.usage.completion_tokens": usage.completionTokens,
|
|
631
|
+
"llm.usage.total_tokens": usage.totalTokens
|
|
632
|
+
});
|
|
633
|
+
emitModelUsageEvent(runtime, modelType, params.prompt, usage);
|
|
634
|
+
}
|
|
635
|
+
return object;
|
|
636
|
+
} catch (error) {
|
|
637
|
+
if (error instanceof JSONParseError) {
|
|
638
|
+
logger.error(`[generateObject] Failed to parse JSON: ${error.message}`);
|
|
639
|
+
span.recordException(error);
|
|
640
|
+
span.addEvent("llm.error.json_parse", {
|
|
641
|
+
"error.message": error.message,
|
|
642
|
+
"error.text": error.text
|
|
643
|
+
});
|
|
644
|
+
span.addEvent("llm.repair.attempt");
|
|
645
|
+
const repairFunction = getJsonRepairFunction();
|
|
646
|
+
const repairedJsonString = await repairFunction({
|
|
647
|
+
text: error.text,
|
|
648
|
+
error
|
|
649
|
+
});
|
|
650
|
+
if (repairedJsonString) {
|
|
651
|
+
try {
|
|
652
|
+
const repairedObject = JSON.parse(repairedJsonString);
|
|
653
|
+
span.addEvent("llm.repair.success", {
|
|
654
|
+
repaired_object: JSON.stringify(repairedObject, safeReplacer())
|
|
655
|
+
});
|
|
656
|
+
logger.info("[generateObject] Successfully repaired JSON.");
|
|
657
|
+
span.setStatus({
|
|
658
|
+
code: SpanStatusCode.ERROR,
|
|
659
|
+
message: "JSON parsing failed but was repaired"
|
|
660
|
+
});
|
|
661
|
+
return repairedObject;
|
|
662
|
+
} catch (repairParseError) {
|
|
663
|
+
const message = repairParseError instanceof Error ? repairParseError.message : String(repairParseError);
|
|
664
|
+
logger.error(`[generateObject] Failed to parse repaired JSON: ${message}`);
|
|
665
|
+
const exception = repairParseError instanceof Error ? repairParseError : new Error(message);
|
|
666
|
+
span.recordException(exception);
|
|
667
|
+
span.addEvent("llm.repair.parse_error", {
|
|
668
|
+
"error.message": message
|
|
669
|
+
});
|
|
670
|
+
span.setStatus({
|
|
671
|
+
code: SpanStatusCode.ERROR,
|
|
672
|
+
message: `JSON repair failed: ${message}`
|
|
673
|
+
});
|
|
674
|
+
throw repairParseError;
|
|
675
|
+
}
|
|
676
|
+
} else {
|
|
677
|
+
const errMsg = error instanceof Error ? error.message : String(error);
|
|
678
|
+
logger.error("[generateObject] JSON repair failed.");
|
|
679
|
+
span.addEvent("llm.repair.failed");
|
|
680
|
+
span.setStatus({
|
|
681
|
+
code: SpanStatusCode.ERROR,
|
|
682
|
+
message: `JSON repair failed: ${errMsg}`
|
|
683
|
+
});
|
|
684
|
+
throw error;
|
|
685
|
+
}
|
|
686
|
+
} else {
|
|
687
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
688
|
+
logger.error(`[generateObject] Unknown error: ${message}`);
|
|
689
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
690
|
+
span.recordException(exception);
|
|
691
|
+
span.setStatus({
|
|
692
|
+
code: SpanStatusCode.ERROR,
|
|
693
|
+
message
|
|
694
|
+
});
|
|
695
|
+
throw error;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
function getJsonRepairFunction() {
|
|
701
|
+
return async ({ text, error }) => {
|
|
702
|
+
try {
|
|
703
|
+
if (error instanceof JSONParseError) {
|
|
704
|
+
const cleanedText = text.replace(/```json\n|\n```|```/g, "");
|
|
705
|
+
JSON.parse(cleanedText);
|
|
706
|
+
return cleanedText;
|
|
707
|
+
}
|
|
708
|
+
return null;
|
|
709
|
+
} catch (jsonError) {
|
|
710
|
+
const message = jsonError instanceof Error ? jsonError.message : String(jsonError);
|
|
711
|
+
logger.warn(`Failed to repair JSON text: ${message}`);
|
|
712
|
+
return null;
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
function emitModelUsageEvent(runtime, type, prompt, usage) {
|
|
717
|
+
runtime.emitEvent(EventType.MODEL_USED, {
|
|
718
|
+
provider: "openai",
|
|
719
|
+
type,
|
|
720
|
+
prompt,
|
|
721
|
+
tokens: {
|
|
722
|
+
prompt: usage.promptTokens,
|
|
723
|
+
completion: usage.completionTokens,
|
|
724
|
+
total: usage.totalTokens
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
async function fetchTextToSpeech(runtime, text) {
|
|
729
|
+
const apiKey = getApiKey(runtime);
|
|
730
|
+
const model = getSetting(runtime, "OPENAI_TTS_MODEL", "gpt-4o-mini-tts");
|
|
731
|
+
const voice = getSetting(runtime, "OPENAI_TTS_VOICE", "nova");
|
|
732
|
+
const instructions = getSetting(runtime, "OPENAI_TTS_INSTRUCTIONS", "");
|
|
733
|
+
const baseURL = getBaseURL(runtime);
|
|
734
|
+
try {
|
|
735
|
+
const res = await fetch(`${baseURL}/audio/speech`, {
|
|
736
|
+
method: "POST",
|
|
737
|
+
headers: {
|
|
738
|
+
Authorization: `Bearer ${apiKey}`,
|
|
739
|
+
"Content-Type": "application/json"
|
|
740
|
+
},
|
|
741
|
+
body: JSON.stringify({
|
|
742
|
+
model,
|
|
743
|
+
voice,
|
|
744
|
+
input: text,
|
|
745
|
+
...instructions && { instructions }
|
|
746
|
+
})
|
|
747
|
+
});
|
|
748
|
+
if (!res.ok) {
|
|
749
|
+
const err = await res.text();
|
|
750
|
+
throw new Error(`OpenAI TTS error ${res.status}: ${err}`);
|
|
751
|
+
}
|
|
752
|
+
return res.body;
|
|
753
|
+
} catch (err) {
|
|
754
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
755
|
+
throw new Error(`Failed to fetch speech from OpenAI TTS: ${message}`);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
20
758
|
var openaiPlugin = {
|
|
21
759
|
name: "openai",
|
|
22
760
|
description: "OpenAI plugin",
|
|
@@ -26,41 +764,63 @@ var openaiPlugin = {
|
|
|
26
764
|
OPENAI_SMALL_MODEL: process.env.OPENAI_SMALL_MODEL,
|
|
27
765
|
OPENAI_LARGE_MODEL: process.env.OPENAI_LARGE_MODEL,
|
|
28
766
|
SMALL_MODEL: process.env.SMALL_MODEL,
|
|
29
|
-
LARGE_MODEL: process.env.LARGE_MODEL
|
|
767
|
+
LARGE_MODEL: process.env.LARGE_MODEL,
|
|
768
|
+
OPENAI_EMBEDDING_MODEL: process.env.OPENAI_EMBEDDING_MODEL,
|
|
769
|
+
OPENAI_EMBEDDING_DIMENSIONS: process.env.OPENAI_EMBEDDING_DIMENSIONS
|
|
30
770
|
},
|
|
31
|
-
async init(
|
|
771
|
+
async init(_config, runtime) {
|
|
32
772
|
try {
|
|
33
|
-
if (!
|
|
773
|
+
if (!getApiKey(runtime)) {
|
|
34
774
|
logger.warn(
|
|
35
775
|
"OPENAI_API_KEY is not set in environment - OpenAI functionality will be limited"
|
|
36
776
|
);
|
|
37
777
|
return;
|
|
38
778
|
}
|
|
39
779
|
try {
|
|
40
|
-
const baseURL =
|
|
780
|
+
const baseURL = getBaseURL(runtime);
|
|
41
781
|
const response = await fetch(`${baseURL}/models`, {
|
|
42
|
-
headers: { Authorization: `Bearer ${
|
|
782
|
+
headers: { Authorization: `Bearer ${getApiKey(runtime)}` }
|
|
43
783
|
});
|
|
44
784
|
if (!response.ok) {
|
|
45
785
|
logger.warn(`OpenAI API key validation failed: ${response.statusText}`);
|
|
46
786
|
logger.warn("OpenAI functionality will be limited until a valid API key is provided");
|
|
47
787
|
} else {
|
|
788
|
+
logger.log("OpenAI API key validated successfully");
|
|
48
789
|
}
|
|
49
790
|
} catch (fetchError) {
|
|
50
|
-
|
|
791
|
+
const message = fetchError instanceof Error ? fetchError.message : String(fetchError);
|
|
792
|
+
logger.warn(`Error validating OpenAI API key: ${message}`);
|
|
51
793
|
logger.warn("OpenAI functionality will be limited until a valid API key is provided");
|
|
52
794
|
}
|
|
53
795
|
} catch (error) {
|
|
796
|
+
const message = error?.errors?.map((e) => e.message).join(", ") || (error instanceof Error ? error.message : String(error));
|
|
54
797
|
logger.warn(
|
|
55
|
-
`OpenAI plugin configuration issue: ${
|
|
798
|
+
`OpenAI plugin configuration issue: ${message} - You need to configure the OPENAI_API_KEY in your environment variables`
|
|
56
799
|
);
|
|
57
800
|
}
|
|
58
801
|
},
|
|
59
802
|
models: {
|
|
60
|
-
[ModelType.TEXT_EMBEDDING]: async (
|
|
803
|
+
[ModelType.TEXT_EMBEDDING]: async (runtime, params) => {
|
|
804
|
+
const embeddingModelName = getSetting(
|
|
805
|
+
runtime,
|
|
806
|
+
"OPENAI_EMBEDDING_MODEL",
|
|
807
|
+
"text-embedding-3-small"
|
|
808
|
+
);
|
|
809
|
+
const embeddingDimension = Number.parseInt(
|
|
810
|
+
getSetting(runtime, "OPENAI_EMBEDDING_DIMENSIONS", "1536") || "1536",
|
|
811
|
+
10
|
|
812
|
+
);
|
|
813
|
+
logger.debug(
|
|
814
|
+
`[OpenAI] Using embedding model: ${embeddingModelName} with dimension: ${embeddingDimension}`
|
|
815
|
+
);
|
|
816
|
+
if (!Object.values(VECTOR_DIMS).includes(embeddingDimension)) {
|
|
817
|
+
const errorMsg = `Invalid embedding dimension: ${embeddingDimension}. Must be one of: ${Object.values(VECTOR_DIMS).join(", ")}`;
|
|
818
|
+
logger.error(errorMsg);
|
|
819
|
+
throw new Error(errorMsg);
|
|
820
|
+
}
|
|
61
821
|
if (params === null) {
|
|
62
822
|
logger.debug("Creating test embedding for initialization");
|
|
63
|
-
const testVector = Array(
|
|
823
|
+
const testVector = Array(embeddingDimension).fill(0);
|
|
64
824
|
testVector[0] = 0.1;
|
|
65
825
|
return testVector;
|
|
66
826
|
}
|
|
@@ -71,51 +831,94 @@ var openaiPlugin = {
|
|
|
71
831
|
text = params.text;
|
|
72
832
|
} else {
|
|
73
833
|
logger.warn("Invalid input format for embedding");
|
|
74
|
-
const fallbackVector = Array(
|
|
834
|
+
const fallbackVector = Array(embeddingDimension).fill(0);
|
|
75
835
|
fallbackVector[0] = 0.2;
|
|
76
836
|
return fallbackVector;
|
|
77
837
|
}
|
|
78
838
|
if (!text.trim()) {
|
|
79
839
|
logger.warn("Empty text for embedding");
|
|
80
|
-
const emptyVector = Array(
|
|
840
|
+
const emptyVector = Array(embeddingDimension).fill(0);
|
|
81
841
|
emptyVector[0] = 0.3;
|
|
82
842
|
return emptyVector;
|
|
83
843
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return errorVector;
|
|
844
|
+
const attributes = {
|
|
845
|
+
"llm.vendor": "OpenAI",
|
|
846
|
+
"llm.request.type": "embedding",
|
|
847
|
+
"llm.request.model": embeddingModelName,
|
|
848
|
+
"llm.request.embedding.dimensions": embeddingDimension,
|
|
849
|
+
"input.text.length": text.length
|
|
850
|
+
};
|
|
851
|
+
return startLlmSpan(runtime, "LLM.embedding", attributes, async (span) => {
|
|
852
|
+
span.addEvent("llm.prompt", { "prompt.content": text });
|
|
853
|
+
const baseURL = getBaseURL(runtime);
|
|
854
|
+
const apiKey = getApiKey(runtime);
|
|
855
|
+
if (!apiKey) {
|
|
856
|
+
span.setStatus({
|
|
857
|
+
code: SpanStatusCode.ERROR,
|
|
858
|
+
message: "OpenAI API key not configured"
|
|
859
|
+
});
|
|
860
|
+
throw new Error("OpenAI API key not configured");
|
|
102
861
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
862
|
+
try {
|
|
863
|
+
const response = await fetch(`${baseURL}/embeddings`, {
|
|
864
|
+
method: "POST",
|
|
865
|
+
headers: {
|
|
866
|
+
Authorization: `Bearer ${apiKey}`,
|
|
867
|
+
"Content-Type": "application/json"
|
|
868
|
+
},
|
|
869
|
+
body: JSON.stringify({
|
|
870
|
+
model: embeddingModelName,
|
|
871
|
+
input: text
|
|
872
|
+
})
|
|
873
|
+
});
|
|
874
|
+
const responseClone = response.clone();
|
|
875
|
+
const rawResponseBody = await responseClone.text();
|
|
876
|
+
span.addEvent("llm.response.raw", {
|
|
877
|
+
"response.body": rawResponseBody
|
|
878
|
+
});
|
|
879
|
+
if (!response.ok) {
|
|
880
|
+
logger.error(`OpenAI API error: ${response.status} - ${response.statusText}`);
|
|
881
|
+
span.setAttributes({ "error.api.status": response.status });
|
|
882
|
+
span.setStatus({
|
|
883
|
+
code: SpanStatusCode.ERROR,
|
|
884
|
+
message: `OpenAI API error: ${response.status} - ${response.statusText}. Response: ${rawResponseBody}`
|
|
885
|
+
});
|
|
886
|
+
const errorVector = Array(embeddingDimension).fill(0);
|
|
887
|
+
errorVector[0] = 0.4;
|
|
888
|
+
return errorVector;
|
|
889
|
+
}
|
|
890
|
+
const data = await response.json();
|
|
891
|
+
if (!data?.data?.[0]?.embedding) {
|
|
892
|
+
logger.error("API returned invalid structure");
|
|
893
|
+
span.setStatus({
|
|
894
|
+
code: SpanStatusCode.ERROR,
|
|
895
|
+
message: "API returned invalid structure"
|
|
896
|
+
});
|
|
897
|
+
const errorVector = Array(embeddingDimension).fill(0);
|
|
898
|
+
errorVector[0] = 0.5;
|
|
899
|
+
return errorVector;
|
|
900
|
+
}
|
|
901
|
+
const embedding = data.data[0].embedding;
|
|
902
|
+
span.setAttribute("llm.response.embedding.vector_length", embedding.length);
|
|
903
|
+
if (data.usage) {
|
|
904
|
+
span.setAttributes({
|
|
905
|
+
"llm.usage.prompt_tokens": data.usage.prompt_tokens,
|
|
906
|
+
"llm.usage.total_tokens": data.usage.total_tokens
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
logger.log(`Got valid embedding with length ${embedding.length}`);
|
|
910
|
+
return embedding;
|
|
911
|
+
} catch (error) {
|
|
912
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
913
|
+
logger.error(`Error generating embedding: ${message}`);
|
|
914
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
915
|
+
span.recordException(exception);
|
|
916
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
917
|
+
const errorVector = Array(embeddingDimension).fill(0);
|
|
918
|
+
errorVector[0] = 0.6;
|
|
108
919
|
return errorVector;
|
|
109
920
|
}
|
|
110
|
-
|
|
111
|
-
logger.log(`Got valid embedding with length ${embedding.length}`);
|
|
112
|
-
return embedding;
|
|
113
|
-
} catch (error) {
|
|
114
|
-
logger.error("Error generating embedding:", error);
|
|
115
|
-
const errorVector = Array(1536).fill(0);
|
|
116
|
-
errorVector[0] = 0.6;
|
|
117
|
-
return errorVector;
|
|
118
|
-
}
|
|
921
|
+
});
|
|
119
922
|
},
|
|
120
923
|
[ModelType.TEXT_TOKENIZER_ENCODE]: async (_runtime, { prompt, modelType = ModelType.TEXT_LARGE }) => {
|
|
121
924
|
return await tokenizeText(modelType ?? ModelType.TEXT_LARGE, prompt);
|
|
@@ -128,25 +931,46 @@ var openaiPlugin = {
|
|
|
128
931
|
const frequency_penalty = 0.7;
|
|
129
932
|
const presence_penalty = 0.7;
|
|
130
933
|
const max_response_length = 8192;
|
|
131
|
-
const
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
baseURL
|
|
135
|
-
});
|
|
136
|
-
const model = runtime.getSetting("OPENAI_SMALL_MODEL") ?? runtime.getSetting("SMALL_MODEL") ?? "gpt-4o-mini";
|
|
137
|
-
logger.log("generating text");
|
|
934
|
+
const openai = createOpenAIClient(runtime);
|
|
935
|
+
const modelName = getSmallModel(runtime);
|
|
936
|
+
logger.log(`[OpenAI] Using TEXT_SMALL model: ${modelName}`);
|
|
138
937
|
logger.log(prompt);
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
temperature,
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
stopSequences
|
|
938
|
+
const attributes = {
|
|
939
|
+
"llm.vendor": "OpenAI",
|
|
940
|
+
"llm.request.type": "completion",
|
|
941
|
+
"llm.request.model": modelName,
|
|
942
|
+
"llm.request.temperature": temperature,
|
|
943
|
+
"llm.request.max_tokens": max_response_length,
|
|
944
|
+
"llm.request.frequency_penalty": frequency_penalty,
|
|
945
|
+
"llm.request.presence_penalty": presence_penalty,
|
|
946
|
+
"llm.request.stop_sequences": JSON.stringify(stopSequences)
|
|
947
|
+
};
|
|
948
|
+
return startLlmSpan(runtime, "LLM.generateText", attributes, async (span) => {
|
|
949
|
+
span.addEvent("llm.prompt", { "prompt.content": prompt });
|
|
950
|
+
const { text: openaiResponse, usage } = await generateText({
|
|
951
|
+
model: openai.languageModel(modelName),
|
|
952
|
+
prompt,
|
|
953
|
+
system: runtime.character.system ?? void 0,
|
|
954
|
+
temperature,
|
|
955
|
+
maxTokens: max_response_length,
|
|
956
|
+
frequencyPenalty: frequency_penalty,
|
|
957
|
+
presencePenalty: presence_penalty,
|
|
958
|
+
stopSequences
|
|
959
|
+
});
|
|
960
|
+
span.setAttribute("llm.response.processed.length", openaiResponse.length);
|
|
961
|
+
span.addEvent("llm.response.processed", {
|
|
962
|
+
"response.content": openaiResponse.substring(0, 200) + (openaiResponse.length > 200 ? "..." : "")
|
|
963
|
+
});
|
|
964
|
+
if (usage) {
|
|
965
|
+
span.setAttributes({
|
|
966
|
+
"llm.usage.prompt_tokens": usage.promptTokens,
|
|
967
|
+
"llm.usage.completion_tokens": usage.completionTokens,
|
|
968
|
+
"llm.usage.total_tokens": usage.totalTokens
|
|
969
|
+
});
|
|
970
|
+
emitModelUsageEvent(runtime, ModelType.TEXT_SMALL, prompt, usage);
|
|
971
|
+
}
|
|
972
|
+
return openaiResponse;
|
|
148
973
|
});
|
|
149
|
-
return openaiResponse;
|
|
150
974
|
},
|
|
151
975
|
[ModelType.TEXT_LARGE]: async (runtime, {
|
|
152
976
|
prompt,
|
|
@@ -156,194 +980,329 @@ var openaiPlugin = {
|
|
|
156
980
|
frequencyPenalty = 0.7,
|
|
157
981
|
presencePenalty = 0.7
|
|
158
982
|
}) => {
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
model:
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
983
|
+
const openai = createOpenAIClient(runtime);
|
|
984
|
+
const modelName = getLargeModel(runtime);
|
|
985
|
+
logger.log(`[OpenAI] Using TEXT_LARGE model: ${modelName}`);
|
|
986
|
+
logger.log(prompt);
|
|
987
|
+
const attributes = {
|
|
988
|
+
"llm.vendor": "OpenAI",
|
|
989
|
+
"llm.request.type": "completion",
|
|
990
|
+
"llm.request.model": modelName,
|
|
991
|
+
"llm.request.temperature": temperature,
|
|
992
|
+
"llm.request.max_tokens": maxTokens,
|
|
993
|
+
"llm.request.frequency_penalty": frequencyPenalty,
|
|
994
|
+
"llm.request.presence_penalty": presencePenalty,
|
|
995
|
+
"llm.request.stop_sequences": JSON.stringify(stopSequences)
|
|
996
|
+
};
|
|
997
|
+
return startLlmSpan(runtime, "LLM.generateText", attributes, async (span) => {
|
|
998
|
+
span.addEvent("llm.prompt", { "prompt.content": prompt });
|
|
999
|
+
const { text: openaiResponse, usage } = await generateText({
|
|
1000
|
+
model: openai.languageModel(modelName),
|
|
1001
|
+
prompt,
|
|
1002
|
+
system: runtime.character.system ?? void 0,
|
|
1003
|
+
temperature,
|
|
1004
|
+
maxTokens,
|
|
1005
|
+
frequencyPenalty,
|
|
1006
|
+
presencePenalty,
|
|
1007
|
+
stopSequences
|
|
1008
|
+
});
|
|
1009
|
+
span.setAttribute("llm.response.processed.length", openaiResponse.length);
|
|
1010
|
+
span.addEvent("llm.response.processed", {
|
|
1011
|
+
"response.content": openaiResponse.substring(0, 200) + (openaiResponse.length > 200 ? "..." : "")
|
|
1012
|
+
});
|
|
1013
|
+
if (usage) {
|
|
1014
|
+
span.setAttributes({
|
|
1015
|
+
"llm.usage.prompt_tokens": usage.promptTokens,
|
|
1016
|
+
"llm.usage.completion_tokens": usage.completionTokens,
|
|
1017
|
+
"llm.usage.total_tokens": usage.totalTokens
|
|
1018
|
+
});
|
|
1019
|
+
emitModelUsageEvent(runtime, ModelType.TEXT_LARGE, prompt, usage);
|
|
1020
|
+
}
|
|
1021
|
+
return openaiResponse;
|
|
174
1022
|
});
|
|
175
|
-
return openaiResponse;
|
|
176
1023
|
},
|
|
177
1024
|
[ModelType.IMAGE]: async (runtime, params) => {
|
|
178
|
-
const
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
1025
|
+
const n = params.n || 1;
|
|
1026
|
+
const size = params.size || "1024x1024";
|
|
1027
|
+
const prompt = params.prompt;
|
|
1028
|
+
const modelName = "dall-e-3";
|
|
1029
|
+
logger.log(`[OpenAI] Using IMAGE model: ${modelName}`);
|
|
1030
|
+
const attributes = {
|
|
1031
|
+
"llm.vendor": "OpenAI",
|
|
1032
|
+
"llm.request.type": "image_generation",
|
|
1033
|
+
"llm.request.image.size": size,
|
|
1034
|
+
"llm.request.image.count": n
|
|
1035
|
+
};
|
|
1036
|
+
return startLlmSpan(runtime, "LLM.imageGeneration", attributes, async (span) => {
|
|
1037
|
+
span.addEvent("llm.prompt", { "prompt.content": prompt });
|
|
1038
|
+
const baseURL = getBaseURL(runtime);
|
|
1039
|
+
const apiKey = getApiKey(runtime);
|
|
1040
|
+
if (!apiKey) {
|
|
1041
|
+
span.setStatus({
|
|
1042
|
+
code: SpanStatusCode.ERROR,
|
|
1043
|
+
message: "OpenAI API key not configured"
|
|
1044
|
+
});
|
|
1045
|
+
throw new Error("OpenAI API key not configured");
|
|
1046
|
+
}
|
|
1047
|
+
try {
|
|
1048
|
+
const response = await fetch(`${baseURL}/images/generations`, {
|
|
1049
|
+
method: "POST",
|
|
1050
|
+
headers: {
|
|
1051
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1052
|
+
"Content-Type": "application/json"
|
|
1053
|
+
},
|
|
1054
|
+
body: JSON.stringify({
|
|
1055
|
+
prompt,
|
|
1056
|
+
n,
|
|
1057
|
+
size
|
|
1058
|
+
})
|
|
1059
|
+
});
|
|
1060
|
+
const responseClone = response.clone();
|
|
1061
|
+
const rawResponseBody = await responseClone.text();
|
|
1062
|
+
span.addEvent("llm.response.raw", {
|
|
1063
|
+
"response.body": rawResponseBody
|
|
1064
|
+
});
|
|
1065
|
+
if (!response.ok) {
|
|
1066
|
+
span.setAttributes({ "error.api.status": response.status });
|
|
1067
|
+
span.setStatus({
|
|
1068
|
+
code: SpanStatusCode.ERROR,
|
|
1069
|
+
message: `Failed to generate image: ${response.statusText}. Response: ${rawResponseBody}`
|
|
1070
|
+
});
|
|
1071
|
+
throw new Error(`Failed to generate image: ${response.statusText}`);
|
|
1072
|
+
}
|
|
1073
|
+
const data = await response.json();
|
|
1074
|
+
const typedData = data;
|
|
1075
|
+
span.addEvent("llm.response.processed", {
|
|
1076
|
+
"response.urls": JSON.stringify(typedData.data)
|
|
1077
|
+
});
|
|
1078
|
+
return typedData.data;
|
|
1079
|
+
} catch (error) {
|
|
1080
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1081
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
1082
|
+
span.recordException(exception);
|
|
1083
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
1084
|
+
throw error;
|
|
1085
|
+
}
|
|
190
1086
|
});
|
|
191
|
-
if (!response.ok) {
|
|
192
|
-
throw new Error(`Failed to generate image: ${response.statusText}`);
|
|
193
|
-
}
|
|
194
|
-
const data = await response.json();
|
|
195
|
-
const typedData = data;
|
|
196
|
-
return typedData.data;
|
|
197
1087
|
},
|
|
198
1088
|
[ModelType.IMAGE_DESCRIPTION]: async (runtime, params) => {
|
|
199
1089
|
let imageUrl;
|
|
200
|
-
let
|
|
1090
|
+
let promptText;
|
|
1091
|
+
const modelName = "gpt-4o-mini";
|
|
1092
|
+
logger.log(`[OpenAI] Using IMAGE_DESCRIPTION model: ${modelName}`);
|
|
1093
|
+
const maxTokens = 300;
|
|
201
1094
|
if (typeof params === "string") {
|
|
202
1095
|
imageUrl = params;
|
|
203
|
-
|
|
1096
|
+
promptText = "Please analyze this image and provide a title and detailed description.";
|
|
204
1097
|
} else {
|
|
205
1098
|
imageUrl = params.imageUrl;
|
|
206
|
-
|
|
1099
|
+
promptText = params.prompt || "Please analyze this image and provide a title and detailed description.";
|
|
207
1100
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
1101
|
+
const attributes = {
|
|
1102
|
+
"llm.vendor": "OpenAI",
|
|
1103
|
+
"llm.request.type": "chat",
|
|
1104
|
+
"llm.request.model": modelName,
|
|
1105
|
+
"llm.request.max_tokens": maxTokens,
|
|
1106
|
+
"llm.request.image.url": imageUrl
|
|
1107
|
+
};
|
|
1108
|
+
const messages = [
|
|
1109
|
+
{
|
|
1110
|
+
role: "user",
|
|
1111
|
+
content: [
|
|
1112
|
+
{ type: "text", text: promptText },
|
|
1113
|
+
{ type: "image_url", image_url: { url: imageUrl } }
|
|
1114
|
+
]
|
|
1115
|
+
}
|
|
1116
|
+
];
|
|
1117
|
+
return startLlmSpan(runtime, "LLM.imageDescription", attributes, async (span) => {
|
|
1118
|
+
span.addEvent("llm.prompt", {
|
|
1119
|
+
"prompt.content": JSON.stringify(messages, safeReplacer())
|
|
1120
|
+
});
|
|
1121
|
+
const baseURL = getBaseURL(runtime);
|
|
1122
|
+
const apiKey = getApiKey(runtime);
|
|
211
1123
|
if (!apiKey) {
|
|
212
1124
|
logger.error("OpenAI API key not set");
|
|
1125
|
+
span.setStatus({
|
|
1126
|
+
code: SpanStatusCode.ERROR,
|
|
1127
|
+
message: "OpenAI API key not configured"
|
|
1128
|
+
});
|
|
213
1129
|
return {
|
|
214
1130
|
title: "Failed to analyze image",
|
|
215
1131
|
description: "API key not configured"
|
|
216
1132
|
};
|
|
217
1133
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
1134
|
+
try {
|
|
1135
|
+
const response = await fetch(`${baseURL}/chat/completions`, {
|
|
1136
|
+
method: "POST",
|
|
1137
|
+
headers: {
|
|
1138
|
+
"Content-Type": "application/json",
|
|
1139
|
+
Authorization: `Bearer ${apiKey}`
|
|
1140
|
+
},
|
|
1141
|
+
body: JSON.stringify({
|
|
1142
|
+
model: modelName,
|
|
1143
|
+
messages,
|
|
1144
|
+
max_tokens: maxTokens
|
|
1145
|
+
})
|
|
1146
|
+
});
|
|
1147
|
+
const responseClone = response.clone();
|
|
1148
|
+
const rawResponseBody = await responseClone.text();
|
|
1149
|
+
span.addEvent("llm.response.raw", {
|
|
1150
|
+
"response.body": rawResponseBody
|
|
1151
|
+
});
|
|
1152
|
+
if (!response.ok) {
|
|
1153
|
+
span.setAttributes({ "error.api.status": response.status });
|
|
1154
|
+
span.setStatus({
|
|
1155
|
+
code: SpanStatusCode.ERROR,
|
|
1156
|
+
message: `OpenAI API error: ${response.status}. Response: ${rawResponseBody}`
|
|
1157
|
+
});
|
|
1158
|
+
throw new Error(`OpenAI API error: ${response.status}`);
|
|
1159
|
+
}
|
|
1160
|
+
const result = await response.json();
|
|
1161
|
+
const typedResult = result;
|
|
1162
|
+
const content = typedResult.choices?.[0]?.message?.content;
|
|
1163
|
+
if (typedResult.usage) {
|
|
1164
|
+
span.setAttributes({
|
|
1165
|
+
"llm.usage.prompt_tokens": typedResult.usage.prompt_tokens,
|
|
1166
|
+
"llm.usage.completion_tokens": typedResult.usage.completion_tokens,
|
|
1167
|
+
"llm.usage.total_tokens": typedResult.usage.total_tokens
|
|
1168
|
+
});
|
|
1169
|
+
}
|
|
1170
|
+
if (typedResult.choices?.[0]?.finish_reason) {
|
|
1171
|
+
span.setAttribute("llm.response.finish_reason", typedResult.choices[0].finish_reason);
|
|
1172
|
+
}
|
|
1173
|
+
if (!content) {
|
|
1174
|
+
span.setStatus({
|
|
1175
|
+
code: SpanStatusCode.ERROR,
|
|
1176
|
+
message: "No content in API response"
|
|
1177
|
+
});
|
|
1178
|
+
return {
|
|
1179
|
+
title: "Failed to analyze image",
|
|
1180
|
+
description: "No response from API"
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
const titleMatch = content.match(/title[:\s]+(.+?)(?:\n|$)/i);
|
|
1184
|
+
const title = titleMatch?.[1]?.trim() || "Image Analysis";
|
|
1185
|
+
const description = content.replace(/title[:\s]+(.+?)(?:\n|$)/i, "").trim();
|
|
1186
|
+
const processedResult = { title, description };
|
|
1187
|
+
span.addEvent("llm.response.processed", {
|
|
1188
|
+
"response.object": JSON.stringify(processedResult, safeReplacer())
|
|
1189
|
+
});
|
|
1190
|
+
return processedResult;
|
|
1191
|
+
} catch (error) {
|
|
1192
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1193
|
+
logger.error(`Error analyzing image: ${message}`);
|
|
1194
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
1195
|
+
span.recordException(exception);
|
|
1196
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
250
1197
|
return {
|
|
251
1198
|
title: "Failed to analyze image",
|
|
252
|
-
description:
|
|
1199
|
+
description: `Error: ${message}`
|
|
253
1200
|
};
|
|
254
1201
|
}
|
|
255
|
-
|
|
256
|
-
const title = titleMatch?.[1] || "Image Analysis";
|
|
257
|
-
const description = content.replace(/title[:\s]+(.+?)(?:\n|$)/i, "").trim();
|
|
258
|
-
return { title, description };
|
|
259
|
-
} catch (error) {
|
|
260
|
-
logger.error("Error analyzing image:", error);
|
|
261
|
-
return {
|
|
262
|
-
title: "Failed to analyze image",
|
|
263
|
-
description: `Error: ${error instanceof Error ? error.message : String(error)}`
|
|
264
|
-
};
|
|
265
|
-
}
|
|
1202
|
+
});
|
|
266
1203
|
},
|
|
267
1204
|
[ModelType.TRANSCRIPTION]: async (runtime, audioBuffer) => {
|
|
268
1205
|
logger.log("audioBuffer", audioBuffer);
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
1206
|
+
const modelName = "whisper-1";
|
|
1207
|
+
logger.log(`[OpenAI] Using TRANSCRIPTION model: ${modelName}`);
|
|
1208
|
+
const attributes = {
|
|
1209
|
+
"llm.vendor": "OpenAI",
|
|
1210
|
+
"llm.request.type": "transcription",
|
|
1211
|
+
"llm.request.model": modelName,
|
|
1212
|
+
"llm.request.audio.input_size_bytes": audioBuffer?.length || 0
|
|
1213
|
+
};
|
|
1214
|
+
return startLlmSpan(runtime, "LLM.transcription", attributes, async (span) => {
|
|
1215
|
+
span.addEvent("llm.prompt", {
|
|
1216
|
+
"prompt.info": "Audio buffer for transcription"
|
|
1217
|
+
});
|
|
1218
|
+
const baseURL = getBaseURL(runtime);
|
|
1219
|
+
const apiKey = getApiKey(runtime);
|
|
1220
|
+
if (!apiKey) {
|
|
1221
|
+
span.setStatus({
|
|
1222
|
+
code: SpanStatusCode.ERROR,
|
|
1223
|
+
message: "OpenAI API key not configured"
|
|
1224
|
+
});
|
|
1225
|
+
throw new Error("OpenAI API key not configured - Cannot make request");
|
|
1226
|
+
}
|
|
1227
|
+
if (!audioBuffer || audioBuffer.length === 0) {
|
|
1228
|
+
span.setStatus({
|
|
1229
|
+
code: SpanStatusCode.ERROR,
|
|
1230
|
+
message: "Audio buffer is empty or invalid"
|
|
1231
|
+
});
|
|
1232
|
+
throw new Error("Audio buffer is empty or invalid for transcription");
|
|
1233
|
+
}
|
|
1234
|
+
const formData = new FormData();
|
|
1235
|
+
formData.append("file", new Blob([audioBuffer]), "recording.mp3");
|
|
1236
|
+
formData.append("model", "whisper-1");
|
|
1237
|
+
try {
|
|
1238
|
+
const response = await fetch(`${baseURL}/audio/transcriptions`, {
|
|
1239
|
+
method: "POST",
|
|
1240
|
+
headers: {
|
|
1241
|
+
Authorization: `Bearer ${apiKey}`
|
|
1242
|
+
},
|
|
1243
|
+
body: formData
|
|
1244
|
+
});
|
|
1245
|
+
const responseClone = response.clone();
|
|
1246
|
+
const rawResponseBody = await responseClone.text();
|
|
1247
|
+
span.addEvent("llm.response.raw", {
|
|
1248
|
+
"response.body": rawResponseBody
|
|
1249
|
+
});
|
|
1250
|
+
logger.log("response", response);
|
|
1251
|
+
if (!response.ok) {
|
|
1252
|
+
span.setAttributes({ "error.api.status": response.status });
|
|
1253
|
+
span.setStatus({
|
|
1254
|
+
code: SpanStatusCode.ERROR,
|
|
1255
|
+
message: `Failed to transcribe audio: ${response.statusText}. Response: ${rawResponseBody}`
|
|
1256
|
+
});
|
|
1257
|
+
throw new Error(`Failed to transcribe audio: ${response.statusText}`);
|
|
1258
|
+
}
|
|
1259
|
+
const data = await response.json();
|
|
1260
|
+
const processedText = data.text;
|
|
1261
|
+
span.setAttribute("llm.response.processed.length", processedText.length);
|
|
1262
|
+
span.addEvent("llm.response.processed", {
|
|
1263
|
+
"response.text": processedText
|
|
1264
|
+
});
|
|
1265
|
+
return processedText;
|
|
1266
|
+
} catch (error) {
|
|
1267
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1268
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
1269
|
+
span.recordException(exception);
|
|
1270
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
1271
|
+
throw error;
|
|
1272
|
+
}
|
|
280
1273
|
});
|
|
281
|
-
logger.log("response", response);
|
|
282
|
-
if (!response.ok) {
|
|
283
|
-
throw new Error(`Failed to transcribe audio: ${response.statusText}`);
|
|
284
|
-
}
|
|
285
|
-
const data = await response.json();
|
|
286
|
-
return data.text;
|
|
287
1274
|
},
|
|
288
|
-
[ModelType.
|
|
289
|
-
const
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
1275
|
+
[ModelType.TEXT_TO_SPEECH]: async (runtime, text) => {
|
|
1276
|
+
const ttsModelName = getSetting(runtime, "OPENAI_TTS_MODEL", "gpt-4o-mini-tts");
|
|
1277
|
+
const attributes = {
|
|
1278
|
+
"llm.vendor": "OpenAI",
|
|
1279
|
+
"llm.request.type": "tts",
|
|
1280
|
+
"llm.request.model": ttsModelName,
|
|
1281
|
+
"input.text.length": text.length
|
|
1282
|
+
};
|
|
1283
|
+
return startLlmSpan(runtime, "LLM.tts", attributes, async (span) => {
|
|
1284
|
+
logger.log(`[OpenAI] Using TEXT_TO_SPEECH model: ${ttsModelName}`);
|
|
1285
|
+
span.addEvent("llm.prompt", { "prompt.content": text });
|
|
1286
|
+
try {
|
|
1287
|
+
const speechStream = await fetchTextToSpeech(runtime, text);
|
|
1288
|
+
span.addEvent("llm.response.success", {
|
|
1289
|
+
info: "Speech stream generated"
|
|
303
1290
|
});
|
|
304
|
-
return
|
|
1291
|
+
return speechStream;
|
|
1292
|
+
} catch (error) {
|
|
1293
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1294
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
1295
|
+
span.recordException(exception);
|
|
1296
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
1297
|
+
throw error;
|
|
305
1298
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
temperature: params.temperature
|
|
311
|
-
});
|
|
312
|
-
return object;
|
|
313
|
-
} catch (error) {
|
|
314
|
-
logger.error("Error generating object:", error);
|
|
315
|
-
throw error;
|
|
316
|
-
}
|
|
1299
|
+
});
|
|
1300
|
+
},
|
|
1301
|
+
[ModelType.OBJECT_SMALL]: async (runtime, params) => {
|
|
1302
|
+
return generateObjectByModelType(runtime, params, ModelType.OBJECT_SMALL, getSmallModel);
|
|
317
1303
|
},
|
|
318
1304
|
[ModelType.OBJECT_LARGE]: async (runtime, params) => {
|
|
319
|
-
|
|
320
|
-
const openai = createOpenAI({
|
|
321
|
-
apiKey: runtime.getSetting("OPENAI_API_KEY"),
|
|
322
|
-
baseURL
|
|
323
|
-
});
|
|
324
|
-
const model = runtime.getSetting("OPENAI_LARGE_MODEL") ?? runtime.getSetting("LARGE_MODEL") ?? "gpt-4o";
|
|
325
|
-
try {
|
|
326
|
-
if (params.schema) {
|
|
327
|
-
logger.info("Using OBJECT_LARGE without schema validation");
|
|
328
|
-
const { object: object2 } = await generateObject({
|
|
329
|
-
model: openai.languageModel(model),
|
|
330
|
-
output: "no-schema",
|
|
331
|
-
prompt: params.prompt,
|
|
332
|
-
temperature: params.temperature
|
|
333
|
-
});
|
|
334
|
-
return object2;
|
|
335
|
-
}
|
|
336
|
-
const { object } = await generateObject({
|
|
337
|
-
model: openai.languageModel(model),
|
|
338
|
-
output: "no-schema",
|
|
339
|
-
prompt: params.prompt,
|
|
340
|
-
temperature: params.temperature
|
|
341
|
-
});
|
|
342
|
-
return object;
|
|
343
|
-
} catch (error) {
|
|
344
|
-
logger.error("Error generating object:", error);
|
|
345
|
-
throw error;
|
|
346
|
-
}
|
|
1305
|
+
return generateObjectByModelType(runtime, params, ModelType.OBJECT_LARGE, getLargeModel);
|
|
347
1306
|
}
|
|
348
1307
|
},
|
|
349
1308
|
tests: [
|
|
@@ -353,14 +1312,14 @@ var openaiPlugin = {
|
|
|
353
1312
|
{
|
|
354
1313
|
name: "openai_test_url_and_api_key_validation",
|
|
355
1314
|
fn: async (runtime) => {
|
|
356
|
-
const baseURL = runtime
|
|
1315
|
+
const baseURL = getBaseURL(runtime);
|
|
357
1316
|
const response = await fetch(`${baseURL}/models`, {
|
|
358
1317
|
headers: {
|
|
359
|
-
Authorization: `Bearer ${runtime
|
|
1318
|
+
Authorization: `Bearer ${getApiKey(runtime)}`
|
|
360
1319
|
}
|
|
361
1320
|
});
|
|
362
1321
|
const data = await response.json();
|
|
363
|
-
logger.log("Models Available:", data?.data
|
|
1322
|
+
logger.log("Models Available:", data?.data?.length ?? "N/A");
|
|
364
1323
|
if (!response.ok) {
|
|
365
1324
|
throw new Error(`Failed to validate OpenAI API key: ${response.statusText}`);
|
|
366
1325
|
}
|
|
@@ -375,7 +1334,8 @@ var openaiPlugin = {
|
|
|
375
1334
|
});
|
|
376
1335
|
logger.log("embedding", embedding);
|
|
377
1336
|
} catch (error) {
|
|
378
|
-
|
|
1337
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1338
|
+
logger.error(`Error in test_text_embedding: ${message}`);
|
|
379
1339
|
throw error;
|
|
380
1340
|
}
|
|
381
1341
|
}
|
|
@@ -392,7 +1352,8 @@ var openaiPlugin = {
|
|
|
392
1352
|
}
|
|
393
1353
|
logger.log("generated with test_text_large:", text);
|
|
394
1354
|
} catch (error) {
|
|
395
|
-
|
|
1355
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1356
|
+
logger.error(`Error in test_text_large: ${message}`);
|
|
396
1357
|
throw error;
|
|
397
1358
|
}
|
|
398
1359
|
}
|
|
@@ -409,7 +1370,8 @@ var openaiPlugin = {
|
|
|
409
1370
|
}
|
|
410
1371
|
logger.log("generated with test_text_small:", text);
|
|
411
1372
|
} catch (error) {
|
|
412
|
-
|
|
1373
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1374
|
+
logger.error(`Error in test_text_small: ${message}`);
|
|
413
1375
|
throw error;
|
|
414
1376
|
}
|
|
415
1377
|
}
|
|
@@ -426,7 +1388,8 @@ var openaiPlugin = {
|
|
|
426
1388
|
});
|
|
427
1389
|
logger.log("generated with test_image_generation:", image);
|
|
428
1390
|
} catch (error) {
|
|
429
|
-
|
|
1391
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1392
|
+
logger.error(`Error in test_image_generation: ${message}`);
|
|
430
1393
|
throw error;
|
|
431
1394
|
}
|
|
432
1395
|
}
|
|
@@ -447,10 +1410,12 @@ var openaiPlugin = {
|
|
|
447
1410
|
logger.error("Invalid image description result format:", result);
|
|
448
1411
|
}
|
|
449
1412
|
} catch (e) {
|
|
450
|
-
|
|
1413
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
1414
|
+
logger.error(`Error in image description test: ${message}`);
|
|
451
1415
|
}
|
|
452
1416
|
} catch (e) {
|
|
453
|
-
|
|
1417
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
1418
|
+
logger.error(`Error in openai_test_image_description: ${message}`);
|
|
454
1419
|
}
|
|
455
1420
|
}
|
|
456
1421
|
},
|
|
@@ -469,7 +1434,8 @@ var openaiPlugin = {
|
|
|
469
1434
|
);
|
|
470
1435
|
logger.log("generated with test_transcription:", transcription);
|
|
471
1436
|
} catch (error) {
|
|
472
|
-
|
|
1437
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1438
|
+
logger.error(`Error in test_transcription: ${message}`);
|
|
473
1439
|
throw error;
|
|
474
1440
|
}
|
|
475
1441
|
}
|
|
@@ -498,6 +1464,23 @@ var openaiPlugin = {
|
|
|
498
1464
|
}
|
|
499
1465
|
logger.log("Decoded text:", decodedText);
|
|
500
1466
|
}
|
|
1467
|
+
},
|
|
1468
|
+
{
|
|
1469
|
+
name: "openai_test_text_to_speech",
|
|
1470
|
+
fn: async (runtime) => {
|
|
1471
|
+
try {
|
|
1472
|
+
const text = "Hello, this is a test for text-to-speech.";
|
|
1473
|
+
const response = await fetchTextToSpeech(runtime, text);
|
|
1474
|
+
if (!response) {
|
|
1475
|
+
throw new Error("Failed to generate speech");
|
|
1476
|
+
}
|
|
1477
|
+
logger.log("Generated speech successfully");
|
|
1478
|
+
} catch (error) {
|
|
1479
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1480
|
+
logger.error(`Error in openai_test_text_to_speech: ${message}`);
|
|
1481
|
+
throw error;
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
501
1484
|
}
|
|
502
1485
|
]
|
|
503
1486
|
}
|