Add intent resolving with Xenova/all-MiniLM-L6-v2

This commit is contained in:
Apher 2026-07-07 00:36:57 +00:00
parent c3f00ac699
commit cafb386840
3 changed files with 967 additions and 13 deletions

859
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,7 @@
"license": "ISC",
"type": "commonjs",
"dependencies": {
"@xenova/transformers": "^2.17.2",
"pg": "^8.22.0",
"redis": "^6.1.0",
"sequelize": "^6.37.8"

View File

@ -3,6 +3,8 @@ if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const {pipeline} = require("@xenova/transformers");
const models = require("./models");
const db = models.db;
const redis = models.redis;
@ -10,16 +12,106 @@ const {sequelize, Sequelize} = db;
const config = require("./config");
const handleIntent = async (streamKey, intent) => {
await redis.client.xAdd(
streamKey,
'*',
const INTENTS = [
{
transcript_id: intent.id,
command: "[TEST] INTENT_RECEIVED",
device_id: intent.message.device_id
name: "play_music",
examples: ["play music", "start music", "resume music", "play songs"],
},
{
name: "stop_music",
examples: ["stop music", "pause music", "halt music", "stop playback"],
},
{
name: "open_browser",
examples: ["open browser", "launch browser", "start chrome", "open chrome"],
},
{
name: "set_volume",
examples: ["set volume to 30", "volume 50", "turn volume up", "turn volume down"],
},
];
let extractor;
function normalizeText(text) {
return text
.toLowerCase()
.replace(/[^\w\s]/g, " ")
.replace(/\s+/g, " ")
.trim();
}
)
function cosineSimilarity(a, b) {
let dot = 0;
let magA = 0;
let magB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
magA += a[i] * a[i];
magB += b[i] * b[i];
}
return dot / (Math.sqrt(magA) * Math.sqrt(magB));
}
async function initModel() {
extractor = await pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
}
async function embed(text) {
const output = await extractor(normalizeText(text), {
pooling: "mean",
normalize: true,
});
return Array.from(output.data);
}
async function classifyIntent(text) {
const inputEmbedding = await embed(text);
let best = {
name: "unknown",
confidence: 0,
matched: null,
};
for (const intent of INTENTS) {
for (const example of intent.examples) {
const exampleEmbedding = await embed(example);
const score = cosineSimilarity(inputEmbedding, exampleEmbedding);
if (score > best.confidence) {
best = {
name: intent.name,
confidence: score,
matched: example,
};
}
}
}
return best;
}
async function handleIntent(streamKey, msg) {
const text = msg.message?.text;
const deviceId = msg.message?.device_id;
if (!text) return;
const intent = await classifyIntent(text);
await redis.client.xAdd(streamKey, '*', {
transcript_id: msg.id,
command: intent.name,
confidence: String(intent.confidence),
matched: intent.matched || "",
device_id: deviceId || "",
text,
});
console.log("TODO: Handle Intents", intent);
}
@ -60,6 +152,8 @@ const main = async () => {
if (doForce) console.info("Database dropped and resynced (force=true).");
await initModel();
await redis.ensureGroup("transcripts", 'nlp');
await redis.ensureGroup("commands", 'nlp');