-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_db.py
50 lines (43 loc) · 1.48 KB
/
create_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
from langchain_community.document_loaders import DirectoryLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.schema import Document
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores.chroma import Chroma
from typing import List
import os
import shutil
from env import openai_api_key
from paths import DATA_PATH, CHROMA_PATH
os.environ['OPENAI_API_KEY'] = openai_api_key
def generate_data_store():
documents = load_documents()
chunks = split_text(documents)
save_to_chroma(chunks)
def load_documents():
loader = DirectoryLoader(DATA_PATH, glob="*.md")
documents = loader.load()
return documents
def split_text(documents: List[Document]):
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=200,
chunk_overlap=50,
length_function=len,
add_start_index=True
)
chunks = text_splitter.split_documents(documents)
print(f"We split {len(documents)} into {len(chunks)} chunks")
# docs = chunks[10: 13]
# for doc in docs:
# print(doc.page_content)
# print(doc.metadata)
return chunks
def save_to_chroma(chunks: List[Document]):
if os.path.exists(CHROMA_PATH):
shutil.rmtree(CHROMA_PATH)
# Creating new DB
db = Chroma.from_documents(
chunks, OpenAIEmbeddings(), persist_directory=CHROMA_PATH
)
print(f"Saved {len(chunks)} chunks to {CHROMA_PATH}")
if __name__ == "__main__":
generate_data_store()