-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72e018f
commit 5b1e3c6
Showing
9 changed files
with
64 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const createSHA256Hash = async (content: string): Promise<string> => { | ||
const encoder = new TextEncoder() | ||
const data = encoder.encode(content) | ||
const hashBuffer = await crypto.subtle.digest('SHA-256', data) | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)) | ||
const hashHex = hashArray | ||
.map((byte) => byte.toString(16).padStart(2, '0')) | ||
.join('') | ||
return hashHex | ||
} |
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { OpenAIEmbeddings } from 'langchain/embeddings/openai' | ||
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter' | ||
import { MemoryVectorStore } from 'langchain/vectorstores/memory' | ||
import { createSHA256Hash } from './createSHA256Hash' | ||
|
||
/** | ||
* This function is responsible for getting the matched content | ||
* from the context and query | ||
*/ | ||
export const getMatchedContent = async ( | ||
query: string, | ||
context: string, | ||
apiKey: string, | ||
) => { | ||
const vectorStore = await getContextVectorStore(context, apiKey) | ||
const retriever = vectorStore.asRetriever() | ||
const relevantDocs = await retriever.getRelevantDocuments(query) | ||
return relevantDocs.map((doc) => doc.pageContent).join('\n') | ||
} | ||
|
||
/** | ||
* This function is responsible for getting the context vector store | ||
* from the context. It caches the vector store in the local storage | ||
* for faster retrieval | ||
*/ | ||
const getContextVectorStore = async (context: string, apiKey: string) => { | ||
const embeddings = new OpenAIEmbeddings({ openAIApiKey: apiKey }) | ||
const hashKey = `SYNCIA_STORE_EMBEDDINGS_${await createSHA256Hash(context)}` | ||
const memoryVectors: [] | null = JSON.parse( | ||
localStorage.getItem(hashKey) || 'null', | ||
) | ||
|
||
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)) | ||
return store | ||
} | ||
|
||
console.log({ memoryVectors }) | ||
|
||
const store = new MemoryVectorStore(embeddings) | ||
store.memoryVectors = memoryVectors | ||
return store | ||
} |
File renamed without changes.