-
Notifications
You must be signed in to change notification settings - Fork 10
/
embeddings.py
102 lines (74 loc) · 2.71 KB
/
embeddings.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
from langchain.document_loaders import UnstructuredFileLoader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
import nltk
from dotenv import load_dotenv
import os
load_dotenv()
nltk.download("punkt")
print("Loading global variables")
# Load Langchain variables
embeddings = OpenAIEmbeddings()
llm = OpenAI(temperature=0)
vectorstore_location = "./docs/"
text_splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=25)
print("base_formatter function")
def check_file(file_path):
loader = UnstructuredFileLoader(file_path)
docs = loader.load()
print(docs)
return docs
def base_formatter(docs):
print("formatting")
print(
f"\n{'-' * 100}\n".join(
[f"Document {i+1}:\n\n" + d.page_content for i, d in enumerate(docs)]
)
)
return f"\n{'-' * 100}\n".join(
[f"Document {i + 1}:\n\n{d.page_content}" for i, d in enumerate(docs)]
)
print("loading check_file function 43")
# Check if the files are valid
def create_mass_embedding(folder_path):
print("creating mass embedding")
if not os.path.exists(folder_path):
folder_path = "docs/empty"
result = "Folder does not exist"
print(result)
return
else:
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
result = create_embedding(file_path, filename)
print(f"Embedding created for {filename}: {result}")
with open("./docs/embed_index.txt", "a") as f:
f.write(f"{os.path.join(folder_path, file_path)}\n")
print(f"Embedding created for {filename}: {result}")
return result
print("create_embedding function")
# Embed a single embedding
def create_embedding(file_path, optional_arg="metadata"):
print("creating embedding")
data = check_file(file_path)
metadata = optional_arg
meta = metadata if metadata else "file_path"
vectordb = Chroma.from_documents(
documents=data,
metadata=meta,
embedding=embeddings,
persist_directory=vectorstore_location,
)
vectordb.persist()
print(data)
return "Embedding created"
print("load_vector_store_docs function")
def load_vector_store_docs():
print("running load_vector_store_docs")
docs = Chroma(persist_directory=vectorstore_location, embedding_function=embeddings)
print(docs)
return docs
print("memory_search function")
# Query the database and pass the info to chatgpt for response