Skip to content

Instantly share code, notes, and snippets.

View siliconjungle's full-sized avatar

James Addison siliconjungle

View GitHub Profile
const hasCircularReference = (graph, startUID) => {
const visited = new Set()
const stack = [startUID]
while (stack.length > 0) {
let currentUID = stack.pop()
if (visited.has(currentUID)) {
return true
}
@siliconjungle
siliconjungle / vector-db.js
Created October 15, 2023 21:59
Simple vector db embeddings.
import readline from 'readline'
import chalk from 'chalk'
import OpenAI from 'openai'
import { LocalIndex } from 'vectra'
import { fileURLToPath } from 'url'
import path, { dirname } from 'path'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
// These methods were made by chat gpt and have not been tested.
const cosineSimilarity = (vecA, vecB) => {
if (vecA.length !== vecB.length) {
throw new Error("Vectors must be of the same length.");
}
let dotProduct = vecA.reduce((sum, val, i) => sum + val * vecB[i], 0);
let normA = vecA.reduce((sum, val) => sum + val * val, 0);
let normB = vecB.reduce((sum, val) => sum + val * val, 0);
@siliconjungle
siliconjungle / bag-of-words.js
Created November 12, 2023 23:33
Simple bag of words implementation
const tokenize = (text) =>
text.toLowerCase().split(/\W+/).filter(token => token.length > 0)
const buildVocabulary = (sentences) => {
const vocabulary = new Set()
sentences.forEach(sentence => {
tokenize(sentence).forEach(token => {
vocabulary.add(token)
})
@siliconjungle
siliconjungle / simple-diff.js
Created December 10, 2023 19:49
Simple diff
// This is a basic diff mechanism, it is not going to work for characters that take up more than
// one byte. It is also not going to normalise text.
// these are things that *should happen* in the future, but for now they are going to be ignored.
// returns the insert position & the text to insert as well as the delete position & the text to delete
// one of the problems with this kind of diff is that you can't tell in some cases where the insert
// actually occurred.
// for this you will need to take into account the cursor start & end.
export const diff = (a, b) => {
if (a === b) {
path {
fill: none;
stroke: rgba(52, 53, 60, 1);
stroke-width: 4;
stroke-linecap: round;
}
svg > text {
font-family: 'Geist', sans-serif !important;
fill: rgb(201, 213, 229);