@micdrop/openai 1.6.0 → 1.7.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/README.md +29 -0
- package/dist/index.d.mts +105 -2
- package/dist/index.d.ts +105 -2
- package/dist/index.js +134 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +116 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -215,6 +215,35 @@ new MicdropServer(socket, {
|
|
|
215
215
|
|
|
216
216
|
It emits the events of an agent, plus `Audio` (the voice of the model, PCM 16 bits, 16 kHz, mono) and `PartialMessage` (the transcript of the answer so far).
|
|
217
217
|
|
|
218
|
+
## OpenAI Classifier
|
|
219
|
+
|
|
220
|
+
Classifies each turn of the user with the [Decisions API](https://developers.openai.com/api/docs/guides/decisions): predicates, choices and scores answered with probabilities by `gpt-6-luna`.
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import { choice, OpenaiClassifier, predicate } from '@micdrop/openai'
|
|
224
|
+
import { MicdropServer } from '@micdrop/server'
|
|
225
|
+
|
|
226
|
+
const classifier = new OpenaiClassifier({
|
|
227
|
+
apiKey: process.env.OPENAI_API_KEY || '',
|
|
228
|
+
questions: {
|
|
229
|
+
intent: choice('What does the user in `turn` want?', {
|
|
230
|
+
billing: 'A charge, an invoice, a refund',
|
|
231
|
+
outage: 'The service is down or slow',
|
|
232
|
+
other: 'Anything else',
|
|
233
|
+
}),
|
|
234
|
+
wantsHuman: predicate('Does the user in `turn` ask for a human?'),
|
|
235
|
+
},
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
new MicdropServer(socket, {
|
|
239
|
+
stt,
|
|
240
|
+
agent,
|
|
241
|
+
tts,
|
|
242
|
+
classifier,
|
|
243
|
+
classifierOptions: { waitBeforeAnswer: true },
|
|
244
|
+
})
|
|
245
|
+
```
|
|
246
|
+
|
|
218
247
|
## Documentation
|
|
219
248
|
|
|
220
249
|
Read full [documentation of the OpenAI integration for Micdrop](https://micdrop.dev/docs/ai-integration/provided-integrations/openai) on the [website](https://micdrop.dev).
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Agent, AgentOptions, Realtime, RealtimeOptions, STT, TTS } from '@micdrop/server';
|
|
1
|
+
import { Agent, AgentOptions, Classifier, ClassifierInput, Realtime, RealtimeOptions, STT, TTS } from '@micdrop/server';
|
|
2
2
|
import OpenAI from 'openai';
|
|
3
3
|
import { PassThrough, Readable } from 'stream';
|
|
4
4
|
|
|
@@ -24,6 +24,109 @@ declare class OpenaiAgent extends Agent<OpenaiAgentOptions> {
|
|
|
24
24
|
cancel(): void;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* OpenAI classifier, running the Decisions API: typed questions (predicate,
|
|
29
|
+
* choice, score) answered with probabilities, about 10x faster than the
|
|
30
|
+
* Responses API.
|
|
31
|
+
*
|
|
32
|
+
* @see https://developers.openai.com/api/docs/guides/decisions
|
|
33
|
+
*/
|
|
34
|
+
/** The probability that a condition is true */
|
|
35
|
+
interface OpenaiPredicateQuestion {
|
|
36
|
+
type: 'predicate';
|
|
37
|
+
instructions: string;
|
|
38
|
+
}
|
|
39
|
+
/** One value out of several, without order */
|
|
40
|
+
interface OpenaiChoiceQuestion<V extends string = string> {
|
|
41
|
+
type: 'choice';
|
|
42
|
+
instructions: string;
|
|
43
|
+
choices: Record<V, string | null>;
|
|
44
|
+
}
|
|
45
|
+
/** A position on ordered levels, from the lowest */
|
|
46
|
+
interface OpenaiScoreQuestion {
|
|
47
|
+
type: 'score';
|
|
48
|
+
instructions: string;
|
|
49
|
+
levels: Array<string | {
|
|
50
|
+
label: string;
|
|
51
|
+
description?: string;
|
|
52
|
+
}>;
|
|
53
|
+
}
|
|
54
|
+
type OpenaiQuestion = OpenaiPredicateQuestion | OpenaiChoiceQuestion<any> | OpenaiScoreQuestion;
|
|
55
|
+
type OpenaiQuestions = Record<string, OpenaiQuestion>;
|
|
56
|
+
/**
|
|
57
|
+
* Asks for the probability that a condition is true
|
|
58
|
+
* @param instructions - The condition, written around what can be observed
|
|
59
|
+
*/
|
|
60
|
+
declare function predicate(instructions: string): OpenaiPredicateQuestion;
|
|
61
|
+
/**
|
|
62
|
+
* Asks for one value out of several
|
|
63
|
+
* @param instructions - The question
|
|
64
|
+
* @param choices - Each value with a description of when it applies, or null
|
|
65
|
+
*/
|
|
66
|
+
declare function choice<V extends string>(instructions: string, choices: Record<V, string | null>): OpenaiChoiceQuestion<V>;
|
|
67
|
+
/**
|
|
68
|
+
* Asks for a score on ordered levels
|
|
69
|
+
* @param instructions - The question
|
|
70
|
+
* @param levels - At least two levels, from the lowest up, each a label or a
|
|
71
|
+
* label with a description
|
|
72
|
+
*/
|
|
73
|
+
declare function score(instructions: string, levels: OpenaiScoreQuestion['levels']): OpenaiScoreQuestion;
|
|
74
|
+
interface OpenaiPredicateAnswer {
|
|
75
|
+
/** Probability that the condition is true, from 0 to 1 */
|
|
76
|
+
probability: number;
|
|
77
|
+
}
|
|
78
|
+
interface OpenaiChoiceAnswer<V extends string = string> {
|
|
79
|
+
choice: V;
|
|
80
|
+
confidence: number;
|
|
81
|
+
probabilities: Record<V, number>;
|
|
82
|
+
}
|
|
83
|
+
interface OpenaiScoreAnswer {
|
|
84
|
+
/** Probability-weighted average of the level indices, from 0 */
|
|
85
|
+
score: number;
|
|
86
|
+
confidence: number;
|
|
87
|
+
/** Probability of each level, by index */
|
|
88
|
+
probabilities: Record<number, number>;
|
|
89
|
+
}
|
|
90
|
+
type OpenaiAnswer<Q extends OpenaiQuestion> = Q extends OpenaiChoiceQuestion<infer V> ? OpenaiChoiceAnswer<V> : Q extends OpenaiScoreQuestion ? OpenaiScoreAnswer : OpenaiPredicateAnswer;
|
|
91
|
+
/** What an OpenAI classifier returns for its questions */
|
|
92
|
+
interface OpenaiClassification<Q extends OpenaiQuestions> {
|
|
93
|
+
/** The answer to each question, missing when the model refused it */
|
|
94
|
+
answers: {
|
|
95
|
+
[K in keyof Q]?: OpenaiAnswer<Q[K]>;
|
|
96
|
+
};
|
|
97
|
+
/** The questions the model refused to answer */
|
|
98
|
+
refusals: Array<keyof Q & string>;
|
|
99
|
+
usage: OpenAI.Decision['usage'];
|
|
100
|
+
}
|
|
101
|
+
type OpenaiClassifierOptions<Q extends OpenaiQuestions> = OpenaiOptions & {
|
|
102
|
+
/** Model answering the questions, `gpt-6-luna` by default */
|
|
103
|
+
model?: string;
|
|
104
|
+
/**
|
|
105
|
+
* Questions asked about each input, built with `predicate()`, `choice()`
|
|
106
|
+
* and `score()`. A function picks them per input.
|
|
107
|
+
*
|
|
108
|
+
* Every question is answered in a single request. Point at the fields of
|
|
109
|
+
* the input by name, between backticks: in a call, `turn` and `history`.
|
|
110
|
+
*/
|
|
111
|
+
questions: Q | ((input: ClassifierInput) => Q);
|
|
112
|
+
/**
|
|
113
|
+
* Builds what the model reads from the input: a text, or user messages
|
|
114
|
+
* with text and images. The input as JSON by default, a text as is.
|
|
115
|
+
*/
|
|
116
|
+
input?: (input: ClassifierInput) => string | OpenAI.Decisions.DecisionInputMessage[];
|
|
117
|
+
/** Identifies the end user to OpenAI, for abuse detection */
|
|
118
|
+
safetyIdentifier?: string;
|
|
119
|
+
/** Timeout of each request in ms, 3000 by default */
|
|
120
|
+
timeout?: number;
|
|
121
|
+
};
|
|
122
|
+
declare class OpenaiClassifier<Q extends OpenaiQuestions> extends Classifier<OpenaiClassification<Q>> {
|
|
123
|
+
private options;
|
|
124
|
+
private openai;
|
|
125
|
+
constructor(options: OpenaiClassifierOptions<Q>);
|
|
126
|
+
protected evaluate(input: ClassifierInput, signal: AbortSignal): Promise<OpenaiClassification<Q>>;
|
|
127
|
+
private buildInput;
|
|
128
|
+
}
|
|
129
|
+
|
|
27
130
|
/**
|
|
28
131
|
* OpenAI Realtime API, a model hearing the user and answering with its voice
|
|
29
132
|
*
|
|
@@ -194,4 +297,4 @@ declare class OpenaiTTS extends TTS {
|
|
|
194
297
|
private speakItem;
|
|
195
298
|
}
|
|
196
299
|
|
|
197
|
-
export { OpenaiAgent, type OpenaiAgentOptions, type OpenaiOptions, OpenaiRealtime, type OpenaiRealtimeOptions, OpenaiSTT, type OpenaiSTTOptions, OpenaiTTS, type OpenaiTTSOptions };
|
|
300
|
+
export { OpenaiAgent, type OpenaiAgentOptions, type OpenaiAnswer, type OpenaiChoiceAnswer, type OpenaiChoiceQuestion, type OpenaiClassification, OpenaiClassifier, type OpenaiClassifierOptions, type OpenaiOptions, type OpenaiPredicateAnswer, type OpenaiPredicateQuestion, type OpenaiQuestion, type OpenaiQuestions, OpenaiRealtime, type OpenaiRealtimeOptions, OpenaiSTT, type OpenaiSTTOptions, type OpenaiScoreAnswer, type OpenaiScoreQuestion, OpenaiTTS, type OpenaiTTSOptions, choice, predicate, score };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Agent, AgentOptions, Realtime, RealtimeOptions, STT, TTS } from '@micdrop/server';
|
|
1
|
+
import { Agent, AgentOptions, Classifier, ClassifierInput, Realtime, RealtimeOptions, STT, TTS } from '@micdrop/server';
|
|
2
2
|
import OpenAI from 'openai';
|
|
3
3
|
import { PassThrough, Readable } from 'stream';
|
|
4
4
|
|
|
@@ -24,6 +24,109 @@ declare class OpenaiAgent extends Agent<OpenaiAgentOptions> {
|
|
|
24
24
|
cancel(): void;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* OpenAI classifier, running the Decisions API: typed questions (predicate,
|
|
29
|
+
* choice, score) answered with probabilities, about 10x faster than the
|
|
30
|
+
* Responses API.
|
|
31
|
+
*
|
|
32
|
+
* @see https://developers.openai.com/api/docs/guides/decisions
|
|
33
|
+
*/
|
|
34
|
+
/** The probability that a condition is true */
|
|
35
|
+
interface OpenaiPredicateQuestion {
|
|
36
|
+
type: 'predicate';
|
|
37
|
+
instructions: string;
|
|
38
|
+
}
|
|
39
|
+
/** One value out of several, without order */
|
|
40
|
+
interface OpenaiChoiceQuestion<V extends string = string> {
|
|
41
|
+
type: 'choice';
|
|
42
|
+
instructions: string;
|
|
43
|
+
choices: Record<V, string | null>;
|
|
44
|
+
}
|
|
45
|
+
/** A position on ordered levels, from the lowest */
|
|
46
|
+
interface OpenaiScoreQuestion {
|
|
47
|
+
type: 'score';
|
|
48
|
+
instructions: string;
|
|
49
|
+
levels: Array<string | {
|
|
50
|
+
label: string;
|
|
51
|
+
description?: string;
|
|
52
|
+
}>;
|
|
53
|
+
}
|
|
54
|
+
type OpenaiQuestion = OpenaiPredicateQuestion | OpenaiChoiceQuestion<any> | OpenaiScoreQuestion;
|
|
55
|
+
type OpenaiQuestions = Record<string, OpenaiQuestion>;
|
|
56
|
+
/**
|
|
57
|
+
* Asks for the probability that a condition is true
|
|
58
|
+
* @param instructions - The condition, written around what can be observed
|
|
59
|
+
*/
|
|
60
|
+
declare function predicate(instructions: string): OpenaiPredicateQuestion;
|
|
61
|
+
/**
|
|
62
|
+
* Asks for one value out of several
|
|
63
|
+
* @param instructions - The question
|
|
64
|
+
* @param choices - Each value with a description of when it applies, or null
|
|
65
|
+
*/
|
|
66
|
+
declare function choice<V extends string>(instructions: string, choices: Record<V, string | null>): OpenaiChoiceQuestion<V>;
|
|
67
|
+
/**
|
|
68
|
+
* Asks for a score on ordered levels
|
|
69
|
+
* @param instructions - The question
|
|
70
|
+
* @param levels - At least two levels, from the lowest up, each a label or a
|
|
71
|
+
* label with a description
|
|
72
|
+
*/
|
|
73
|
+
declare function score(instructions: string, levels: OpenaiScoreQuestion['levels']): OpenaiScoreQuestion;
|
|
74
|
+
interface OpenaiPredicateAnswer {
|
|
75
|
+
/** Probability that the condition is true, from 0 to 1 */
|
|
76
|
+
probability: number;
|
|
77
|
+
}
|
|
78
|
+
interface OpenaiChoiceAnswer<V extends string = string> {
|
|
79
|
+
choice: V;
|
|
80
|
+
confidence: number;
|
|
81
|
+
probabilities: Record<V, number>;
|
|
82
|
+
}
|
|
83
|
+
interface OpenaiScoreAnswer {
|
|
84
|
+
/** Probability-weighted average of the level indices, from 0 */
|
|
85
|
+
score: number;
|
|
86
|
+
confidence: number;
|
|
87
|
+
/** Probability of each level, by index */
|
|
88
|
+
probabilities: Record<number, number>;
|
|
89
|
+
}
|
|
90
|
+
type OpenaiAnswer<Q extends OpenaiQuestion> = Q extends OpenaiChoiceQuestion<infer V> ? OpenaiChoiceAnswer<V> : Q extends OpenaiScoreQuestion ? OpenaiScoreAnswer : OpenaiPredicateAnswer;
|
|
91
|
+
/** What an OpenAI classifier returns for its questions */
|
|
92
|
+
interface OpenaiClassification<Q extends OpenaiQuestions> {
|
|
93
|
+
/** The answer to each question, missing when the model refused it */
|
|
94
|
+
answers: {
|
|
95
|
+
[K in keyof Q]?: OpenaiAnswer<Q[K]>;
|
|
96
|
+
};
|
|
97
|
+
/** The questions the model refused to answer */
|
|
98
|
+
refusals: Array<keyof Q & string>;
|
|
99
|
+
usage: OpenAI.Decision['usage'];
|
|
100
|
+
}
|
|
101
|
+
type OpenaiClassifierOptions<Q extends OpenaiQuestions> = OpenaiOptions & {
|
|
102
|
+
/** Model answering the questions, `gpt-6-luna` by default */
|
|
103
|
+
model?: string;
|
|
104
|
+
/**
|
|
105
|
+
* Questions asked about each input, built with `predicate()`, `choice()`
|
|
106
|
+
* and `score()`. A function picks them per input.
|
|
107
|
+
*
|
|
108
|
+
* Every question is answered in a single request. Point at the fields of
|
|
109
|
+
* the input by name, between backticks: in a call, `turn` and `history`.
|
|
110
|
+
*/
|
|
111
|
+
questions: Q | ((input: ClassifierInput) => Q);
|
|
112
|
+
/**
|
|
113
|
+
* Builds what the model reads from the input: a text, or user messages
|
|
114
|
+
* with text and images. The input as JSON by default, a text as is.
|
|
115
|
+
*/
|
|
116
|
+
input?: (input: ClassifierInput) => string | OpenAI.Decisions.DecisionInputMessage[];
|
|
117
|
+
/** Identifies the end user to OpenAI, for abuse detection */
|
|
118
|
+
safetyIdentifier?: string;
|
|
119
|
+
/** Timeout of each request in ms, 3000 by default */
|
|
120
|
+
timeout?: number;
|
|
121
|
+
};
|
|
122
|
+
declare class OpenaiClassifier<Q extends OpenaiQuestions> extends Classifier<OpenaiClassification<Q>> {
|
|
123
|
+
private options;
|
|
124
|
+
private openai;
|
|
125
|
+
constructor(options: OpenaiClassifierOptions<Q>);
|
|
126
|
+
protected evaluate(input: ClassifierInput, signal: AbortSignal): Promise<OpenaiClassification<Q>>;
|
|
127
|
+
private buildInput;
|
|
128
|
+
}
|
|
129
|
+
|
|
27
130
|
/**
|
|
28
131
|
* OpenAI Realtime API, a model hearing the user and answering with its voice
|
|
29
132
|
*
|
|
@@ -194,4 +297,4 @@ declare class OpenaiTTS extends TTS {
|
|
|
194
297
|
private speakItem;
|
|
195
298
|
}
|
|
196
299
|
|
|
197
|
-
export { OpenaiAgent, type OpenaiAgentOptions, type OpenaiOptions, OpenaiRealtime, type OpenaiRealtimeOptions, OpenaiSTT, type OpenaiSTTOptions, OpenaiTTS, type OpenaiTTSOptions };
|
|
300
|
+
export { OpenaiAgent, type OpenaiAgentOptions, type OpenaiAnswer, type OpenaiChoiceAnswer, type OpenaiChoiceQuestion, type OpenaiClassification, OpenaiClassifier, type OpenaiClassifierOptions, type OpenaiOptions, type OpenaiPredicateAnswer, type OpenaiPredicateQuestion, type OpenaiQuestion, type OpenaiQuestions, OpenaiRealtime, type OpenaiRealtimeOptions, OpenaiSTT, type OpenaiSTTOptions, type OpenaiScoreAnswer, type OpenaiScoreQuestion, OpenaiTTS, type OpenaiTTSOptions, choice, predicate, score };
|
package/dist/index.js
CHANGED
|
@@ -31,9 +31,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
OpenaiAgent: () => OpenaiAgent,
|
|
34
|
+
OpenaiClassifier: () => OpenaiClassifier,
|
|
34
35
|
OpenaiRealtime: () => OpenaiRealtime,
|
|
35
36
|
OpenaiSTT: () => OpenaiSTT,
|
|
36
|
-
OpenaiTTS: () => OpenaiTTS
|
|
37
|
+
OpenaiTTS: () => OpenaiTTS,
|
|
38
|
+
choice: () => choice,
|
|
39
|
+
predicate: () => predicate,
|
|
40
|
+
score: () => score
|
|
37
41
|
});
|
|
38
42
|
module.exports = __toCommonJS(index_exports);
|
|
39
43
|
|
|
@@ -184,8 +188,108 @@ var OpenaiAgent = class extends import_server.Agent {
|
|
|
184
188
|
}
|
|
185
189
|
};
|
|
186
190
|
|
|
187
|
-
// src/
|
|
191
|
+
// src/OpenaiClassifier.ts
|
|
188
192
|
var import_server2 = require("@micdrop/server");
|
|
193
|
+
var import_openai2 = __toESM(require("openai"));
|
|
194
|
+
function predicate(instructions) {
|
|
195
|
+
return { type: "predicate", instructions };
|
|
196
|
+
}
|
|
197
|
+
function choice(instructions, choices) {
|
|
198
|
+
return { type: "choice", instructions, choices };
|
|
199
|
+
}
|
|
200
|
+
function score(instructions, levels) {
|
|
201
|
+
return { type: "score", instructions, levels };
|
|
202
|
+
}
|
|
203
|
+
var DEFAULT_MODEL2 = "gpt-6-luna";
|
|
204
|
+
var DEFAULT_TIMEOUT = 3e3;
|
|
205
|
+
var OpenaiClassifier = class extends import_server2.Classifier {
|
|
206
|
+
constructor(options) {
|
|
207
|
+
super();
|
|
208
|
+
this.options = options;
|
|
209
|
+
this.openai = "openai" in options ? options.openai : new import_openai2.default({ apiKey: options.apiKey });
|
|
210
|
+
}
|
|
211
|
+
async evaluate(input, signal) {
|
|
212
|
+
const { questions, model, safetyIdentifier, timeout } = this.options;
|
|
213
|
+
const asked = typeof questions === "function" ? questions(input) : questions;
|
|
214
|
+
const names = Object.keys(asked);
|
|
215
|
+
const decision = await this.openai.decisions.create(
|
|
216
|
+
{
|
|
217
|
+
model: model || DEFAULT_MODEL2,
|
|
218
|
+
input: this.buildInput(input),
|
|
219
|
+
questions: names.map((name) => buildQuestion(name, asked[name])),
|
|
220
|
+
safety_identifier: safetyIdentifier
|
|
221
|
+
},
|
|
222
|
+
{ signal, timeout: timeout ?? DEFAULT_TIMEOUT, maxRetries: 0 }
|
|
223
|
+
);
|
|
224
|
+
const answers = {};
|
|
225
|
+
const refusals = [];
|
|
226
|
+
decision.answers.forEach((answer, index) => {
|
|
227
|
+
const name = answer.name ?? names[index];
|
|
228
|
+
switch (answer.type) {
|
|
229
|
+
case "predicate":
|
|
230
|
+
answers[name] = { probability: answer.probability };
|
|
231
|
+
break;
|
|
232
|
+
case "choice":
|
|
233
|
+
answers[name] = {
|
|
234
|
+
choice: answer.choice,
|
|
235
|
+
confidence: answer.confidence,
|
|
236
|
+
probabilities: Object.fromEntries(
|
|
237
|
+
answer.probabilities.map((p) => [String(p.value), p.probability])
|
|
238
|
+
)
|
|
239
|
+
};
|
|
240
|
+
break;
|
|
241
|
+
case "score":
|
|
242
|
+
answers[name] = {
|
|
243
|
+
score: answer.score,
|
|
244
|
+
confidence: answer.confidence,
|
|
245
|
+
probabilities: Object.fromEntries(
|
|
246
|
+
answer.probabilities.map((p) => [p.value, p.probability])
|
|
247
|
+
)
|
|
248
|
+
};
|
|
249
|
+
break;
|
|
250
|
+
case "refusal":
|
|
251
|
+
refusals.push(name);
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
return {
|
|
256
|
+
answers,
|
|
257
|
+
refusals,
|
|
258
|
+
usage: decision.usage
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
buildInput(input) {
|
|
262
|
+
if (this.options.input) return this.options.input(input);
|
|
263
|
+
return typeof input === "string" ? input : JSON.stringify(input);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
function buildQuestion(name, question) {
|
|
267
|
+
switch (question.type) {
|
|
268
|
+
case "predicate":
|
|
269
|
+
return { type: "predicate", name, instructions: question.instructions };
|
|
270
|
+
case "choice":
|
|
271
|
+
return {
|
|
272
|
+
type: "choice",
|
|
273
|
+
name,
|
|
274
|
+
instructions: question.instructions,
|
|
275
|
+
choices: Object.entries(question.choices).map(
|
|
276
|
+
([value, description]) => description ? { value, description } : { value }
|
|
277
|
+
)
|
|
278
|
+
};
|
|
279
|
+
case "score":
|
|
280
|
+
return {
|
|
281
|
+
type: "score",
|
|
282
|
+
name,
|
|
283
|
+
instructions: question.instructions,
|
|
284
|
+
levels: question.levels.map(
|
|
285
|
+
(level) => typeof level === "string" ? { label: level } : level
|
|
286
|
+
)
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// src/OpenaiRealtime.ts
|
|
292
|
+
var import_server3 = require("@micdrop/server");
|
|
189
293
|
var import_ws = __toESM(require("ws"));
|
|
190
294
|
var import_zod2 = __toESM(require("zod"));
|
|
191
295
|
|
|
@@ -199,7 +303,7 @@ function isListingLanguages(model) {
|
|
|
199
303
|
}
|
|
200
304
|
|
|
201
305
|
// src/OpenaiRealtime.ts
|
|
202
|
-
var
|
|
306
|
+
var DEFAULT_MODEL3 = "gpt-realtime-2.1";
|
|
203
307
|
var DEFAULT_VOICE = "marin";
|
|
204
308
|
var DEFAULT_TRANSCRIPTION_MODEL = "gpt-4o-transcribe";
|
|
205
309
|
var SAMPLE_RATE = 16e3;
|
|
@@ -208,7 +312,7 @@ var OPENAI_BYTES_PER_MS = OPENAI_SAMPLE_RATE * 2 / 1e3;
|
|
|
208
312
|
var DEFAULT_CONNECTION_TIMEOUT = 5e3;
|
|
209
313
|
var DEFAULT_RETRY_DELAY2 = 1e3;
|
|
210
314
|
var DEFAULT_MAX_RETRY = 3;
|
|
211
|
-
var OpenaiRealtime = class extends
|
|
315
|
+
var OpenaiRealtime = class extends import_server3.Realtime {
|
|
212
316
|
constructor(options) {
|
|
213
317
|
super(options);
|
|
214
318
|
this.connected = false;
|
|
@@ -217,8 +321,8 @@ var OpenaiRealtime = class extends import_server2.Realtime {
|
|
|
217
321
|
// Events sent while the connection opens
|
|
218
322
|
this.pending = [];
|
|
219
323
|
this.retryCount = 0;
|
|
220
|
-
this.inputResampler = new
|
|
221
|
-
this.outputResampler = new
|
|
324
|
+
this.inputResampler = new import_server3.Pcm16Resampler(SAMPLE_RATE, OPENAI_SAMPLE_RATE);
|
|
325
|
+
this.outputResampler = new import_server3.Pcm16Resampler(OPENAI_SAMPLE_RATE, SAMPLE_RATE);
|
|
222
326
|
// Bumped by every interruption, so late responses can tell they are stale
|
|
223
327
|
this.generation = 0;
|
|
224
328
|
this.requested = [];
|
|
@@ -328,7 +432,7 @@ var OpenaiRealtime = class extends import_server2.Realtime {
|
|
|
328
432
|
type: "session.update",
|
|
329
433
|
session: {
|
|
330
434
|
type: "realtime",
|
|
331
|
-
model: this.options.model ||
|
|
435
|
+
model: this.options.model || DEFAULT_MODEL3,
|
|
332
436
|
instructions: this.options.systemPrompt,
|
|
333
437
|
output_modalities: ["audio"],
|
|
334
438
|
audio: {
|
|
@@ -368,7 +472,7 @@ var OpenaiRealtime = class extends import_server2.Realtime {
|
|
|
368
472
|
}
|
|
369
473
|
initWS() {
|
|
370
474
|
return new Promise((resolve, reject) => {
|
|
371
|
-
const model = this.options.model ||
|
|
475
|
+
const model = this.options.model || DEFAULT_MODEL3;
|
|
372
476
|
const socket = new import_ws.default(
|
|
373
477
|
`wss://api.openai.com/v1/realtime?model=${encodeURIComponent(model)}`,
|
|
374
478
|
{ headers: { Authorization: `Bearer ${this.options.apiKey}` } }
|
|
@@ -554,9 +658,9 @@ var OpenaiRealtime = class extends import_server2.Realtime {
|
|
|
554
658
|
};
|
|
555
659
|
|
|
556
660
|
// src/OpenaiSTT.ts
|
|
557
|
-
var
|
|
661
|
+
var import_server4 = require("@micdrop/server");
|
|
558
662
|
var import_ws2 = __toESM(require("ws"));
|
|
559
|
-
var
|
|
663
|
+
var DEFAULT_MODEL4 = "gpt-4o-transcribe";
|
|
560
664
|
var DEFAULT_LANGUAGE = "en";
|
|
561
665
|
var SAMPLE_RATE2 = 16e3;
|
|
562
666
|
var OPENAI_SAMPLE_RATE2 = 24e3;
|
|
@@ -564,7 +668,7 @@ var DEFAULT_CONNECTION_TIMEOUT2 = 5e3;
|
|
|
564
668
|
var DEFAULT_TRANSCRIPTION_TIMEOUT = 4e3;
|
|
565
669
|
var DEFAULT_RETRY_DELAY3 = 1e3;
|
|
566
670
|
var DEFAULT_MAX_RETRY2 = 3;
|
|
567
|
-
var OpenaiSTT = class extends
|
|
671
|
+
var OpenaiSTT = class extends import_server4.STT {
|
|
568
672
|
constructor(options) {
|
|
569
673
|
super();
|
|
570
674
|
this.options = options;
|
|
@@ -572,7 +676,7 @@ var OpenaiSTT = class extends import_server3.STT {
|
|
|
572
676
|
this.destroyed = false;
|
|
573
677
|
this.audioChunksPending = [];
|
|
574
678
|
// Store audio chunks to send them again if reconnecting
|
|
575
|
-
this.resampler = new
|
|
679
|
+
this.resampler = new import_server4.Pcm16Resampler(SAMPLE_RATE2, OPENAI_SAMPLE_RATE2);
|
|
576
680
|
this.initPromise = this.initWS().catch((error) => {
|
|
577
681
|
console.error("[OpenaiSTT] Connection error:", error);
|
|
578
682
|
this.reconnect();
|
|
@@ -618,7 +722,7 @@ var OpenaiSTT = class extends import_server3.STT {
|
|
|
618
722
|
}
|
|
619
723
|
sendSessionUpdate() {
|
|
620
724
|
if (!this.socket) return;
|
|
621
|
-
const model = this.options.model ||
|
|
725
|
+
const model = this.options.model || DEFAULT_MODEL4;
|
|
622
726
|
this.socket.send(
|
|
623
727
|
JSON.stringify({
|
|
624
728
|
type: "session.update",
|
|
@@ -786,11 +890,11 @@ function closeSocket(socket) {
|
|
|
786
890
|
}
|
|
787
891
|
|
|
788
892
|
// src/OpenaiTTS.ts
|
|
789
|
-
var
|
|
790
|
-
var
|
|
893
|
+
var import_server6 = require("@micdrop/server");
|
|
894
|
+
var import_openai3 = __toESM(require("openai"));
|
|
791
895
|
|
|
792
896
|
// src/OpenaiRealtimeSpeech.ts
|
|
793
|
-
var
|
|
897
|
+
var import_server5 = require("@micdrop/server");
|
|
794
898
|
var import_ws3 = __toESM(require("ws"));
|
|
795
899
|
var DEFAULT_REASONING_EFFORT = "minimal";
|
|
796
900
|
var DEFAULT_CONNECTION_TIMEOUT3 = 5e3;
|
|
@@ -819,7 +923,7 @@ var OpenaiRealtimeSpeech = class {
|
|
|
819
923
|
const item = {
|
|
820
924
|
id: `micdrop-tts-${++this.itemCount}`,
|
|
821
925
|
text,
|
|
822
|
-
resampler: new
|
|
926
|
+
resampler: new import_server5.Pcm16Resampler(OPENAI_SAMPLE_RATE3, OUTPUT_SAMPLE_RATE),
|
|
823
927
|
audio: [],
|
|
824
928
|
done: false
|
|
825
929
|
};
|
|
@@ -1063,14 +1167,14 @@ function normalize(text = "") {
|
|
|
1063
1167
|
}
|
|
1064
1168
|
|
|
1065
1169
|
// src/OpenaiTTS.ts
|
|
1066
|
-
var
|
|
1170
|
+
var DEFAULT_MODEL5 = "gpt-4o-mini-tts";
|
|
1067
1171
|
var DEFAULT_VOICE2 = "alloy";
|
|
1068
1172
|
var DEFAULT_REALTIME_VOICE = "marin";
|
|
1069
1173
|
var OPENAI_SAMPLE_RATE4 = 24e3;
|
|
1070
1174
|
var OUTPUT_SAMPLE_RATE2 = 16e3;
|
|
1071
1175
|
var MAX_IN_FLIGHT = 2;
|
|
1072
1176
|
var OPENING_CUSHION = 300 * 32;
|
|
1073
|
-
var OpenaiTTS = class extends
|
|
1177
|
+
var OpenaiTTS = class extends import_server6.TTS {
|
|
1074
1178
|
constructor(options) {
|
|
1075
1179
|
super();
|
|
1076
1180
|
this.options = options;
|
|
@@ -1081,7 +1185,7 @@ var OpenaiTTS = class extends import_server5.TTS {
|
|
|
1081
1185
|
// `counter`, which numbers the synthesis calls and must only move when there
|
|
1082
1186
|
// is something to synthesize.
|
|
1083
1187
|
this.generation = 0;
|
|
1084
|
-
this.splitter = new
|
|
1188
|
+
this.splitter = new import_server6.SentenceSplitter();
|
|
1085
1189
|
this.queue = [];
|
|
1086
1190
|
// Sentences whose synthesis has not started
|
|
1087
1191
|
this.pending = [];
|
|
@@ -1092,11 +1196,11 @@ var OpenaiTTS = class extends import_server5.TTS {
|
|
|
1092
1196
|
this.processing = false;
|
|
1093
1197
|
// More than one request is in flight at a time, so they are stopped as a set.
|
|
1094
1198
|
this.syntheses = /* @__PURE__ */ new Set();
|
|
1095
|
-
const model = options.model ||
|
|
1199
|
+
const model = options.model || DEFAULT_MODEL5;
|
|
1096
1200
|
if (model.startsWith("gpt-realtime")) {
|
|
1097
1201
|
this.realtime = new OpenaiRealtimeSpeech(
|
|
1098
1202
|
{
|
|
1099
|
-
apiKey: "openai" in options ? options.openai.apiKey : options.apiKey,
|
|
1203
|
+
apiKey: ("openai" in options ? options.openai.apiKey : options.apiKey) ?? "",
|
|
1100
1204
|
model,
|
|
1101
1205
|
voice: options.voice || DEFAULT_REALTIME_VOICE,
|
|
1102
1206
|
instructions: options.instructions,
|
|
@@ -1107,7 +1211,7 @@ var OpenaiTTS = class extends import_server5.TTS {
|
|
|
1107
1211
|
(...message) => this.log(...message)
|
|
1108
1212
|
);
|
|
1109
1213
|
} else {
|
|
1110
|
-
this.openai = "openai" in options ? options.openai : new
|
|
1214
|
+
this.openai = "openai" in options ? options.openai : new import_openai3.default({ apiKey: options.apiKey });
|
|
1111
1215
|
}
|
|
1112
1216
|
}
|
|
1113
1217
|
speak(textStream) {
|
|
@@ -1205,7 +1309,7 @@ var OpenaiTTS = class extends import_server5.TTS {
|
|
|
1205
1309
|
const controller = new AbortController();
|
|
1206
1310
|
const response = this.openai.audio.speech.create(
|
|
1207
1311
|
{
|
|
1208
|
-
model: this.options.model ||
|
|
1312
|
+
model: this.options.model || DEFAULT_MODEL5,
|
|
1209
1313
|
voice: this.options.voice || DEFAULT_VOICE2,
|
|
1210
1314
|
input: text,
|
|
1211
1315
|
response_format: "pcm",
|
|
@@ -1219,7 +1323,7 @@ var OpenaiTTS = class extends import_server5.TTS {
|
|
|
1219
1323
|
async function* read() {
|
|
1220
1324
|
const { body } = await response;
|
|
1221
1325
|
if (!body) return;
|
|
1222
|
-
const resampler = new
|
|
1326
|
+
const resampler = new import_server6.Pcm16Resampler(
|
|
1223
1327
|
OPENAI_SAMPLE_RATE4,
|
|
1224
1328
|
OUTPUT_SAMPLE_RATE2
|
|
1225
1329
|
);
|
|
@@ -1277,8 +1381,12 @@ var OpenaiTTS = class extends import_server5.TTS {
|
|
|
1277
1381
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1278
1382
|
0 && (module.exports = {
|
|
1279
1383
|
OpenaiAgent,
|
|
1384
|
+
OpenaiClassifier,
|
|
1280
1385
|
OpenaiRealtime,
|
|
1281
1386
|
OpenaiSTT,
|
|
1282
|
-
OpenaiTTS
|
|
1387
|
+
OpenaiTTS,
|
|
1388
|
+
choice,
|
|
1389
|
+
predicate,
|
|
1390
|
+
score
|
|
1283
1391
|
});
|
|
1284
1392
|
//# sourceMappingURL=index.js.map
|