-
Notifications
You must be signed in to change notification settings - Fork 0
/
embedded.py
53 lines (45 loc) · 1.48 KB
/
embedded.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
from dotenv import load_dotenv
from pymongo import MongoClient
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import MongoDBAtlasVectorSearch
from langchain_community.document_loaders import DirectoryLoader, TextLoader
from langchain_community.embeddings import HuggingFaceEmbeddings
load_dotenv()
# Load environment variables
MONGO_URI = os.getenv("MONGO_URI")
DB_NAME = "GCBOT"
COLLECTION_NAME = "GCBOT"
ATLAS_VECTOR_SEARCH_INDEX_NAME = "GCBOT"
EMBEDDING_FIELD_NAME = "embedding"
client = MongoClient(MONGO_URI)
db = client[DB_NAME]
collection = db[COLLECTION_NAME]
DATA_DIR = os.getenv("DATA_DIR", "AllData")
# Document loading and splitting
loader = DirectoryLoader(
DATA_DIR,
glob="**/*.txt",
use_multithreading=True,
loader_cls=TextLoader,
)
data = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=100)
docs = text_splitter.split_documents(data)
# Initialize Hugging Face Embeddings
model_name = "sentence-transformers/all-MiniLM-L6-v2"
model_kwargs = {"device": "cpu"}
encode_kwargs = {"normalize_embeddings": True}
hf = HuggingFaceEmbeddings(
model_name=model_name,
model_kwargs=model_kwargs,
encode_kwargs=encode_kwargs,
multi_process=False, # Disable multiprocessing
)
# Insert the documents into MongoDB Atlas Vector Search
x = MongoDBAtlasVectorSearch.from_documents(
documents=docs,
embedding=hf,
collection=collection,
index_name="embedding",
)