-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill_db.py
62 lines (44 loc) · 1.31 KB
/
fill_db.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
54
55
56
57
58
59
60
61
from langchain_community.document_loaders import PyPDFDirectoryLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
import chromadb
from sentence_transformers import SentenceTransformer
import os
from dotenv import load_dotenv
load_dotenv()
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
# setting the environment
DATA_PATH = r"data"
CHROMA_PATH = r"chroma_db"
chroma_client = chromadb.PersistentClient(path=CHROMA_PATH)
collection = chroma_client.get_or_create_collection(name="growing_vegetables")
# loading the document
loader = PyPDFDirectoryLoader(DATA_PATH)
raw_documents = loader.load()
# splitting the document
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=300,
chunk_overlap=100,
length_function=len,
is_separator_regex=False,
)
chunks = text_splitter.split_documents(raw_documents)
# preparing to be added in chromadb
documents = []
metadata = []
ids = []
embeddings = []
i = 0
for chunk in chunks:
documents.append(chunk.page_content)
ids.append("ID"+str(i))
metadata.append(chunk.metadata)
embedding = embedding_model.encode(chunk.page_content)
embeddings.append(embedding.tolist())
i += 1
# adding to chromadb
collection.upsert(
documents=documents,
metadatas=metadata,
ids=ids,
embeddings=embeddings
)