Skip to content

Commit

Permalink
added cache system to vector store
Browse files Browse the repository at this point in the history
  • Loading branch information
Royal-lobster committed Nov 25, 2023
1 parent 72e018f commit 5b1e3c6
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/components/Settings/Sections/ChatSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import SectionHeading from '../Elements/SectionHeading'
import FieldWrapper from '../Elements/FieldWrapper'
import { useSettings } from '../../../hooks/useSettings'
import { validateApiKey } from '../../../utils/validApiKey'
import { validateApiKey } from '../../../lib/validApiKey'
import { AvailableModels, Mode } from '../../../config/settings'

const ChatSettings = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Sidebar/auth/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect } from 'react'
import { useSettings } from '../../../hooks/useSettings'
import { validateApiKey } from '../../../utils/validApiKey'
import { validateApiKey } from '../../../lib/validApiKey'

const Auth = () => {
const [, setSettings] = useSettings()
Expand Down
2 changes: 1 addition & 1 deletion src/components/Sidebar/chat/ChatHistory.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useChatHistory } from '../../../hooks/useChatHistory'
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'
import { RiAddLine, RiCloseCircleFill, RiTimeLine } from 'react-icons/ri'
import { generateReadableRelativeDate } from '../../../utils/generateReadableDate'
import { generateReadableRelativeDate } from '../../../lib/generateReadableDate'

const ChatHistory = () => {
const {
Expand Down
19 changes: 2 additions & 17 deletions src/hooks/useChatCompletion.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import endent from 'endent'
import { ChatOpenAI } from 'langchain/chat_models/openai'
import { OpenAIEmbeddings } from 'langchain/embeddings/openai'
import { AIMessage, HumanMessage, SystemMessage } from 'langchain/schema'
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'
import { MemoryVectorStore } from 'langchain/vectorstores/memory'
import { useMemo, useState } from 'react'
import { AvailableModels, Mode } from '../config/settings'
import { ChatRole, useCurrentChat } from './useCurrentChat'
import { getMatchedContent } from '../lib/getMatchedContent'

interface UseChatCompletionProps {
model: AvailableModels
Expand Down Expand Up @@ -80,20 +78,7 @@ export const useChatCompletion = ({
*/
let matchedContext
if (context) {
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
})
const docs = await textSplitter.createDocuments([context])
const vectorStore = await MemoryVectorStore.fromDocuments(
docs,
new OpenAIEmbeddings({
openAIApiKey: apiKey,
}),
)
const retriever = vectorStore.asRetriever()
const relevantDocs = await retriever.getRelevantDocuments(query)
console.log(relevantDocs)
matchedContext = relevantDocs.map((doc) => doc.pageContent).join('\n')
matchedContext = await getMatchedContent(query, context, apiKey)
}

const expandedQuery = matchedContext
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function useStorage<T>(
): [T, SetValue<T>] {
const [storedValue, setStoredValue] = useAtom(atom)

// biome-ignore lint/correctness/useExhaustiveDependencies: This works fine. i don't want to change it.
useEffect(() => {
readStorage<T>(key, area).then((res) => {
if (res) setStoredValue(res)
Expand Down
10 changes: 10 additions & 0 deletions src/lib/createSHA256Hash.ts
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.
48 changes: 48 additions & 0 deletions src/lib/getMatchedContent.ts
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.

0 comments on commit 5b1e3c6

Please sign in to comment.