@micdrop/openai 1.6.1 → 1.7.1

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/dist/index.mjs CHANGED
@@ -145,6 +145,106 @@ var OpenaiAgent = class extends Agent {
145
145
  }
146
146
  };
147
147
 
148
+ // src/OpenaiClassifier.ts
149
+ import { Classifier } from "@micdrop/server";
150
+ import OpenAI2 from "openai";
151
+ function predicate(instructions) {
152
+ return { type: "predicate", instructions };
153
+ }
154
+ function choice(instructions, choices) {
155
+ return { type: "choice", instructions, choices };
156
+ }
157
+ function score(instructions, levels) {
158
+ return { type: "score", instructions, levels };
159
+ }
160
+ var DEFAULT_MODEL2 = "gpt-6-luna";
161
+ var DEFAULT_TIMEOUT = 3e3;
162
+ var OpenaiClassifier = class extends Classifier {
163
+ constructor(options) {
164
+ super();
165
+ this.options = options;
166
+ this.openai = "openai" in options ? options.openai : new OpenAI2({ apiKey: options.apiKey });
167
+ }
168
+ async evaluate(input, signal) {
169
+ const { questions, model, safetyIdentifier, timeout } = this.options;
170
+ const asked = typeof questions === "function" ? questions(input) : questions;
171
+ const names = Object.keys(asked);
172
+ const decision = await this.openai.decisions.create(
173
+ {
174
+ model: model || DEFAULT_MODEL2,
175
+ input: this.buildInput(input),
176
+ questions: names.map((name) => buildQuestion(name, asked[name])),
177
+ safety_identifier: safetyIdentifier
178
+ },
179
+ { signal, timeout: timeout ?? DEFAULT_TIMEOUT, maxRetries: 0 }
180
+ );
181
+ const answers = {};
182
+ const refusals = [];
183
+ decision.answers.forEach((answer, index) => {
184
+ const name = answer.name ?? names[index];
185
+ switch (answer.type) {
186
+ case "predicate":
187
+ answers[name] = { probability: answer.probability };
188
+ break;
189
+ case "choice":
190
+ answers[name] = {
191
+ choice: answer.choice,
192
+ confidence: answer.confidence,
193
+ probabilities: Object.fromEntries(
194
+ answer.probabilities.map((p) => [String(p.value), p.probability])
195
+ )
196
+ };
197
+ break;
198
+ case "score":
199
+ answers[name] = {
200
+ score: answer.score,
201
+ confidence: answer.confidence,
202
+ probabilities: Object.fromEntries(
203
+ answer.probabilities.map((p) => [p.value, p.probability])
204
+ )
205
+ };
206
+ break;
207
+ case "refusal":
208
+ refusals.push(name);
209
+ break;
210
+ }
211
+ });
212
+ return {
213
+ answers,
214
+ refusals,
215
+ usage: decision.usage
216
+ };
217
+ }
218
+ buildInput(input) {
219
+ if (this.options.input) return this.options.input(input);
220
+ return typeof input === "string" ? input : JSON.stringify(input);
221
+ }
222
+ };
223
+ function buildQuestion(name, question) {
224
+ switch (question.type) {
225
+ case "predicate":
226
+ return { type: "predicate", name, instructions: question.instructions };
227
+ case "choice":
228
+ return {
229
+ type: "choice",
230
+ name,
231
+ instructions: question.instructions,
232
+ choices: Object.entries(question.choices).map(
233
+ ([value, description]) => description ? { value, description } : { value }
234
+ )
235
+ };
236
+ case "score":
237
+ return {
238
+ type: "score",
239
+ name,
240
+ instructions: question.instructions,
241
+ levels: question.levels.map(
242
+ (level) => typeof level === "string" ? { label: level } : level
243
+ )
244
+ };
245
+ }
246
+ }
247
+
148
248
  // src/OpenaiRealtime.ts
