File size: 917 Bytes
a73e8b4
 
 
 
 
d87145a
a73e8b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
import { HuggingFaceTransformersEmbeddings } from "langchain/embeddings/hf_transformers";

const DEFAULT_CHUNK_SIZE = 1000;
const VECTOR_STORE_SIZE = 5;
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: DEFAULT_CHUNK_SIZE });

const model = new HuggingFaceTransformersEmbeddings({
    modelName: "Xenova/all-MiniLM-L6-v2",
});

export const similaritySearch = async (input: string, content: string) => {
    const documents = await textSplitter.createDocuments([content]);
  
    const vectorStore = await MemoryVectorStore.fromTexts(    
      [...documents.map(doc => doc.pageContent)],
      [...documents.map((v, k) => k)],
      model
    )
    const queryResult = await vectorStore.similaritySearch(input, VECTOR_STORE_SIZE);
    return queryResult;
}