-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingestion.py
79 lines (62 loc) · 2.88 KB
/
ingestion.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
from dotenv import load_dotenv
import os
load_dotenv()
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import ReadTheDocsLoader
from langchain_openai import OpenAIEmbeddings
from langchain_pinecone import PineconeVectorStore
from langchain_community.document_loaders import FireCrawlLoader
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
def ingest_docs():
loader = ReadTheDocsLoader(
path="langchain-docs1/api.python.langchain.com/en/latest",
encoding="ISO-8859-1",
)
raw_documents = loader.load()
print(f"loaded {len(raw_documents)} documents")
text_splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)
documents = text_splitter.split_documents(raw_documents)
for doc in documents:
new_url = doc.metadata["source"]
new_url = new_url.replace("langchain-docs1", "https:/")
doc.metadata.update({"source": new_url})
print(f"Going to add {len(documents)} to Pinecone")
PineconeVectorStore.from_documents(
documents, embeddings, index_name=os.environ["INDEX_NAME"]
)
print("****Loading to vectorstore done****")
def ingest_docs2() -> None:
langchain_documents_base_urls = [
"https://python.langchain.com/v0.2/docs/integrations/chat/",
"https://python.langchain.com/v0.2/docs/integrations/llms/",
"https://python.langchain.com/v0.2/docs/integrations/text_embedding/",
"https://python.langchain.com/v0.2/docs/integrations/document_loaders/",
"https://python.langchain.com/v0.2/docs/integrations/document_transformers/",
"https://python.langchain.com/v0.2/docs/integrations/vectorstores/",
"https://python.langchain.com/v0.2/docs/integrations/retrievers/",
"https://python.langchain.com/v0.2/docs/integrations/tools/",
"https://python.langchain.com/v0.2/docs/integrations/stores/",
"https://python.langchain.com/v0.2/docs/integrations/llm_caching/",
"https://python.langchain.com/v0.2/docs/integrations/graphs/",
"https://python.langchain.com/v0.2/docs/integrations/memory/",
"https://python.langchain.com/v0.2/docs/integrations/callbacks/",
"https://python.langchain.com/v0.2/docs/integrations/chat_loaders/",
"https://python.langchain.com/v0.2/docs/concepts/",
]
for url in langchain_documents_base_urls:
print(f"FireCrawling{url=}")
loader = FireCrawlLoader(
url= url,
mode = "crawl",
params = {
"limit": 5,
},
)
docs = loader.load()
print(f"Going to add {len(docs)} documents to Pinecone")
PineconeVectorStore.from_documents(
docs, embeddings, index_name = "firecrawl-index"
)
print(f"****Loading {url}* to vectorstore done ***")
if __name__ == "__main__":
ingest_docs()