Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when storing embeddings in localStorage #96

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/hooks/useStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,69 @@ export async function setStorage<T>(
return false
}
}

/**
* Function to save data to IndexedDB
*/
export const saveToIndexedDB = async (key: string, data: any) => {
return new Promise<void>((resolve, reject) => {
const request = indexedDB.open('SynciaDB', 1)

request.onupgradeneeded = (event) => {
const db = (event.target as IDBOpenDBRequest).result
db.createObjectStore('embeddings')
}

request.onsuccess = (event) => {
const db = (event.target as IDBOpenDBRequest).result
const transaction = db.transaction('embeddings', 'readwrite')
const store = transaction.objectStore('embeddings')
store.put(data, key)

transaction.oncomplete = () => {
resolve()
}

transaction.onerror = (event) => {
reject(event)
}
}

request.onerror = (event) => {
reject(event)
}
})
}
Royal-lobster marked this conversation as resolved.
Show resolved Hide resolved

/**
* Function to retrieve data from IndexedDB
*/
export const getFromIndexedDB = async (key: string) => {
return new Promise<any>((resolve, reject) => {
const request = indexedDB.open('SynciaDB', 1)

request.onupgradeneeded = (event) => {
const db = (event.target as IDBOpenDBRequest).result
db.createObjectStore('embeddings')
}

request.onsuccess = (event) => {
const db = (event.target as IDBOpenDBRequest).result
const transaction = db.transaction('embeddings', 'readonly')
const store = transaction.objectStore('embeddings')
const getRequest = store.get(key)

getRequest.onsuccess = () => {
resolve(getRequest.result)
}

getRequest.onerror = (event) => {
reject(event)
}
}

request.onerror = (event) => {
reject(event)
}
})
}
Royal-lobster marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 4 additions & 5 deletions src/lib/getMatchedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OpenAIEmbeddings } from '@langchain/openai'
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'
import { MemoryVectorStore } from 'langchain/vectorstores/memory'
import { createSHA256Hash } from './createSHA256Hash'
import { getFromIndexedDB, saveToIndexedDB } from '../hooks/useStorage'

/**
* This function is responsible for getting the matched content
Expand All @@ -21,7 +22,7 @@ export const getMatchedContent = async (

/**
* This function is responsible for getting the context vector store
* from the context. It caches the vector store in the local storage
* from the context. It caches the vector store in the IndexedDB
* for faster retrieval
*/
const getContextVectorStore = async (
Expand All @@ -36,17 +37,15 @@ const getContextVectorStore = async (
},
})
const hashKey = `SYNCIA_STORE_EMBEDDINGS_${await createSHA256Hash(context)}`
const memoryVectors: [] | null = JSON.parse(
localStorage.getItem(hashKey) || 'null',
)
const memoryVectors: [] | null = await getFromIndexedDB(hashKey)

Royal-lobster marked this conversation as resolved.
Show resolved Hide resolved
if (!memoryVectors) {
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
})
const docs = await textSplitter.createDocuments([context])
const store = await MemoryVectorStore.fromDocuments(docs, embeddings)
localStorage.setItem(hashKey, JSON.stringify(store.memoryVectors))
await saveToIndexedDB(hashKey, store.memoryVectors)
return store
Royal-lobster marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
Loading