-
Notifications
You must be signed in to change notification settings - Fork 5
/
seed.py
67 lines (53 loc) · 2.05 KB
/
seed.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
import os
from dotenv import load_dotenv
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
from langchain_community.document_loaders import UnstructuredHTMLLoader
from langchain_nomic import NomicEmbeddings
from pymongo import MongoClient
load_dotenv()
## Langmith tracking
os.environ["LANGCHAIN_TRACING_V2"]="true"
os.environ["LANGCHAIN_API_KEY"]=os.getenv("LANGCHAIN_API_KEY")
# Connect to MongoDB Atlas cluster
client = MongoClient(os.getenv("MONGO_URI"))
DB_NAME = os.getenv("DB_NAME")
COLLECTION_NAME = os.getenv("COLLECTION_NAME")
ATLAS_VECTOR_SEARCH_INDEX_NAME = "vector_index"
MONGODB_COLLECTION = client[DB_NAME][COLLECTION_NAME]
def seeder(url, cname):
"""
Seed the MongoDB collection with documents extracted from the given URL.
Args:
url (str): The URL from which to extract documents.
cname (str): The name of the company associated with the documents.
Returns:
None
"""
# r = requests.get(url)
# with open("docs.html", "w") as f:
# f.write(r.text)
loader = UnstructuredHTMLLoader(url)
data = loader.load()
# Split the text into smaller chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1500, chunk_overlap=500)
texts = text_splitter.split_documents(data)
embeddings = NomicEmbeddings(
nomic_api_key=os.getenv("NOMIC_API_KEY"),
model='nomic-embed-text-v1.5',
)
for i in range(len(texts)):
texts[i].metadata["source"] = url
texts[i].metadata["ticker"] = cname
vector_search = MongoDBAtlasVectorSearch.from_documents(
documents=texts,
embedding=embeddings,
collection=MONGODB_COLLECTION,
index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,
)
if __name__ == "__main__":
seeder("example_filings/tsla-20231231.html", "TSLA")
seeder("example_filings/10-Q", "TSLA")
seeder("example_filings/aapl-20230930.html", "AAPL")
seeder("example_filings/tsla-20231231.html", "AAPL")
print("Seeding complete.")