@livekit/agents 0.5.2 → 0.6.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/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/llm/index.cjs +2 -0
- package/dist/llm/index.cjs.map +1 -1
- package/dist/llm/index.d.ts +1 -1
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +2 -0
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/llm.cjs +47 -3
- package/dist/llm/llm.cjs.map +1 -1
- package/dist/llm/llm.d.ts +15 -2
- package/dist/llm/llm.d.ts.map +1 -1
- package/dist/llm/llm.js +46 -3
- package/dist/llm/llm.js.map +1 -1
- package/dist/metrics/base.cjs +44 -0
- package/dist/metrics/base.cjs.map +1 -0
- package/dist/metrics/base.d.ts +96 -0
- package/dist/metrics/base.d.ts.map +1 -0
- package/dist/metrics/base.js +20 -0
- package/dist/metrics/base.js.map +1 -0
- package/dist/metrics/index.cjs +35 -0
- package/dist/metrics/index.cjs.map +1 -0
- package/dist/metrics/index.d.ts +5 -0
- package/dist/metrics/index.d.ts.map +1 -0
- package/dist/metrics/index.js +9 -0
- package/dist/metrics/index.js.map +1 -0
- package/dist/metrics/usage_collector.cjs +53 -0
- package/dist/metrics/usage_collector.cjs.map +1 -0
- package/dist/metrics/usage_collector.d.ts +14 -0
- package/dist/metrics/usage_collector.d.ts.map +1 -0
- package/dist/metrics/usage_collector.js +29 -0
- package/dist/metrics/usage_collector.js.map +1 -0
- package/dist/metrics/utils.cjs +104 -0
- package/dist/metrics/utils.cjs.map +1 -0
- package/dist/metrics/utils.d.ts +10 -0
- package/dist/metrics/utils.d.ts.map +1 -0
- package/dist/metrics/utils.js +73 -0
- package/dist/metrics/utils.js.map +1 -0
- package/dist/multimodal/multimodal_agent.cjs +7 -13
- package/dist/multimodal/multimodal_agent.cjs.map +1 -1
- package/dist/multimodal/multimodal_agent.d.ts +1 -4
- package/dist/multimodal/multimodal_agent.d.ts.map +1 -1
- package/dist/multimodal/multimodal_agent.js +7 -13
- package/dist/multimodal/multimodal_agent.js.map +1 -1
- package/dist/pipeline/index.cjs +2 -0
- package/dist/pipeline/index.cjs.map +1 -1
- package/dist/pipeline/index.d.ts +1 -1
- package/dist/pipeline/index.d.ts.map +1 -1
- package/dist/pipeline/index.js +3 -1
- package/dist/pipeline/index.js.map +1 -1
- package/dist/pipeline/pipeline_agent.cjs +166 -66
- package/dist/pipeline/pipeline_agent.cjs.map +1 -1
- package/dist/pipeline/pipeline_agent.d.ts +10 -4
- package/dist/pipeline/pipeline_agent.d.ts.map +1 -1
- package/dist/pipeline/pipeline_agent.js +169 -69
- package/dist/pipeline/pipeline_agent.js.map +1 -1
- package/dist/pipeline/speech_handle.cjs +49 -1
- package/dist/pipeline/speech_handle.cjs.map +1 -1
- package/dist/pipeline/speech_handle.d.ts +12 -2
- package/dist/pipeline/speech_handle.d.ts.map +1 -1
- package/dist/pipeline/speech_handle.js +50 -2
- package/dist/pipeline/speech_handle.js.map +1 -1
- package/dist/stt/index.cjs.map +1 -1
- package/dist/stt/index.d.ts +1 -1
- package/dist/stt/index.d.ts.map +1 -1
- package/dist/stt/index.js.map +1 -1
- package/dist/stt/stream_adapter.cjs +15 -5
- package/dist/stt/stream_adapter.cjs.map +1 -1
- package/dist/stt/stream_adapter.d.ts +4 -1
- package/dist/stt/stream_adapter.d.ts.map +1 -1
- package/dist/stt/stream_adapter.js +15 -5
- package/dist/stt/stream_adapter.js.map +1 -1
- package/dist/stt/stt.cjs +46 -2
- package/dist/stt/stt.cjs.map +1 -1
- package/dist/stt/stt.d.ts +25 -3
- package/dist/stt/stt.d.ts.map +1 -1
- package/dist/stt/stt.js +46 -2
- package/dist/stt/stt.js.map +1 -1
- package/dist/tts/index.cjs +4 -2
- package/dist/tts/index.cjs.map +1 -1
- package/dist/tts/index.d.ts +1 -1
- package/dist/tts/index.d.ts.map +1 -1
- package/dist/tts/index.js +3 -1
- package/dist/tts/index.js.map +1 -1
- package/dist/tts/stream_adapter.cjs +14 -3
- package/dist/tts/stream_adapter.cjs.map +1 -1
- package/dist/tts/stream_adapter.d.ts +3 -0
- package/dist/tts/stream_adapter.d.ts.map +1 -1
- package/dist/tts/stream_adapter.js +15 -4
- package/dist/tts/stream_adapter.js.map +1 -1
- package/dist/tts/tts.cjs +109 -6
- package/dist/tts/tts.cjs.map +1 -1
- package/dist/tts/tts.d.ts +24 -1
- package/dist/tts/tts.d.ts.map +1 -1
- package/dist/tts/tts.js +107 -5
- package/dist/tts/tts.js.map +1 -1
- package/dist/vad.cjs +43 -2
- package/dist/vad.cjs.map +1 -1
- package/dist/vad.d.ts +21 -4
- package/dist/vad.d.ts.map +1 -1
- package/dist/vad.js +43 -2
- package/dist/vad.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -1
- package/src/llm/index.ts +2 -0
- package/src/llm/llm.ts +55 -3
- package/src/metrics/base.ts +127 -0
- package/src/metrics/index.ts +20 -0
- package/src/metrics/usage_collector.ts +40 -0
- package/src/metrics/utils.ts +100 -0
- package/src/multimodal/multimodal_agent.ts +12 -17
- package/src/pipeline/index.ts +1 -1
- package/src/pipeline/pipeline_agent.ts +206 -87
- package/src/pipeline/speech_handle.ts +67 -2
- package/src/stt/index.ts +2 -0
- package/src/stt/stream_adapter.ts +17 -5
- package/src/stt/stt.ts +67 -3
- package/src/tts/index.ts +2 -0
- package/src/tts/stream_adapter.ts +17 -4
- package/src/tts/tts.ts +127 -4
- package/src/vad.ts +61 -4
package/dist/vad.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/vad.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame } from '@livekit/rtc-node';\nimport { AsyncIterableQueue } from './utils.js';\n\nexport enum VADEventType {\n START_OF_SPEECH,\n INFERENCE_DONE,\n END_OF_SPEECH,\n}\n\nexport interface VADEvent {\n /** Type of the VAD event (e.g., start of speech, end of speech, inference done). */\n type: VADEventType;\n /**\n * Index of the audio sample where the event occurred, relative to the inference sample rate.\n */\n samplesIndex: number;\n /** Timestamp when the event was fired. */\n timestamp: number;\n /** Duration of the
|
|
1
|
+
{"version":3,"sources":["../src/vad.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame } from '@livekit/rtc-node';\nimport type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';\nimport { EventEmitter } from 'node:events';\nimport type { VADMetrics } from './metrics/base.js';\nimport { AsyncIterableQueue } from './utils.js';\n\nexport enum VADEventType {\n START_OF_SPEECH,\n INFERENCE_DONE,\n END_OF_SPEECH,\n METRICS_COLLECTED,\n}\n\nexport interface VADEvent {\n /** Type of the VAD event (e.g., start of speech, end of speech, inference done). */\n type: VADEventType;\n /**\n * Index of the audio sample where the event occurred, relative to the inference sample rate.\n */\n samplesIndex: number;\n /** Timestamp when the event was fired. */\n timestamp: number;\n /** Duration of the speech segment. */\n speechDuration: number;\n /** Duration of the silence segment. */\n silenceDuration: number;\n /**\n * List of audio frames associated with the speech.\n *\n * @remarks\n * - For `start_of_speech` events, this contains the audio chunks that triggered the detection.\n * - For `inference_done` events, this contains the audio chunks that were processed.\n * - For `end_of_speech` events, this contains the complete user speech.\n */\n frames: AudioFrame[];\n /** Probability that speech is present (only for `INFERENCE_DONE` events). */\n probability: number;\n /** Time taken to perform the inference, in seconds (only for `INFERENCE_DONE` events). */\n inferenceDuration: number;\n /** Indicates whether speech was detected in the frames. */\n speaking: boolean;\n /** Threshold used to detect silence. */\n rawAccumulatedSilence: number;\n /** Threshold used to detect speech. */\n rawAccumulatedSpeech: number;\n}\n\nexport interface VADCapabilities {\n updateInterval: number;\n}\n\nexport type VADCallbacks = {\n [VADEventType.METRICS_COLLECTED]: (metrics: VADMetrics) => void;\n};\n\nexport abstract class VAD extends (EventEmitter as new () => TypedEmitter<VADCallbacks>) {\n #capabilities: VADCapabilities;\n abstract label: string;\n\n constructor(capabilities: VADCapabilities) {\n super();\n this.#capabilities = capabilities;\n }\n\n get capabilities(): VADCapabilities {\n return this.#capabilities;\n }\n\n /**\n * Returns a {@link VADStream} that can be used to push audio frames and receive VAD events.\n */\n abstract stream(): VADStream;\n}\n\nexport abstract class VADStream implements AsyncIterableIterator<VADEvent> {\n protected static readonly FLUSH_SENTINEL = Symbol('FLUSH_SENTINEL');\n protected input = new AsyncIterableQueue<AudioFrame | typeof VADStream.FLUSH_SENTINEL>();\n protected queue = new AsyncIterableQueue<VADEvent>();\n protected output = new AsyncIterableQueue<VADEvent>();\n protected closed = false;\n #vad: VAD;\n #lastActivityTime = BigInt(0);\n\n constructor(vad: VAD) {\n this.#vad = vad;\n this.monitorMetrics();\n }\n\n protected async monitorMetrics() {\n let inferenceDurationTotal = 0;\n let inferenceCount = 0;\n\n for await (const event of this.queue) {\n this.output.put(event);\n switch (event.type) {\n case VADEventType.START_OF_SPEECH:\n inferenceCount++;\n if (inferenceCount >= 1 / this.#vad.capabilities.updateInterval) {\n this.#vad.emit(VADEventType.METRICS_COLLECTED, {\n timestamp: Date.now(),\n idleTime: Math.trunc(\n Number((process.hrtime.bigint() - this.#lastActivityTime) / BigInt(1000000)),\n ),\n inferenceDurationTotal,\n inferenceCount,\n label: this.#vad.label,\n });\n\n inferenceCount = 0;\n inferenceDurationTotal = 0;\n }\n break;\n case VADEventType.INFERENCE_DONE:\n case VADEventType.END_OF_SPEECH:\n this.#lastActivityTime = process.hrtime.bigint();\n break;\n }\n }\n this.output.close();\n }\n\n pushFrame(frame: AudioFrame) {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n this.input.put(frame);\n }\n\n flush() {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n this.input.put(VADStream.FLUSH_SENTINEL);\n }\n\n endInput() {\n if (this.input.closed) {\n throw new Error('Input is closed');\n }\n if (this.closed) {\n throw new Error('Stream is closed');\n }\n this.input.close();\n }\n\n next(): Promise<IteratorResult<VADEvent>> {\n return this.output.next();\n }\n\n close() {\n this.input.close();\n this.queue.close();\n this.output.close();\n this.closed = true;\n }\n\n [Symbol.asyncIterator](): VADStream {\n return this;\n }\n}\n"],"mappings":"AAKA,SAAS,oBAAoB;AAE7B,SAAS,0BAA0B;AAE5B,IAAK,eAAL,kBAAKA,kBAAL;AACL,EAAAA,4BAAA;AACA,EAAAA,4BAAA;AACA,EAAAA,4BAAA;AACA,EAAAA,4BAAA;AAJU,SAAAA;AAAA,GAAA;AAiDL,MAAe,YAAa,aAAsD;AAAA,EACvF;AAAA,EAGA,YAAY,cAA+B;AACzC,UAAM;AACN,SAAK,gBAAgB;AAAA,EACvB;AAAA,EAEA,IAAI,eAAgC;AAClC,WAAO,KAAK;AAAA,EACd;AAMF;AAEO,MAAe,UAAqD;AAAA,EACzE,OAA0B,iBAAiB,OAAO,gBAAgB;AAAA,EACxD,QAAQ,IAAI,mBAAiE;AAAA,EAC7E,QAAQ,IAAI,mBAA6B;AAAA,EACzC,SAAS,IAAI,mBAA6B;AAAA,EAC1C,SAAS;AAAA,EACnB;AAAA,EACA,oBAAoB,OAAO,CAAC;AAAA,EAE5B,YAAY,KAAU;AACpB,SAAK,OAAO;AACZ,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,MAAgB,iBAAiB;AAC/B,QAAI,yBAAyB;AAC7B,QAAI,iBAAiB;AAErB,qBAAiB,SAAS,KAAK,OAAO;AACpC,WAAK,OAAO,IAAI,KAAK;AACrB,cAAQ,MAAM,MAAM;AAAA,QAClB,KAAK;AACH;AACA,cAAI,kBAAkB,IAAI,KAAK,KAAK,aAAa,gBAAgB;AAC/D,iBAAK,KAAK,KAAK,2BAAgC;AAAA,cAC7C,WAAW,KAAK,IAAI;AAAA,cACpB,UAAU,KAAK;AAAA,gBACb,QAAQ,QAAQ,OAAO,OAAO,IAAI,KAAK,qBAAqB,OAAO,GAAO,CAAC;AAAA,cAC7E;AAAA,cACA;AAAA,cACA;AAAA,cACA,OAAO,KAAK,KAAK;AAAA,YACnB,CAAC;AAED,6BAAiB;AACjB,qCAAyB;AAAA,UAC3B;AACA;AAAA,QACF,KAAK;AAAA,QACL,KAAK;AACH,eAAK,oBAAoB,QAAQ,OAAO,OAAO;AAC/C;AAAA,MACJ;AAAA,IACF;AACA,SAAK,OAAO,MAAM;AAAA,EACpB;AAAA,EAEA,UAAU,OAAmB;AAC3B,QAAI,KAAK,MAAM,QAAQ;AACrB,YAAM,IAAI,MAAM,iBAAiB;AAAA,IACnC;AACA,QAAI,KAAK,QAAQ;AACf,YAAM,IAAI,MAAM,kBAAkB;AAAA,IACpC;AACA,SAAK,MAAM,IAAI,KAAK;AAAA,EACtB;AAAA,EAEA,QAAQ;AACN,QAAI,KAAK,MAAM,QAAQ;AACrB,YAAM,IAAI,MAAM,iBAAiB;AAAA,IACnC;AACA,QAAI,KAAK,QAAQ;AACf,YAAM,IAAI,MAAM,kBAAkB;AAAA,IACpC;AACA,SAAK,MAAM,IAAI,UAAU,cAAc;AAAA,EACzC;AAAA,EAEA,WAAW;AACT,QAAI,KAAK,MAAM,QAAQ;AACrB,YAAM,IAAI,MAAM,iBAAiB;AAAA,IACnC;AACA,QAAI,KAAK,QAAQ;AACf,YAAM,IAAI,MAAM,kBAAkB;AAAA,IACpC;AACA,SAAK,MAAM,MAAM;AAAA,EACnB;AAAA,EAEA,OAA0C;AACxC,WAAO,KAAK,OAAO,KAAK;AAAA,EAC1B;AAAA,EAEA,QAAQ;AACN,SAAK,MAAM,MAAM;AACjB,SAAK,MAAM,MAAM;AACjB,SAAK,OAAO,MAAM;AAClB,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,CAAC,OAAO,aAAa,IAAe;AAClC,WAAO;AAAA,EACT;AACF;","names":["VADEventType"]}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import * as cli from './cli.js';
|
|
13
13
|
import * as llm from './llm/index.js';
|
|
14
|
+
import * as metrics from './metrics/index.js';
|
|
14
15
|
import * as multimodal from './multimodal/index.js';
|
|
15
16
|
import * as pipeline from './pipeline/index.js';
|
|
16
17
|
import * as stt from './stt/index.js';
|
|
@@ -28,4 +29,4 @@ export * from './generator.js';
|
|
|
28
29
|
export * from './audio.js';
|
|
29
30
|
export * from './transcription.js';
|
|
30
31
|
|
|
31
|
-
export { cli, stt, tts, llm, pipeline, multimodal, tokenize };
|
|
32
|
+
export { cli, stt, tts, llm, pipeline, multimodal, tokenize, metrics };
|
package/src/llm/index.ts
CHANGED
package/src/llm/llm.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
|
|
2
2
|
//
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';
|
|
5
|
+
import { EventEmitter } from 'node:events';
|
|
6
|
+
import type { LLMMetrics } from '../metrics/base.js';
|
|
4
7
|
import { AsyncIterableQueue } from '../utils.js';
|
|
5
8
|
import type { ChatContext, ChatRole } from './chat_context.js';
|
|
6
9
|
import type { FunctionCallInfo, FunctionContext } from './function_context.js';
|
|
@@ -28,7 +31,15 @@ export interface ChatChunk {
|
|
|
28
31
|
usage?: CompletionUsage;
|
|
29
32
|
}
|
|
30
33
|
|
|
31
|
-
export
|
|
34
|
+
export enum LLMEvent {
|
|
35
|
+
METRICS_COLLECTED,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type LLMCallbacks = {
|
|
39
|
+
[LLMEvent.METRICS_COLLECTED]: (metrics: LLMMetrics) => void;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export abstract class LLM extends (EventEmitter as new () => TypedEmitter<LLMCallbacks>) {
|
|
32
43
|
/**
|
|
33
44
|
* Returns a {@link LLMStream} that can be used to push text and receive LLM responses.
|
|
34
45
|
*/
|
|
@@ -48,16 +59,56 @@ export abstract class LLM {
|
|
|
48
59
|
}
|
|
49
60
|
|
|
50
61
|
export abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {
|
|
62
|
+
protected output = new AsyncIterableQueue<ChatChunk>();
|
|
51
63
|
protected queue = new AsyncIterableQueue<ChatChunk>();
|
|
52
64
|
protected closed = false;
|
|
53
65
|
protected _functionCalls: FunctionCallInfo[] = [];
|
|
66
|
+
abstract label: string;
|
|
54
67
|
|
|
68
|
+
#llm: LLM;
|
|
55
69
|
#chatCtx: ChatContext;
|
|
56
70
|
#fncCtx?: FunctionContext;
|
|
57
71
|
|
|
58
|
-
constructor(chatCtx: ChatContext, fncCtx?: FunctionContext) {
|
|
72
|
+
constructor(llm: LLM, chatCtx: ChatContext, fncCtx?: FunctionContext) {
|
|
73
|
+
this.#llm = llm;
|
|
59
74
|
this.#chatCtx = chatCtx;
|
|
60
75
|
this.#fncCtx = fncCtx;
|
|
76
|
+
this.monitorMetrics();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
protected async monitorMetrics() {
|
|
80
|
+
const startTime = process.hrtime.bigint();
|
|
81
|
+
let ttft: bigint | undefined;
|
|
82
|
+
let requestId = '';
|
|
83
|
+
let usage: CompletionUsage | undefined;
|
|
84
|
+
|
|
85
|
+
for await (const ev of this.queue) {
|
|
86
|
+
this.output.put(ev);
|
|
87
|
+
requestId = ev.requestId;
|
|
88
|
+
if (!ttft) {
|
|
89
|
+
ttft = process.hrtime.bigint() - startTime;
|
|
90
|
+
}
|
|
91
|
+
if (ev.usage) {
|
|
92
|
+
usage = ev.usage;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
this.output.close();
|
|
96
|
+
|
|
97
|
+
const duration = process.hrtime.bigint() - startTime;
|
|
98
|
+
const metrics: LLMMetrics = {
|
|
99
|
+
timestamp: Date.now(),
|
|
100
|
+
requestId,
|
|
101
|
+
ttft: Math.trunc(Number(ttft! / BigInt(1000000))),
|
|
102
|
+
duration: Math.trunc(Number(duration / BigInt(1000000))),
|
|
103
|
+
cancelled: false, // XXX(nbsp)
|
|
104
|
+
label: this.label,
|
|
105
|
+
completionTokens: usage?.completionTokens || 0,
|
|
106
|
+
promptTokens: usage?.promptTokens || 0,
|
|
107
|
+
totalTokens: usage?.totalTokens || 0,
|
|
108
|
+
tokensPerSecond:
|
|
109
|
+
(usage?.completionTokens || 0) / Math.trunc(Number(duration / BigInt(1000000000))),
|
|
110
|
+
};
|
|
111
|
+
this.#llm.emit(LLMEvent.METRICS_COLLECTED, metrics);
|
|
61
112
|
}
|
|
62
113
|
|
|
63
114
|
/** List of called functions from this stream. */
|
|
@@ -88,10 +139,11 @@ export abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {
|
|
|
88
139
|
}
|
|
89
140
|
|
|
90
141
|
next(): Promise<IteratorResult<ChatChunk>> {
|
|
91
|
-
return this.
|
|
142
|
+
return this.output.next();
|
|
92
143
|
}
|
|
93
144
|
|
|
94
145
|
close() {
|
|
146
|
+
this.output.close();
|
|
95
147
|
this.queue.close();
|
|
96
148
|
this.closed = true;
|
|
97
149
|
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
export interface LLMMetrics {
|
|
6
|
+
requestId: string;
|
|
7
|
+
timestamp: number;
|
|
8
|
+
ttft: number;
|
|
9
|
+
duration: number;
|
|
10
|
+
label: string;
|
|
11
|
+
cancelled: boolean;
|
|
12
|
+
completionTokens: number;
|
|
13
|
+
promptTokens: number;
|
|
14
|
+
totalTokens: number;
|
|
15
|
+
tokensPerSecond: number;
|
|
16
|
+
error?: Error;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface STTMetrics {
|
|
20
|
+
requestId: string;
|
|
21
|
+
timestamp: number;
|
|
22
|
+
duration: number;
|
|
23
|
+
label: string;
|
|
24
|
+
audioDuration: number;
|
|
25
|
+
streamed: boolean;
|
|
26
|
+
error?: Error;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface TTSMetrics {
|
|
30
|
+
requestId: string;
|
|
31
|
+
timestamp: number;
|
|
32
|
+
ttfb: number;
|
|
33
|
+
duration: number;
|
|
34
|
+
label: string;
|
|
35
|
+
audioDuration: number;
|
|
36
|
+
cancelled: boolean;
|
|
37
|
+
charactersCount: number;
|
|
38
|
+
streamed: boolean;
|
|
39
|
+
error?: Error;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface VADMetrics {
|
|
43
|
+
timestamp: number;
|
|
44
|
+
idleTime: number;
|
|
45
|
+
inferenceDurationTotal: number;
|
|
46
|
+
inferenceCount: number;
|
|
47
|
+
label: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface PipelineEOUMetrics {
|
|
51
|
+
/**
|
|
52
|
+
* Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics
|
|
53
|
+
*/
|
|
54
|
+
sequenceId: string;
|
|
55
|
+
/** Timestamp of when the event was recorded */
|
|
56
|
+
timestamp: number;
|
|
57
|
+
/** Amount of time between the end of speech from VAD and the decision to end the user's turn */
|
|
58
|
+
endOfUtteranceDelay: number;
|
|
59
|
+
/**
|
|
60
|
+
* Time taken to obtain the transcript after the end of the user's speech.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* May be 0 if the transcript was already available.
|
|
64
|
+
*/
|
|
65
|
+
transcriptionDelay: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface PipelineLLMMetrics extends LLMMetrics {
|
|
69
|
+
/**
|
|
70
|
+
* Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics
|
|
71
|
+
*/
|
|
72
|
+
sequenceId: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface PipelineTTSMetrics extends TTSMetrics {
|
|
76
|
+
/**
|
|
77
|
+
* Unique identifier shared across different metrics to combine related STT, LLM, and TTS metrics
|
|
78
|
+
*/
|
|
79
|
+
sequenceId: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type PipelineSTTMetrics = STTMetrics;
|
|
83
|
+
export type PipelineVADMetrics = VADMetrics;
|
|
84
|
+
|
|
85
|
+
export class MultimodalLLMError extends Error {
|
|
86
|
+
type?: string;
|
|
87
|
+
reason?: string;
|
|
88
|
+
code?: string;
|
|
89
|
+
constructor(
|
|
90
|
+
{
|
|
91
|
+
type,
|
|
92
|
+
reason,
|
|
93
|
+
code,
|
|
94
|
+
message,
|
|
95
|
+
}: { type?: string; reason?: string; code?: string; message?: string } = {},
|
|
96
|
+
options?: ErrorOptions,
|
|
97
|
+
) {
|
|
98
|
+
super(message, options);
|
|
99
|
+
this.type = type;
|
|
100
|
+
this.reason = reason;
|
|
101
|
+
this.code = code;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface MultimodalLLMMetrics extends LLMMetrics {
|
|
106
|
+
inputTokenDetails: {
|
|
107
|
+
cachedTokens: number;
|
|
108
|
+
textTokens: number;
|
|
109
|
+
audioTokens: number;
|
|
110
|
+
};
|
|
111
|
+
outputTokenDetails: {
|
|
112
|
+
textTokens: number;
|
|
113
|
+
audioTokens: number;
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export type AgentMetrics =
|
|
118
|
+
| STTMetrics
|
|
119
|
+
| LLMMetrics
|
|
120
|
+
| TTSMetrics
|
|
121
|
+
| VADMetrics
|
|
122
|
+
| PipelineSTTMetrics
|
|
123
|
+
| PipelineEOUMetrics
|
|
124
|
+
| PipelineLLMMetrics
|
|
125
|
+
| PipelineTTSMetrics
|
|
126
|
+
| PipelineVADMetrics
|
|
127
|
+
| MultimodalLLMMetrics;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
export type {
|
|
6
|
+
AgentMetrics,
|
|
7
|
+
STTMetrics,
|
|
8
|
+
LLMMetrics,
|
|
9
|
+
TTSMetrics,
|
|
10
|
+
VADMetrics,
|
|
11
|
+
PipelineSTTMetrics,
|
|
12
|
+
PipelineEOUMetrics,
|
|
13
|
+
PipelineLLMMetrics,
|
|
14
|
+
PipelineTTSMetrics,
|
|
15
|
+
PipelineVADMetrics,
|
|
16
|
+
MultimodalLLMMetrics,
|
|
17
|
+
} from './base.js';
|
|
18
|
+
export { MultimodalLLMError } from './base.js';
|
|
19
|
+
export { type UsageSummary, UsageCollector } from './usage_collector.js';
|
|
20
|
+
export { logMetrics } from './utils.js';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import type { AgentMetrics } from './base.js';
|
|
5
|
+
import { isLLMMetrics, isSTTMetrics, isTTSMetrics } from './utils.js';
|
|
6
|
+
|
|
7
|
+
export interface UsageSummary {
|
|
8
|
+
llmPromptTokens: number;
|
|
9
|
+
llmCompletionTokens: number;
|
|
10
|
+
ttsCharactersCount: number;
|
|
11
|
+
sttAudioDuration: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class UsageCollector {
|
|
15
|
+
#summary: UsageSummary;
|
|
16
|
+
|
|
17
|
+
constructor() {
|
|
18
|
+
this.#summary = {
|
|
19
|
+
llmPromptTokens: 0,
|
|
20
|
+
llmCompletionTokens: 0,
|
|
21
|
+
ttsCharactersCount: 0,
|
|
22
|
+
sttAudioDuration: 0,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
collect(metrics: AgentMetrics) {
|
|
27
|
+
if (isLLMMetrics(metrics)) {
|
|
28
|
+
this.#summary.llmPromptTokens += metrics.promptTokens;
|
|
29
|
+
this.#summary.llmCompletionTokens += metrics.completionTokens;
|
|
30
|
+
} else if (isTTSMetrics(metrics)) {
|
|
31
|
+
this.#summary.ttsCharactersCount += metrics.charactersCount;
|
|
32
|
+
} else if (isSTTMetrics(metrics)) {
|
|
33
|
+
this.#summary.sttAudioDuration += metrics.audioDuration;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get summary(): UsageSummary {
|
|
38
|
+
return { ...this.#summary };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { log } from '../log.js';
|
|
5
|
+
import type {
|
|
6
|
+
AgentMetrics,
|
|
7
|
+
LLMMetrics,
|
|
8
|
+
PipelineEOUMetrics,
|
|
9
|
+
PipelineLLMMetrics,
|
|
10
|
+
PipelineTTSMetrics,
|
|
11
|
+
STTMetrics,
|
|
12
|
+
TTSMetrics,
|
|
13
|
+
VADMetrics,
|
|
14
|
+
} from './base.js';
|
|
15
|
+
|
|
16
|
+
export const logMetrics = (metrics: AgentMetrics) => {
|
|
17
|
+
const logger = log();
|
|
18
|
+
if (isPipelineLLMMetrics(metrics)) {
|
|
19
|
+
logger
|
|
20
|
+
.child({
|
|
21
|
+
sequenceId: metrics.sequenceId,
|
|
22
|
+
ttft: metrics.ttft,
|
|
23
|
+
inputTokens: metrics.promptTokens,
|
|
24
|
+
outputTokens: metrics.completionTokens,
|
|
25
|
+
tokensPerSecond: metrics.tokensPerSecond,
|
|
26
|
+
})
|
|
27
|
+
.info('Pipeline LLM metrics');
|
|
28
|
+
} else if (isLLMMetrics(metrics)) {
|
|
29
|
+
logger
|
|
30
|
+
.child({
|
|
31
|
+
ttft: metrics.ttft,
|
|
32
|
+
inputTokens: metrics.promptTokens,
|
|
33
|
+
outputTokens: metrics.completionTokens,
|
|
34
|
+
tokensPerSecond: metrics.tokensPerSecond,
|
|
35
|
+
})
|
|
36
|
+
.info('LLM metrics');
|
|
37
|
+
} else if (isPipelineTTSMetrics(metrics)) {
|
|
38
|
+
logger
|
|
39
|
+
.child({
|
|
40
|
+
sequenceId: metrics.sequenceId,
|
|
41
|
+
ttfb: metrics.ttfb,
|
|
42
|
+
audioDuration: metrics.audioDuration,
|
|
43
|
+
})
|
|
44
|
+
.info('Pipeline TTS metrics');
|
|
45
|
+
} else if (isTTSMetrics(metrics)) {
|
|
46
|
+
logger
|
|
47
|
+
.child({
|
|
48
|
+
ttfb: metrics.ttfb,
|
|
49
|
+
audioDuration: metrics.audioDuration,
|
|
50
|
+
})
|
|
51
|
+
.info('TTS metrics');
|
|
52
|
+
} else if (isPipelineEOUMetrics(metrics)) {
|
|
53
|
+
logger
|
|
54
|
+
.child({
|
|
55
|
+
sequenceId: metrics.sequenceId,
|
|
56
|
+
endOfUtteranceDelay: metrics.endOfUtteranceDelay,
|
|
57
|
+
transcriptionDelay: metrics.transcriptionDelay,
|
|
58
|
+
})
|
|
59
|
+
.info('Pipeline EOU metrics');
|
|
60
|
+
} else if (isSTTMetrics(metrics)) {
|
|
61
|
+
logger
|
|
62
|
+
.child({
|
|
63
|
+
audioDuration: metrics.audioDuration,
|
|
64
|
+
})
|
|
65
|
+
.info('STT metrics');
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const isLLMMetrics = (metrics: AgentMetrics): metrics is LLMMetrics => {
|
|
70
|
+
return !!(metrics as LLMMetrics).ttft;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const isPipelineLLMMetrics = (metrics: AgentMetrics): metrics is PipelineLLMMetrics => {
|
|
74
|
+
return isLLMMetrics(metrics) && !!(metrics as PipelineLLMMetrics).sequenceId;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const isVADMetrics = (metrics: AgentMetrics): metrics is VADMetrics => {
|
|
78
|
+
return !!(metrics as VADMetrics).inferenceCount;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const isPipelineEOUMetrics = (metrics: AgentMetrics): metrics is PipelineEOUMetrics => {
|
|
82
|
+
return !!(metrics as PipelineEOUMetrics).endOfUtteranceDelay;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const isTTSMetrics = (metrics: AgentMetrics): metrics is TTSMetrics => {
|
|
86
|
+
return !!(metrics as TTSMetrics).ttfb;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const isPipelineTTSMetrics = (metrics: AgentMetrics): metrics is PipelineTTSMetrics => {
|
|
90
|
+
return isTTSMetrics(metrics) && !!(metrics as PipelineTTSMetrics).sequenceId;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const isSTTMetrics = (metrics: AgentMetrics): metrics is STTMetrics => {
|
|
94
|
+
return !(
|
|
95
|
+
isLLMMetrics(metrics) ||
|
|
96
|
+
isVADMetrics(metrics) ||
|
|
97
|
+
isPipelineEOUMetrics(metrics) ||
|
|
98
|
+
isTTSMetrics(metrics)
|
|
99
|
+
);
|
|
100
|
+
};
|
|
@@ -21,6 +21,7 @@ import { EventEmitter } from 'node:events';
|
|
|
21
21
|
import { AudioByteStream } from '../audio.js';
|
|
22
22
|
import * as llm from '../llm/index.js';
|
|
23
23
|
import { log } from '../log.js';
|
|
24
|
+
import type { MultimodalLLMMetrics } from '../metrics/base.js';
|
|
24
25
|
import { BasicTranscriptionForwarder } from '../transcription.js';
|
|
25
26
|
import { findMicroTrackId } from '../utils.js';
|
|
26
27
|
import { AgentPlayout, type PlayoutHandle } from './agent_playout.js';
|
|
@@ -60,7 +61,7 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
60
61
|
room: Room | null = null;
|
|
61
62
|
linkedParticipant: RemoteParticipant | null = null;
|
|
62
63
|
subscribedTrack: RemoteAudioTrack | null = null;
|
|
63
|
-
readMicroTask:
|
|
64
|
+
readMicroTask: Promise<void> | null = null;
|
|
64
65
|
|
|
65
66
|
constructor({
|
|
66
67
|
model,
|
|
@@ -284,6 +285,7 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
284
285
|
});
|
|
285
286
|
|
|
286
287
|
this.#session.on('input_speech_started', (ev: any) => {
|
|
288
|
+
this.emit('user_started_speaking');
|
|
287
289
|
if (this.#playingHandle && !this.#playingHandle.done) {
|
|
288
290
|
this.#playingHandle.interrupt();
|
|
289
291
|
|
|
@@ -326,6 +328,10 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
326
328
|
this.#updateState();
|
|
327
329
|
});
|
|
328
330
|
|
|
331
|
+
this.#session.on('metrics_collected', (metrics: MultimodalLLMMetrics) => {
|
|
332
|
+
this.emit('metrics_collected', metrics);
|
|
333
|
+
});
|
|
334
|
+
|
|
329
335
|
resolve(this.#session);
|
|
330
336
|
});
|
|
331
337
|
}
|
|
@@ -404,22 +410,11 @@ export class MultimodalAgent extends EventEmitter {
|
|
|
404
410
|
};
|
|
405
411
|
this.subscribedTrack = track;
|
|
406
412
|
|
|
407
|
-
|
|
408
|
-
this.
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
this.readMicroTask = {
|
|
413
|
-
promise: new Promise<void>((resolve, reject) => {
|
|
414
|
-
cancel = () => {
|
|
415
|
-
reject(new Error('Task cancelled'));
|
|
416
|
-
};
|
|
417
|
-
readAudioStreamTask(new AudioStream(track, this.model.sampleRate, this.model.numChannels))
|
|
418
|
-
.then(resolve)
|
|
419
|
-
.catch(reject);
|
|
420
|
-
}),
|
|
421
|
-
cancel: () => cancel(),
|
|
422
|
-
};
|
|
413
|
+
this.readMicroTask = new Promise<void>((resolve, reject) => {
|
|
414
|
+
readAudioStreamTask(new AudioStream(track, this.model.sampleRate, this.model.numChannels))
|
|
415
|
+
.then(resolve)
|
|
416
|
+
.catch(reject);
|
|
417
|
+
});
|
|
423
418
|
}
|
|
424
419
|
|
|
425
420
|
#getLocalTrackSid(): string | null {
|
package/src/pipeline/index.ts
CHANGED