forked from emarco177/documentation-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingestion.py
36 lines (29 loc) · 1.2 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
import os
from langchain.document_loaders import ReadTheDocsLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Pinecone
import pinecone
pinecone.init(
api_key=os.environ["PINECONE_API_KEY"],
environment=os.environ["PINECONE_ENVIRONMENT_REGION"],
)
INDEX_NAME = "langchain-doc-index"
def ingest_docs():
loader = ReadTheDocsLoader("langchain-docs/langchain.readthedocs.io/en/latest")
raw_documents = loader.load()
print(f"loaded {len(raw_documents)} documents")
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=400, chunk_overlap=50, separators=["\n\n", "\n", " ", ""]
)
documents = text_splitter.split_documents(raw_documents)
for doc in documents:
new_url = doc.metadata["source"]
new_url = new_url.replace("langchain-docs", "https:/")
doc.metadata.update({"source": new_url})
embeddings = OpenAIEmbeddings()
print(f"Going to add {len(documents)} to Pinecone")
Pinecone.from_documents(documents, embeddings, index_name=INDEX_NAME)
print("****Loading to vectorestore done ***")
if __name__ == "__main__":
ingest_docs()