Created
June 6, 2024 10:02
-
-
Save coderofsalvation/6fb2c3dc63c9826a653c6a78087ab488 to your computer and use it in GitHub Desktop.
superfast cli/speech completion via levensteihn distance #NOLLM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function LDbrain(opts) { | |
const trainingData = []; | |
function trainKeyValue(key, properties) { | |
trainingData.push({ k: key, ...properties }); | |
} | |
function train(data) { | |
for (let k in data) { | |
// wrap string in functions, so they can still be lazily evaluated with template vars | |
let vars = data[k]; | |
for (let i in vars) { | |
if (!i.match(/^(C|R|U|D|L|S|I|T|O)$/)) continue; | |
if (typeof vars[i] !== 'function') { | |
vars[i] = new Function('v',`return \`${vars[i]}\``) | |
} | |
} | |
trainKeyValue(k, vars); | |
} | |
} | |
function run(input, treshold = 10) { | |
const results = trainingData.map(({ k, ...properties }) => ({ k, ...properties, distance: levenshtein(input, k) })); | |
const minDistance = Math.min(...results.map(result => result.distance)); | |
// Filter out items with distances greater than the minimum distance | |
const filteredClass = results.filter(result => result.distance === minDistance && result.distance < treshold); | |
const closestClass = filteredClass[0] ? filteredClass[0].class : '' | |
const filteredResults = results.filter( (r) => r.class == closestClass && r.distance < treshold ) | |
return { items: filteredResults, out: CRUDLSITO(filteredResults) } | |
} | |
// [CRUDL]SITO cmd | |
function CRUDLSITO(items){ | |
let vars = {} | |
items.map( (item) => { | |
for( let i in item ) | |
if( i.match(/^(C|R|U|D|L|S|I|T|O)$/) ) vars[i] = item[i] | |
}) | |
vars = new Proxy(vars,{ | |
get(vars,k){ | |
if( vars[k] ) return vars[k] | |
if( k == "I" ) return (vars.I = () => opts.prompt('unique id?')) | |
if( k == "T" ) return (vars.T = () => opts.prompt('what type?')) | |
} | |
}) | |
vars.cmd = (vars.C||vars.R||vars.U||vars.D||vars.L) | |
if( !vars.cmd ){ | |
let crudl = opts.prompt(`(c)reate, (s)how, (u)pdate, (d)elete or (l)ink ${vars.T()}?`) | |
let cmd = crudl[0].toUpperCase() | |
vars.cmd = vars[cmd] = trainingData.filter( (t) => t.class == items[0].class && t[cmd] != undefined )[0][cmd] | |
} | |
if( vars.cmd ) return vars.cmd(vars) | |
else return 'not enough operands '+JSON.stringify(vars) | |
} | |
function levenshtein(a, b) { | |
const m = a.length; | |
const n = b.length; | |
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); | |
for (let i = 0; i <= m; i++) { | |
for (let j = 0; j <= n; j++) { | |
if (i === 0) { | |
dp[i][j] = j; | |
} else if (j === 0) { | |
dp[i][j] = i; | |
} else { | |
dp[i][j] = Math.min( | |
dp[i - 1][j - 1] + (a[i - 1] !== b[j - 1] ? 1 : 0), | |
dp[i][j - 1] + 1, | |
dp[i - 1][j] + 1 | |
); | |
} | |
} | |
} | |
return dp[m][n]; | |
} | |
return { | |
train, | |
trainKeyValue, | |
run, | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example usage: | |
b = LDbrain(window); | |
// import json trainingdata | |
b.train({ | |
"create": { class: 'aframe.entity', C: '<a-${v.T()} id="${v.I()}"/>' }, | |
'cube': { class: 'aframe.entity', T: 'cube' }, | |
'named': { class: 'aframe.entity', I: 'entity${String(Math.random()).substr(2,5)}' }, | |
}) | |
// realtime training | |
b.trainKeyValue('show', { class: 'bash.find', C: (v) => `find . -type ${v.T()}` }); | |
b.trainKeyValue('files', { class: 'bash.find', T: (v) => `f` }); | |
result2 = b.run('a cube',4); | |
console.log(result2); | |
// Output: [ { k: 'create', class: 'aframe', distance: .. }, { k: 'cube', class: 'aframe', tpl: [Function: tpl], distance: .. } ] | |
console.log( b.run('show files') ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment