speechflow 1.7.1 → 2.0.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/CHANGELOG.md +24 -0
- package/README.md +388 -120
- package/etc/claude.md +5 -5
- package/etc/speechflow.yaml +2 -2
- package/package.json +3 -3
- package/speechflow-cli/dst/speechflow-main-api.js.map +1 -1
- package/speechflow-cli/dst/speechflow-main-cli.js +1 -0
- package/speechflow-cli/dst/speechflow-main-cli.js.map +1 -1
- package/speechflow-cli/dst/speechflow-main-graph.d.ts +1 -0
- package/speechflow-cli/dst/speechflow-main-graph.js +30 -9
- package/speechflow-cli/dst/speechflow-main-graph.js.map +1 -1
- package/speechflow-cli/dst/speechflow-main-nodes.js +1 -0
- package/speechflow-cli/dst/speechflow-main-nodes.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-compressor-wt.js +1 -0
- package/speechflow-cli/dst/speechflow-node-a2a-compressor-wt.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-compressor.js +7 -9
- package/speechflow-cli/dst/speechflow-node-a2a-compressor.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-expander-wt.js +1 -0
- package/speechflow-cli/dst/speechflow-node-a2a-expander-wt.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-expander.js +8 -9
- package/speechflow-cli/dst/speechflow-node-a2a-expander.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-filler.js +2 -0
- package/speechflow-cli/dst/speechflow-node-a2a-filler.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-gender.js +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-gender.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-meter.js +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-pitch.js +11 -9
- package/speechflow-cli/dst/speechflow-node-a2a-pitch.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-rnnoise-wt.js +1 -0
- package/speechflow-cli/dst/speechflow-node-a2a-rnnoise-wt.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-rnnoise.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-speex.js +4 -2
- package/speechflow-cli/dst/speechflow-node-a2a-speex.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-vad.js +19 -22
- package/speechflow-cli/dst/speechflow-node-a2a-vad.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2a-wav.js +31 -4
- package/speechflow-cli/dst/speechflow-node-a2a-wav.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2t-amazon.d.ts +0 -1
- package/speechflow-cli/dst/speechflow-node-a2t-amazon.js +2 -11
- package/speechflow-cli/dst/speechflow-node-a2t-amazon.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-a2t-google.d.ts +16 -0
- package/speechflow-cli/dst/speechflow-node-a2t-google.js +314 -0
- package/speechflow-cli/dst/speechflow-node-a2t-google.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-a2t-openai.js +6 -1
- package/speechflow-cli/dst/speechflow-node-a2t-openai.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-t2a-amazon.d.ts +1 -1
- package/speechflow-cli/dst/speechflow-node-t2a-amazon.js +27 -7
- package/speechflow-cli/dst/speechflow-node-t2a-amazon.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-t2a-elevenlabs.d.ts +1 -1
- package/speechflow-cli/dst/speechflow-node-t2a-elevenlabs.js +5 -3
- package/speechflow-cli/dst/speechflow-node-t2a-elevenlabs.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-t2a-google.d.ts +15 -0
- package/speechflow-cli/dst/speechflow-node-t2a-google.js +215 -0
- package/speechflow-cli/dst/speechflow-node-t2a-google.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-t2a-kokoro.d.ts +1 -1
- package/speechflow-cli/dst/speechflow-node-t2a-kokoro.js +27 -6
- package/speechflow-cli/dst/speechflow-node-t2a-kokoro.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-t2a-openai.d.ts +15 -0
- package/speechflow-cli/dst/speechflow-node-t2a-openai.js +192 -0
- package/speechflow-cli/dst/speechflow-node-t2a-openai.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-t2a-supertonic.d.ts +17 -0
- package/speechflow-cli/dst/speechflow-node-t2a-supertonic.js +619 -0
- package/speechflow-cli/dst/speechflow-node-t2a-supertonic.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-t2t-amazon.js +0 -2
- package/speechflow-cli/dst/speechflow-node-t2t-amazon.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-t2t-deepl.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-t2t-google.js.map +1 -1
- package/speechflow-cli/dst/{speechflow-node-t2t-transformers.d.ts → speechflow-node-t2t-opus.d.ts} +1 -3
- package/speechflow-cli/dst/speechflow-node-t2t-opus.js +161 -0
- package/speechflow-cli/dst/speechflow-node-t2t-opus.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-t2t-profanity.d.ts +11 -0
- package/speechflow-cli/dst/speechflow-node-t2t-profanity.js +118 -0
- package/speechflow-cli/dst/speechflow-node-t2t-profanity.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-t2t-punctuation.d.ts +13 -0
- package/speechflow-cli/dst/speechflow-node-t2t-punctuation.js +220 -0
- package/speechflow-cli/dst/speechflow-node-t2t-punctuation.js.map +1 -0
- package/speechflow-cli/dst/{speechflow-node-t2t-openai.d.ts → speechflow-node-t2t-spellcheck.d.ts} +2 -2
- package/speechflow-cli/dst/{speechflow-node-t2t-openai.js → speechflow-node-t2t-spellcheck.js} +48 -100
- package/speechflow-cli/dst/speechflow-node-t2t-spellcheck.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-t2t-subtitle.js +8 -8
- package/speechflow-cli/dst/speechflow-node-t2t-subtitle.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-t2t-summary.d.ts +16 -0
- package/speechflow-cli/dst/speechflow-node-t2t-summary.js +241 -0
- package/speechflow-cli/dst/speechflow-node-t2t-summary.js.map +1 -0
- package/speechflow-cli/dst/{speechflow-node-t2t-ollama.d.ts → speechflow-node-t2t-translate.d.ts} +2 -2
- package/speechflow-cli/dst/{speechflow-node-t2t-transformers.js → speechflow-node-t2t-translate.js} +53 -115
- package/speechflow-cli/dst/speechflow-node-t2t-translate.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-x2x-filter.js +2 -0
- package/speechflow-cli/dst/speechflow-node-x2x-filter.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-xio-exec.d.ts +12 -0
- package/speechflow-cli/dst/speechflow-node-xio-exec.js +224 -0
- package/speechflow-cli/dst/speechflow-node-xio-exec.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-xio-file.d.ts +1 -0
- package/speechflow-cli/dst/speechflow-node-xio-file.js +78 -67
- package/speechflow-cli/dst/speechflow-node-xio-file.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-xio-mqtt.js.map +1 -1
- package/speechflow-cli/dst/speechflow-node-xio-vban.d.ts +17 -0
- package/speechflow-cli/dst/speechflow-node-xio-vban.js +330 -0
- package/speechflow-cli/dst/speechflow-node-xio-vban.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-xio-webrtc.d.ts +39 -0
- package/speechflow-cli/dst/speechflow-node-xio-webrtc.js +502 -0
- package/speechflow-cli/dst/speechflow-node-xio-webrtc.js.map +1 -0
- package/speechflow-cli/dst/speechflow-node-xio-websocket.js +9 -9
- package/speechflow-cli/dst/speechflow-node-xio-websocket.js.map +1 -1
- package/speechflow-cli/dst/speechflow-util-audio.js +8 -5
- package/speechflow-cli/dst/speechflow-util-audio.js.map +1 -1
- package/speechflow-cli/dst/speechflow-util-error.d.ts +1 -0
- package/speechflow-cli/dst/speechflow-util-error.js +5 -0
- package/speechflow-cli/dst/speechflow-util-error.js.map +1 -1
- package/speechflow-cli/dst/speechflow-util-llm.d.ts +35 -0
- package/speechflow-cli/dst/speechflow-util-llm.js +363 -0
- package/speechflow-cli/dst/speechflow-util-llm.js.map +1 -0
- package/speechflow-cli/dst/speechflow-util-queue.js +2 -1
- package/speechflow-cli/dst/speechflow-util-queue.js.map +1 -1
- package/speechflow-cli/dst/speechflow-util.d.ts +1 -0
- package/speechflow-cli/dst/speechflow-util.js +2 -0
- package/speechflow-cli/dst/speechflow-util.js.map +1 -1
- package/speechflow-cli/etc/oxlint.jsonc +2 -1
- package/speechflow-cli/package.json +35 -18
- package/speechflow-cli/src/lib.d.ts +5 -0
- package/speechflow-cli/src/speechflow-main-api.ts +16 -16
- package/speechflow-cli/src/speechflow-main-cli.ts +1 -0
- package/speechflow-cli/src/speechflow-main-graph.ts +38 -14
- package/speechflow-cli/src/speechflow-main-nodes.ts +1 -0
- package/speechflow-cli/src/speechflow-node-a2a-compressor-wt.ts +1 -0
- package/speechflow-cli/src/speechflow-node-a2a-compressor.ts +8 -10
- package/speechflow-cli/src/speechflow-node-a2a-expander-wt.ts +1 -0
- package/speechflow-cli/src/speechflow-node-a2a-expander.ts +9 -10
- package/speechflow-cli/src/speechflow-node-a2a-filler.ts +2 -0
- package/speechflow-cli/src/speechflow-node-a2a-gender.ts +3 -3
- package/speechflow-cli/src/speechflow-node-a2a-meter.ts +2 -2
- package/speechflow-cli/src/speechflow-node-a2a-pitch.ts +11 -9
- package/speechflow-cli/src/speechflow-node-a2a-rnnoise-wt.ts +1 -0
- package/speechflow-cli/src/speechflow-node-a2a-rnnoise.ts +1 -1
- package/speechflow-cli/src/speechflow-node-a2a-speex.ts +5 -3
- package/speechflow-cli/src/speechflow-node-a2a-vad.ts +20 -23
- package/speechflow-cli/src/speechflow-node-a2a-wav.ts +31 -4
- package/speechflow-cli/src/speechflow-node-a2t-amazon.ts +6 -18
- package/speechflow-cli/src/speechflow-node-a2t-google.ts +315 -0
- package/speechflow-cli/src/speechflow-node-a2t-openai.ts +12 -7
- package/speechflow-cli/src/speechflow-node-t2a-amazon.ts +32 -10
- package/speechflow-cli/src/speechflow-node-t2a-elevenlabs.ts +6 -4
- package/speechflow-cli/src/speechflow-node-t2a-google.ts +203 -0
- package/speechflow-cli/src/speechflow-node-t2a-kokoro.ts +33 -10
- package/speechflow-cli/src/speechflow-node-t2a-openai.ts +176 -0
- package/speechflow-cli/src/speechflow-node-t2a-supertonic.ts +710 -0
- package/speechflow-cli/src/speechflow-node-t2t-amazon.ts +3 -4
- package/speechflow-cli/src/speechflow-node-t2t-deepl.ts +2 -2
- package/speechflow-cli/src/speechflow-node-t2t-google.ts +1 -1
- package/speechflow-cli/src/speechflow-node-t2t-opus.ts +137 -0
- package/speechflow-cli/src/speechflow-node-t2t-profanity.ts +93 -0
- package/speechflow-cli/src/speechflow-node-t2t-punctuation.ts +201 -0
- package/speechflow-cli/src/speechflow-node-t2t-spellcheck.ts +188 -0
- package/speechflow-cli/src/speechflow-node-t2t-subtitle.ts +8 -8
- package/speechflow-cli/src/speechflow-node-t2t-summary.ts +229 -0
- package/speechflow-cli/src/speechflow-node-t2t-translate.ts +181 -0
- package/speechflow-cli/src/speechflow-node-x2x-filter.ts +2 -0
- package/speechflow-cli/src/speechflow-node-xio-exec.ts +211 -0
- package/speechflow-cli/src/speechflow-node-xio-file.ts +91 -80
- package/speechflow-cli/src/speechflow-node-xio-mqtt.ts +2 -2
- package/speechflow-cli/src/speechflow-node-xio-vban.ts +325 -0
- package/speechflow-cli/src/speechflow-node-xio-webrtc.ts +535 -0
- package/speechflow-cli/src/speechflow-node-xio-websocket.ts +9 -9
- package/speechflow-cli/src/speechflow-util-audio.ts +10 -5
- package/speechflow-cli/src/speechflow-util-error.ts +9 -0
- package/speechflow-cli/src/speechflow-util-llm.ts +367 -0
- package/speechflow-cli/src/speechflow-util-queue.ts +3 -3
- package/speechflow-cli/src/speechflow-util.ts +2 -0
- package/speechflow-ui-db/package.json +9 -9
- package/speechflow-ui-st/package.json +9 -9
- package/speechflow-cli/dst/speechflow-node-t2t-ollama.js +0 -293
- package/speechflow-cli/dst/speechflow-node-t2t-ollama.js.map +0 -1
- package/speechflow-cli/dst/speechflow-node-t2t-openai.js.map +0 -1
- package/speechflow-cli/dst/speechflow-node-t2t-transformers.js.map +0 -1
- package/speechflow-cli/src/speechflow-node-t2t-ollama.ts +0 -281
- package/speechflow-cli/src/speechflow-node-t2t-openai.ts +0 -247
- package/speechflow-cli/src/speechflow-node-t2t-transformers.ts +0 -247
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
/* standard dependencies */
|
|
8
|
-
import Stream
|
|
8
|
+
import Stream from "node:stream"
|
|
9
9
|
|
|
10
10
|
/* external dependencies */
|
|
11
11
|
import { TranslateClient, TranslateTextCommand } from "@aws-sdk/client-translate"
|
|
@@ -65,8 +65,6 @@ export default class SpeechFlowNodeT2TAmazon extends SpeechFlowNode {
|
|
|
65
65
|
secretAccessKey: this.params.secKey
|
|
66
66
|
}
|
|
67
67
|
})
|
|
68
|
-
if (this.client === null)
|
|
69
|
-
throw new Error("failed to establish Amazon Translate client")
|
|
70
68
|
|
|
71
69
|
/* provide text-to-text translation */
|
|
72
70
|
const maxRetries = 10
|
|
@@ -86,7 +84,8 @@ export default class SpeechFlowNodeT2TAmazon extends SpeechFlowNode {
|
|
|
86
84
|
})
|
|
87
85
|
const out = await this.client!.send(cmd)
|
|
88
86
|
return (out.TranslatedText ?? "").trim()
|
|
89
|
-
}
|
|
87
|
+
}
|
|
88
|
+
catch (e: any) {
|
|
90
89
|
lastError = e
|
|
91
90
|
attempt += 1
|
|
92
91
|
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
/* standard dependencies */
|
|
8
|
-
import Stream
|
|
8
|
+
import Stream from "node:stream"
|
|
9
9
|
|
|
10
10
|
/* external dependencies */
|
|
11
|
-
import * as DeepL
|
|
11
|
+
import * as DeepL from "deepl-node"
|
|
12
12
|
|
|
13
13
|
/* internal dependencies */
|
|
14
14
|
import SpeechFlowNode, { SpeechFlowChunk } from "./speechflow-node"
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** SpeechFlow - Speech Processing Flow Graph
|
|
3
|
+
** Copyright (c) 2024-2025 Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
4
|
+
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/* standard dependencies */
|
|
8
|
+
import path from "node:path"
|
|
9
|
+
import Stream from "node:stream"
|
|
10
|
+
|
|
11
|
+
/* external dependencies */
|
|
12
|
+
import * as Transformers from "@huggingface/transformers"
|
|
13
|
+
|
|
14
|
+
/* internal dependencies */
|
|
15
|
+
import SpeechFlowNode, { SpeechFlowChunk } from "./speechflow-node"
|
|
16
|
+
import * as util from "./speechflow-util"
|
|
17
|
+
|
|
18
|
+
/* SpeechFlow node for OPUS text-to-text translation */
|
|
19
|
+
export default class SpeechFlowNodeT2TOPUS extends SpeechFlowNode {
|
|
20
|
+
/* declare official node name */
|
|
21
|
+
public static name = "t2t-opus"
|
|
22
|
+
|
|
23
|
+
/* internal state */
|
|
24
|
+
private translator: Transformers.TranslationPipeline | null = null
|
|
25
|
+
|
|
26
|
+
/* construct node */
|
|
27
|
+
constructor (id: string, cfg: { [ id: string ]: any }, opts: { [ id: string ]: any }, args: any[]) {
|
|
28
|
+
super(id, cfg, opts, args)
|
|
29
|
+
|
|
30
|
+
/* declare node configuration parameters */
|
|
31
|
+
this.configure({
|
|
32
|
+
src: { type: "string", pos: 0, val: "de", match: /^(?:de|en)$/ },
|
|
33
|
+
dst: { type: "string", pos: 1, val: "en", match: /^(?:de|en)$/ }
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
/* sanity check parameters */
|
|
37
|
+
if (this.params.src === this.params.dst)
|
|
38
|
+
throw new Error("source and destination languages cannot be the same")
|
|
39
|
+
|
|
40
|
+
/* declare node input/output format */
|
|
41
|
+
this.input = "text"
|
|
42
|
+
this.output = "text"
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/* open node */
|
|
46
|
+
async open () {
|
|
47
|
+
/* track download progress when instantiating Transformers engine and model */
|
|
48
|
+
const model = `onnx-community/opus-mt-${this.params.src}-${this.params.dst}`
|
|
49
|
+
const progressState = new Map<string, number>()
|
|
50
|
+
const progressCallback: Transformers.ProgressCallback = (progress: any) => {
|
|
51
|
+
let artifact = model
|
|
52
|
+
if (typeof progress.file === "string")
|
|
53
|
+
artifact += `:${progress.file}`
|
|
54
|
+
let percent = 0
|
|
55
|
+
if (typeof progress.loaded === "number" && typeof progress.total === "number")
|
|
56
|
+
percent = (progress.loaded / progress.total) * 100
|
|
57
|
+
else if (typeof progress.progress === "number")
|
|
58
|
+
percent = progress.progress
|
|
59
|
+
if (percent > 0)
|
|
60
|
+
progressState.set(artifact, percent)
|
|
61
|
+
}
|
|
62
|
+
const interval = setInterval(() => {
|
|
63
|
+
for (const [ artifact, percent ] of progressState) {
|
|
64
|
+
this.log("info", `downloaded ${percent.toFixed(2)}% of artifact "${artifact}"`)
|
|
65
|
+
if (percent >= 100.0)
|
|
66
|
+
progressState.delete(artifact)
|
|
67
|
+
}
|
|
68
|
+
}, 1000)
|
|
69
|
+
|
|
70
|
+
/* instantiate Transformers engine and model */
|
|
71
|
+
try {
|
|
72
|
+
const pipeline = Transformers.pipeline("translation", model, {
|
|
73
|
+
cache_dir: path.join(this.config.cacheDir, "transformers"),
|
|
74
|
+
dtype: "q4",
|
|
75
|
+
device: "auto",
|
|
76
|
+
progress_callback: progressCallback
|
|
77
|
+
})
|
|
78
|
+
this.translator = await pipeline
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
/* clear progress interval again */
|
|
82
|
+
clearInterval(interval)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* provide text-to-text translation */
|
|
86
|
+
const translate = async (text: string) => {
|
|
87
|
+
const result = await this.translator!(text)
|
|
88
|
+
const single = Array.isArray(result) ? result[0] : result
|
|
89
|
+
return (single as Transformers.TranslationSingle).translation_text
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/* establish a duplex stream and connect it to Transformers */
|
|
93
|
+
this.stream = new Stream.Transform({
|
|
94
|
+
readableObjectMode: true,
|
|
95
|
+
writableObjectMode: true,
|
|
96
|
+
decodeStrings: false,
|
|
97
|
+
highWaterMark: 1,
|
|
98
|
+
transform (chunk: SpeechFlowChunk, encoding, callback) {
|
|
99
|
+
if (Buffer.isBuffer(chunk.payload))
|
|
100
|
+
callback(new Error("invalid chunk payload type"))
|
|
101
|
+
else if (chunk.payload === "") {
|
|
102
|
+
this.push(chunk)
|
|
103
|
+
callback()
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
translate(chunk.payload).then((payload) => {
|
|
107
|
+
const chunkNew = chunk.clone()
|
|
108
|
+
chunkNew.payload = payload
|
|
109
|
+
this.push(chunkNew)
|
|
110
|
+
callback()
|
|
111
|
+
}).catch((error: unknown) => {
|
|
112
|
+
callback(util.ensureError(error))
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
final (callback) {
|
|
117
|
+
callback()
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* close node */
|
|
123
|
+
async close () {
|
|
124
|
+
/* shutdown Transformers */
|
|
125
|
+
if (this.translator !== null) {
|
|
126
|
+
this.translator.dispose()
|
|
127
|
+
this.translator = null
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* shutdown stream */
|
|
131
|
+
if (this.stream !== null) {
|
|
132
|
+
await util.destroyStream(this.stream)
|
|
133
|
+
this.stream = null
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** SpeechFlow - Speech Processing Flow Graph
|
|
3
|
+
** Copyright (c) 2024-2025 Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
4
|
+
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/* standard dependencies */
|
|
8
|
+
import Stream from "node:stream"
|
|
9
|
+
|
|
10
|
+
/* external dependencies */
|
|
11
|
+
import BadWordsNext from "bad-words-next"
|
|
12
|
+
import en from "bad-words-next/lib/en"
|
|
13
|
+
import de from "bad-words-next/lib/de"
|
|
14
|
+
|
|
15
|
+
/* internal dependencies */
|
|
16
|
+
import SpeechFlowNode, { SpeechFlowChunk } from "./speechflow-node"
|
|
17
|
+
import * as util from "./speechflow-util"
|
|
18
|
+
|
|
19
|
+
/* language data mapping */
|
|
20
|
+
const langData: { [ lang: string ]: typeof en } = { en, de }
|
|
21
|
+
|
|
22
|
+
/* SpeechFlow node for text-to-text profanity filtering */
|
|
23
|
+
export default class SpeechFlowNodeT2TProfanity extends SpeechFlowNode {
|
|
24
|
+
/* declare official node name */
|
|
25
|
+
public static name = "t2t-profanity"
|
|
26
|
+
|
|
27
|
+
/* construct node */
|
|
28
|
+
constructor (id: string, cfg: { [ id: string ]: any }, opts: { [ id: string ]: any }, args: any[]) {
|
|
29
|
+
super(id, cfg, opts, args)
|
|
30
|
+
|
|
31
|
+
/* declare node configuration parameters */
|
|
32
|
+
this.configure({
|
|
33
|
+
lang: { type: "string", val: "en", match: /^(?:en|de)$/ },
|
|
34
|
+
placeholder: { type: "string", val: "***" },
|
|
35
|
+
mode: { type: "string", val: "replace", match: /^(?:replace|repeat)$/ }
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
/* declare node input/output format */
|
|
39
|
+
this.input = "text"
|
|
40
|
+
this.output = "text"
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/* open node */
|
|
44
|
+
async open () {
|
|
45
|
+
/* create profanity filter instance */
|
|
46
|
+
const filter = util.run("creating profanity filter", () =>
|
|
47
|
+
new BadWordsNext({
|
|
48
|
+
data: langData[this.params.lang],
|
|
49
|
+
placeholder: this.params.placeholder,
|
|
50
|
+
placeholderMode: this.params.mode as "replace" | "repeat"
|
|
51
|
+
})
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
/* apply profanity filtering */
|
|
55
|
+
const censor = (text: string): string =>
|
|
56
|
+
filter.filter(text)
|
|
57
|
+
|
|
58
|
+
/* establish a transform stream and connect it to profanity filtering */
|
|
59
|
+
this.stream = new Stream.Transform({
|
|
60
|
+
readableObjectMode: true,
|
|
61
|
+
writableObjectMode: true,
|
|
62
|
+
decodeStrings: false,
|
|
63
|
+
highWaterMark: 1,
|
|
64
|
+
transform (chunk: SpeechFlowChunk, encoding, callback) {
|
|
65
|
+
if (Buffer.isBuffer(chunk.payload))
|
|
66
|
+
callback(new Error("invalid chunk payload type"))
|
|
67
|
+
else if (chunk.payload === "") {
|
|
68
|
+
this.push(chunk)
|
|
69
|
+
callback()
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
const payload = censor(chunk.payload)
|
|
73
|
+
const chunkNew = chunk.clone()
|
|
74
|
+
chunkNew.payload = payload
|
|
75
|
+
this.push(chunkNew)
|
|
76
|
+
callback()
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
final (callback) {
|
|
80
|
+
callback()
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* close node */
|
|
86
|
+
async close () {
|
|
87
|
+
/* shutdown stream */
|
|
88
|
+
if (this.stream !== null) {
|
|
89
|
+
await util.destroyStream(this.stream)
|
|
90
|
+
this.stream = null
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** SpeechFlow - Speech Processing Flow Graph
|
|
3
|
+
** Copyright (c) 2024-2025 Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
4
|
+
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/* standard dependencies */
|
|
8
|
+
import Stream from "node:stream"
|
|
9
|
+
|
|
10
|
+
/* internal dependencies */
|
|
11
|
+
import SpeechFlowNode, { SpeechFlowChunk } from "./speechflow-node"
|
|
12
|
+
import * as util from "./speechflow-util"
|
|
13
|
+
import { LLM, type LLMCompleteMessage } from "./speechflow-util-llm"
|
|
14
|
+
|
|
15
|
+
/* internal utility types */
|
|
16
|
+
type ConfigEntry = { systemPrompt: string, chat: LLMCompleteMessage[] }
|
|
17
|
+
type Config = { [ key: string ]: ConfigEntry }
|
|
18
|
+
|
|
19
|
+
/* SpeechFlow node for text-to-text punctuation restoration */
|
|
20
|
+
export default class SpeechFlowNodeT2TPunctuation extends SpeechFlowNode {
|
|
21
|
+
/* declare official node name */
|
|
22
|
+
public static name = "t2t-punctuation"
|
|
23
|
+
|
|
24
|
+
/* internal state */
|
|
25
|
+
private llm: LLM | null = null
|
|
26
|
+
|
|
27
|
+
/* internal LLM setup */
|
|
28
|
+
private setup: Config = {
|
|
29
|
+
/* English (EN) punctuation restoration */
|
|
30
|
+
"en": {
|
|
31
|
+
systemPrompt:
|
|
32
|
+
"You are a punctuation restoration specialist for English.\n" +
|
|
33
|
+
"Your task is to add missing punctuation to unpunctuated text.\n" +
|
|
34
|
+
"Output only the punctuated text.\n" +
|
|
35
|
+
"Do NOT use markdown.\n" +
|
|
36
|
+
"Do NOT give any explanations.\n" +
|
|
37
|
+
"Do NOT give any introduction.\n" +
|
|
38
|
+
"Do NOT give any comments.\n" +
|
|
39
|
+
"Do NOT give any preamble.\n" +
|
|
40
|
+
"Do NOT give any prolog.\n" +
|
|
41
|
+
"Do NOT give any epilog.\n" +
|
|
42
|
+
"Do NOT change the words.\n" +
|
|
43
|
+
"Do NOT add or remove words.\n" +
|
|
44
|
+
"Do NOT fix spelling errors.\n" +
|
|
45
|
+
"Do NOT change the grammar.\n" +
|
|
46
|
+
"Do NOT use synonyms.\n" +
|
|
47
|
+
"Keep all original words exactly as they are.\n" +
|
|
48
|
+
"Add periods at sentence endings.\n" +
|
|
49
|
+
"Add commas where appropriate.\n" +
|
|
50
|
+
"Add question marks for questions.\n" +
|
|
51
|
+
"Add exclamation marks where appropriate.\n" +
|
|
52
|
+
"Add colons and semicolons where appropriate.\n" +
|
|
53
|
+
"Capitalize first letters of sentences.\n" +
|
|
54
|
+
"The text you have to punctuate is:\n",
|
|
55
|
+
chat: [
|
|
56
|
+
{ role: "user", content: "hello how are you today" },
|
|
57
|
+
{ role: "assistant", content: "Hello, how are you today?" },
|
|
58
|
+
{ role: "user", content: "i went to the store and bought some milk eggs and bread" },
|
|
59
|
+
{ role: "assistant", content: "I went to the store and bought some milk, eggs, and bread." },
|
|
60
|
+
{ role: "user", content: "what time is it i need to leave soon" },
|
|
61
|
+
{ role: "assistant", content: "What time is it? I need to leave soon." },
|
|
62
|
+
{ role: "user", content: "thats amazing i cant believe it worked" },
|
|
63
|
+
{ role: "assistant", content: "That's amazing! I can't believe it worked!" }
|
|
64
|
+
]
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
/* German (DE) punctuation restoration */
|
|
68
|
+
"de": {
|
|
69
|
+
systemPrompt:
|
|
70
|
+
"Du bist ein Spezialist für Zeichensetzung im Deutschen.\n" +
|
|
71
|
+
"Deine Aufgabe ist es, fehlende Satzzeichen in unpunktierten Text einzufügen.\n" +
|
|
72
|
+
"Gib nur den punktierten Text aus.\n" +
|
|
73
|
+
"Benutze KEIN Markdown.\n" +
|
|
74
|
+
"Gib KEINE Erklärungen.\n" +
|
|
75
|
+
"Gib KEINE Einleitung.\n" +
|
|
76
|
+
"Gib KEINE Kommentare.\n" +
|
|
77
|
+
"Gib KEINE Präambel.\n" +
|
|
78
|
+
"Gib KEINEN Prolog.\n" +
|
|
79
|
+
"Gib KEINEN Epilog.\n" +
|
|
80
|
+
"Ändere NICHT die Wörter.\n" +
|
|
81
|
+
"Füge KEINE Wörter hinzu oder entferne welche.\n" +
|
|
82
|
+
"Korrigiere KEINE Rechtschreibfehler.\n" +
|
|
83
|
+
"Ändere NICHT die Grammatik.\n" +
|
|
84
|
+
"Verwende KEINE Synonyme.\n" +
|
|
85
|
+
"Behalte alle ursprünglichen Wörter genau bei.\n" +
|
|
86
|
+
"Füge Punkte am Satzende ein.\n" +
|
|
87
|
+
"Füge Kommas an passenden Stellen ein.\n" +
|
|
88
|
+
"Füge Fragezeichen bei Fragen ein.\n" +
|
|
89
|
+
"Füge Ausrufezeichen an passenden Stellen ein.\n" +
|
|
90
|
+
"Füge Doppelpunkte und Semikolons an passenden Stellen ein.\n" +
|
|
91
|
+
"Großschreibe die ersten Buchstaben von Sätzen.\n" +
|
|
92
|
+
"Der von dir zu punktierende Text ist:\n",
|
|
93
|
+
chat: [
|
|
94
|
+
{ role: "user", content: "hallo wie geht es dir heute" },
|
|
95
|
+
{ role: "assistant", content: "Hallo, wie geht es dir heute?" },
|
|
96
|
+
{ role: "user", content: "ich bin in den laden gegangen und habe milch eier und brot gekauft" },
|
|
97
|
+
{ role: "assistant", content: "Ich bin in den Laden gegangen und habe Milch, Eier und Brot gekauft." },
|
|
98
|
+
{ role: "user", content: "wie spät ist es ich muss bald los" },
|
|
99
|
+
{ role: "assistant", content: "Wie spät ist es? Ich muss bald los." },
|
|
100
|
+
{ role: "user", content: "das ist fantastisch ich kann nicht glauben dass es funktioniert hat" },
|
|
101
|
+
{ role: "assistant", content: "Das ist fantastisch! Ich kann nicht glauben, dass es funktioniert hat!" }
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/* construct node */
|
|
107
|
+
constructor (id: string, cfg: { [ id: string ]: any }, opts: { [ id: string ]: any }, args: any[]) {
|
|
108
|
+
super(id, cfg, opts, args)
|
|
109
|
+
|
|
110
|
+
/* declare node configuration parameters */
|
|
111
|
+
this.configure({
|
|
112
|
+
provider: { type: "string", val: "ollama", match: /^(?:openai|anthropic|google|ollama|transformers)$/ },
|
|
113
|
+
api: { type: "string", val: "http://127.0.0.1:11434", match: /^https?:\/\/.+?(:\d+)?$/ },
|
|
114
|
+
model: { type: "string", val: "gemma3:4b-it-q4_K_M", match: /^.+$/ },
|
|
115
|
+
key: { type: "string", val: "", match: /^.*$/ },
|
|
116
|
+
lang: { type: "string", pos: 0, val: "en", match: /^(?:de|en)$/ }
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
/* tell effective mode */
|
|
120
|
+
this.log("info", `punctuation restoration for language "${this.params.lang}" ` +
|
|
121
|
+
`via ${this.params.provider} LLM (model: ${this.params.model})`)
|
|
122
|
+
|
|
123
|
+
/* declare node input/output format */
|
|
124
|
+
this.input = "text"
|
|
125
|
+
this.output = "text"
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/* open node */
|
|
129
|
+
async open () {
|
|
130
|
+
/* instantiate LLM */
|
|
131
|
+
this.llm = new LLM({
|
|
132
|
+
provider: this.params.provider,
|
|
133
|
+
api: this.params.api,
|
|
134
|
+
model: this.params.model,
|
|
135
|
+
key: this.params.key,
|
|
136
|
+
temperature: 0.7,
|
|
137
|
+
topP: 0.5
|
|
138
|
+
})
|
|
139
|
+
this.llm.on("log", (level: string, message: string) => {
|
|
140
|
+
this.log(level as "info" | "warning" | "error", message)
|
|
141
|
+
})
|
|
142
|
+
await this.llm.open()
|
|
143
|
+
|
|
144
|
+
/* provide text-to-text punctuation restoration */
|
|
145
|
+
const llm = this.llm!
|
|
146
|
+
const punctuate = async (text: string) => {
|
|
147
|
+
const cfg = this.setup[this.params.lang]
|
|
148
|
+
if (!cfg)
|
|
149
|
+
throw new Error(`unsupported language: ${this.params.lang}`)
|
|
150
|
+
return llm.complete({
|
|
151
|
+
system: cfg.systemPrompt,
|
|
152
|
+
messages: cfg.chat,
|
|
153
|
+
prompt: text
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/* establish a transform stream for punctuation restoration */
|
|
158
|
+
this.stream = new Stream.Transform({
|
|
159
|
+
readableObjectMode: true,
|
|
160
|
+
writableObjectMode: true,
|
|
161
|
+
decodeStrings: false,
|
|
162
|
+
highWaterMark: 1,
|
|
163
|
+
transform (chunk: SpeechFlowChunk, encoding, callback) {
|
|
164
|
+
if (Buffer.isBuffer(chunk.payload))
|
|
165
|
+
callback(new Error("invalid chunk payload type"))
|
|
166
|
+
else if (chunk.payload === "") {
|
|
167
|
+
this.push(chunk)
|
|
168
|
+
callback()
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
punctuate(chunk.payload).then((payload) => {
|
|
172
|
+
const chunkNew = chunk.clone()
|
|
173
|
+
chunkNew.payload = payload
|
|
174
|
+
this.push(chunkNew)
|
|
175
|
+
callback()
|
|
176
|
+
}).catch((error: unknown) => {
|
|
177
|
+
callback(util.ensureError(error))
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
final (callback) {
|
|
182
|
+
callback()
|
|
183
|
+
}
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/* close node */
|
|
188
|
+
async close () {
|
|
189
|
+
/* shutdown stream */
|
|
190
|
+
if (this.stream !== null) {
|
|
191
|
+
await util.destroyStream(this.stream)
|
|
192
|
+
this.stream = null
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/* shutdown LLM */
|
|
196
|
+
if (this.llm !== null) {
|
|
197
|
+
await this.llm.close()
|
|
198
|
+
this.llm = null
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** SpeechFlow - Speech Processing Flow Graph
|
|
3
|
+
** Copyright (c) 2024-2025 Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
4
|
+
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/* standard dependencies */
|
|
8
|
+
import Stream from "node:stream"
|
|
9
|
+
|
|
10
|
+
/* internal dependencies */
|
|
11
|
+
import SpeechFlowNode, { SpeechFlowChunk } from "./speechflow-node"
|
|
12
|
+
import * as util from "./speechflow-util"
|
|
13
|
+
import { LLM, type LLMCompleteMessage } from "./speechflow-util-llm"
|
|
14
|
+
|
|
15
|
+
/* internal utility types */
|
|
16
|
+
type ConfigEntry = { systemPrompt: string, chat: LLMCompleteMessage[] }
|
|
17
|
+
type Config = { [ key: string ]: ConfigEntry }
|
|
18
|
+
|
|
19
|
+
/* SpeechFlow node for LLM-based text-to-text spellchecking */
|
|
20
|
+
export default class SpeechFlowNodeT2TSpellcheck extends SpeechFlowNode {
|
|
21
|
+
/* declare official node name */
|
|
22
|
+
public static name = "t2t-spellcheck"
|
|
23
|
+
|
|
24
|
+
/* internal state */
|
|
25
|
+
private llm: LLM | null = null
|
|
26
|
+
|
|
27
|
+
/* internal LLM setup */
|
|
28
|
+
private setup: Config = {
|
|
29
|
+
/* English (EN) spellchecking */
|
|
30
|
+
"en": {
|
|
31
|
+
systemPrompt:
|
|
32
|
+
"You are a proofreader and spellchecker for English.\n" +
|
|
33
|
+
"Output only the corrected text.\n" +
|
|
34
|
+
"Do NOT use markdown.\n" +
|
|
35
|
+
"Do NOT give any explanations.\n" +
|
|
36
|
+
"Do NOT give any introduction.\n" +
|
|
37
|
+
"Do NOT give any comments.\n" +
|
|
38
|
+
"Do NOT give any preamble.\n" +
|
|
39
|
+
"Do NOT give any prolog.\n" +
|
|
40
|
+
"Do NOT give any epilog.\n" +
|
|
41
|
+
"Do NOT change the grammar.\n" +
|
|
42
|
+
"Do NOT use synonyms for words.\n" +
|
|
43
|
+
"Keep all words.\n" +
|
|
44
|
+
"Fill in missing commas.\n" +
|
|
45
|
+
"Fill in missing points.\n" +
|
|
46
|
+
"Fill in missing question marks.\n" +
|
|
47
|
+
"Fill in missing hyphens.\n" +
|
|
48
|
+
"Focus ONLY on the word spelling.\n" +
|
|
49
|
+
"The text you have to correct is:\n",
|
|
50
|
+
chat: [
|
|
51
|
+
{ role: "user", content: "I luve my wyfe" },
|
|
52
|
+
{ role: "assistant", content: "I love my wife." },
|
|
53
|
+
{ role: "user", content: "The weether is wunderfull!" },
|
|
54
|
+
{ role: "assistant", content: "The weather is wonderful!" },
|
|
55
|
+
{ role: "user", content: "The life awesome but I'm hungry." },
|
|
56
|
+
{ role: "assistant", content: "The life is awesome, but I'm hungry." }
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
/* German (DE) spellchecking */
|
|
61
|
+
"de": {
|
|
62
|
+
systemPrompt:
|
|
63
|
+
"Du bist ein Korrekturleser und Rechtschreibprüfer für Deutsch.\n" +
|
|
64
|
+
"Gib nur den korrigierten Text aus.\n" +
|
|
65
|
+
"Benutze KEIN Markdown.\n" +
|
|
66
|
+
"Gib KEINE Erklärungen.\n" +
|
|
67
|
+
"Gib KEINE Einleitung.\n" +
|
|
68
|
+
"Gib KEINE Kommentare.\n" +
|
|
69
|
+
"Gib KEINE Präambel.\n" +
|
|
70
|
+
"Gib KEINEN Prolog.\n" +
|
|
71
|
+
"Gib KEINEN Epilog.\n" +
|
|
72
|
+
"Ändere NICHT die Grammatik.\n" +
|
|
73
|
+
"Verwende KEINE Synonyme für Wörter.\n" +
|
|
74
|
+
"Behalte alle Wörter bei.\n" +
|
|
75
|
+
"Füge fehlende Kommas ein.\n" +
|
|
76
|
+
"Füge fehlende Punkte ein.\n" +
|
|
77
|
+
"Füge fehlende Fragezeichen ein.\n" +
|
|
78
|
+
"Füge fehlende Bindestriche ein.\n" +
|
|
79
|
+
"Füge fehlende Gedankenstriche ein.\n" +
|
|
80
|
+
"Fokussiere dich NUR auf die Rechtschreibung der Wörter.\n" +
|
|
81
|
+
"Der von dir zu korrigierende Text ist:\n",
|
|
82
|
+
chat: [
|
|
83
|
+
{ role: "user", content: "Ich ljebe meine Frao" },
|
|
84
|
+
{ role: "assistant", content: "Ich liebe meine Frau." },
|
|
85
|
+
{ role: "user", content: "Die Wedter ist wunderschoen." },
|
|
86
|
+
{ role: "assistant", content: "Das Wetter ist wunderschön." },
|
|
87
|
+
{ role: "user", content: "Das Leben einfach großartig aber ich bin hungrig." },
|
|
88
|
+
{ role: "assistant", content: "Das Leben ist einfach großartig, aber ich bin hungrig." }
|
|
89
|
+
]
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* construct node */
|
|
94
|
+
constructor (id: string, cfg: { [ id: string ]: any }, opts: { [ id: string ]: any }, args: any[]) {
|
|
95
|
+
super(id, cfg, opts, args)
|
|
96
|
+
|
|
97
|
+
/* declare node configuration parameters */
|
|
98
|
+
this.configure({
|
|
99
|
+
lang: { type: "string", pos: 0, val: "en", match: /^(?:de|en)$/ },
|
|
100
|
+
provider: { type: "string", val: "ollama", match: /^(?:openai|anthropic|google|ollama|transformers)$/ },
|
|
101
|
+
api: { type: "string", val: "http://127.0.0.1:11434", match: /^https?:\/\/.+?(:\d+)?$/ },
|
|
102
|
+
model: { type: "string", val: "gemma3:4b-it-q4_K_M", match: /^.+$/ },
|
|
103
|
+
key: { type: "string", val: "", match: /^.*$/ }
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
/* tell effective mode */
|
|
107
|
+
this.log("info", `spellchecking language "${this.params.lang}" ` +
|
|
108
|
+
`via ${this.params.provider} LLM (model: ${this.params.model})`)
|
|
109
|
+
|
|
110
|
+
/* declare node input/output format */
|
|
111
|
+
this.input = "text"
|
|
112
|
+
this.output = "text"
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/* open node */
|
|
116
|
+
async open () {
|
|
117
|
+
/* instantiate LLM */
|
|
118
|
+
this.llm = new LLM({
|
|
119
|
+
provider: this.params.provider,
|
|
120
|
+
api: this.params.api,
|
|
121
|
+
model: this.params.model,
|
|
122
|
+
key: this.params.key,
|
|
123
|
+
temperature: 0.7,
|
|
124
|
+
topP: 0.5
|
|
125
|
+
})
|
|
126
|
+
this.llm.on("log", (level: string, message: string) => {
|
|
127
|
+
this.log(level as "info" | "warning" | "error", message)
|
|
128
|
+
})
|
|
129
|
+
await this.llm.open()
|
|
130
|
+
|
|
131
|
+
/* provide text-to-text spellchecking */
|
|
132
|
+
const llm = this.llm!
|
|
133
|
+
const spellcheck = async (text: string) => {
|
|
134
|
+
const cfg = this.setup[this.params.lang]
|
|
135
|
+
if (!cfg)
|
|
136
|
+
throw new Error(`unsupported language: ${this.params.lang}`)
|
|
137
|
+
return llm.complete({
|
|
138
|
+
system: cfg.systemPrompt,
|
|
139
|
+
messages: cfg.chat,
|
|
140
|
+
prompt: text
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* establish a transform stream and connect it to LLM */
|
|
145
|
+
this.stream = new Stream.Transform({
|
|
146
|
+
readableObjectMode: true,
|
|
147
|
+
writableObjectMode: true,
|
|
148
|
+
decodeStrings: false,
|
|
149
|
+
highWaterMark: 1,
|
|
150
|
+
transform (chunk: SpeechFlowChunk, encoding, callback) {
|
|
151
|
+
if (Buffer.isBuffer(chunk.payload))
|
|
152
|
+
callback(new Error("invalid chunk payload type"))
|
|
153
|
+
else if (chunk.payload === "") {
|
|
154
|
+
this.push(chunk)
|
|
155
|
+
callback()
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
spellcheck(chunk.payload).then((payload) => {
|
|
159
|
+
const chunkNew = chunk.clone()
|
|
160
|
+
chunkNew.payload = payload
|
|
161
|
+
this.push(chunkNew)
|
|
162
|
+
callback()
|
|
163
|
+
}).catch((error: unknown) => {
|
|
164
|
+
callback(util.ensureError(error))
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
final (callback) {
|
|
169
|
+
callback()
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/* close node */
|
|
175
|
+
async close () {
|
|
176
|
+
/* shutdown stream */
|
|
177
|
+
if (this.stream !== null) {
|
|
178
|
+
await util.destroyStream(this.stream)
|
|
179
|
+
this.stream = null
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/* shutdown LLM */
|
|
183
|
+
if (this.llm !== null) {
|
|
184
|
+
await this.llm.close()
|
|
185
|
+
this.llm = null
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|