149
249
  import {
150
250
  Pcm16Resampler,
@@ -163,7 +263,7 @@ function isListingLanguages(model) {
163
263
  }
164
264
 
165
265
  // src/OpenaiRealtime.ts
166
- var DEFAULT_MODEL2 = "gpt-realtime-2.1";
266
+ var DEFAULT_MODEL3 = "gpt-realtime-2.1";
167
267
  var DEFAULT_VOICE = "marin";
168
268
  var DEFAULT_TRANSCRIPTION_MODEL = "gpt-4o-transcribe";
169
269
  var SAMPLE_RATE = 16e3;
@@ -292,7 +392,7 @@ var OpenaiRealtime = class extends Realtime {
292
392
  type: "session.update",
293
393
  session: {
294
394
  type: "realtime",
295
- model: this.options.model || DEFAULT_MODEL2,
395
+ model: this.options.model || DEFAULT_MODEL3,
296
396
  instructions: this.options.systemPrompt,
297
397
  output_modalities: ["audio"],
298
398
  audio: {
@@ -332,7 +432,7 @@ var OpenaiRealtime = class extends Realtime {
332
432
  }
333
433
  initWS() {
334
434
  return new Promise((resolve, reject) => {
335
- const model = this.options.model || DEFAULT_MODEL2;
435
+ const model = this.options.model || DEFAULT_MODEL3;
336
436
  const socket = new WebSocket(
337
437
  `wss://api.openai.com/v1/realtime?model=${encodeURIComponent(model)}`,
338
438
  { headers: { Authorization: `Bearer ${this.options.apiKey}` } }
@@ -520,7 +620,7 @@ var OpenaiRealtime = class extends Realtime {
520
620
  // src/OpenaiSTT.ts
521
621
  import { Pcm16Resampler as Pcm16Resampler2, STT } from "@micdrop/server";
522
622
  import WebSocket2 from "ws";
523
- var DEFAULT_MODEL3 = "gpt-4o-transcribe";
623
+ var DEFAULT_MODEL4 = "gpt-4o-transcribe";
524
624
  var DEFAULT_LANGUAGE = "en";
525
625
  var SAMPLE_RATE2 = 16e3;
526
626
  var OPENAI_SAMPLE_RATE2 = 24e3;
@@ -582,7 +682,7 @@ var OpenaiSTT = class extends STT {
582
682
  }
583
683
  sendSessionUpdate() {
584
684
  if (!this.socket) return;
585
- const model = this.options.model || DEFAULT_MODEL3;
685
+ const model = this.options.model || DEFAULT_MODEL4;
586
686
  this.socket.send(
587
687
  JSON.stringify({
588
688
  type: "session.update",
@@ -751,7 +851,7 @@ function closeSocket(socket) {
751
851
 
752
852
  // src/OpenaiTTS.ts
753
853
  import { Pcm16Resampler as Pcm16Resampler4, SentenceSplitter, TTS } from "@micdrop/server";
754
- import OpenAI2 from "openai";
854
+ import OpenAI3 from "openai";
755
855
 
756
856
  // src/OpenaiRealtimeSpeech.ts
757
857
  import { Pcm16Resampler as Pcm16Resampler3 } from "@micdrop/server";
@@ -1027,7 +1127,7 @@ function normalize(text = "") {
1027
1127
  }
1028
1128
 
1029
1129
  // src/OpenaiTTS.ts
1030
- var DEFAULT_MODEL4 = "gpt-4o-mini-tts";
1130
+ var DEFAULT_MODEL5 = "gpt-4o-mini-tts";
1031
1131
  var DEFAULT_VOICE2 = "alloy";
1032
1132
  var DEFAULT_REALTIME_VOICE = "marin";
1033
1133
  var OPENAI_SAMPLE_RATE4 = 24e3;
@@ -1056,7 +1156,7 @@ var OpenaiTTS = class extends TTS {
1056
1156
  this.processing = false;
1057
1157
  // More than one request is in flight at a time, so they are stopped as a set.
1058
1158
  this.syntheses = /* @__PURE__ */ new Set();
1059
- const model = options.model || DEFAULT_MODEL4;
1159
+ const model = options.model || DEFAULT_MODEL5;
1060
1160
  if (model.startsWith("gpt-realtime")) {
1061
1161
  this.realtime = new OpenaiRealtimeSpeech(
1062
1162
  {
@@ -1071,7 +1171,7 @@ var OpenaiTTS = class extends TTS {
1071
1171
  (...message) => this.log(...message)
1072
1172
  );
1073
1173
  } else {
1074
- this.openai = "openai" in options ? options.openai : new OpenAI2({ apiKey: options.apiKey });
1174
+ this.openai = "openai" in options ? options.openai : new OpenAI3({ apiKey: options.apiKey });
1075
1175
  }
1076
1176
  }
1077
1177
  speak(textStream) {
@@ -1169,7 +1269,7 @@ var OpenaiTTS = class extends TTS {
1169
1269
  const controller = new AbortController();
1170
1270
  const response = this.openai.audio.speech.create(
1171
1271
  {
1172
- model: this.options.model || DEFAULT_MODEL4,
1272
+ model: this.options.model || DEFAULT_MODEL5,
1173
1273
  voice: this.options.voice || DEFAULT_VOICE2,
1174
1274
  input: text,
1175
1275
  response_format: "pcm",
@@ -1240,8 +1340,12 @@ var OpenaiTTS = class extends TTS {
1240
1340
  };
1241
1341
  export {
1242
1342
  OpenaiAgent,
1343
+ OpenaiClassifier,
1243
1344
  OpenaiRealtime,
1244
1345
  OpenaiSTT,
1245
- OpenaiTTS
1346
+ OpenaiTTS,
1347
+ choice,
1348
+ predicate,
1349
+ score
1246
1350
  };
1247
1351
  //# sourceMappingURL=index.mjs.map