-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathingestion.py
More file actions
75 lines (52 loc) · 1.79 KB
/
ingestion.py
File metadata and controls
75 lines (52 loc) · 1.79 KB
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
# import basics
import os
import time
from dotenv import load_dotenv
# import pinecone
from pinecone import Pinecone, ServerlessSpec
# import langchain
from langchain_pinecone import PineconeVectorStore
from langchain_openai import OpenAIEmbeddings
from langchain_core.documents import Document
#documents
from langchain_community.document_loaders import PyPDFDirectoryLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
load_dotenv()
pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
# initialize pinecone database
index_name = os.environ.get("PINECONE_INDEX_NAME") # change if desired
# check whether index exists, and create if not
existing_indexes = [index_info["name"] for index_info in pc.list_indexes()]
if index_name not in existing_indexes:
pc.create_index(
name=index_name,
dimension=3072,
metric="cosine",
spec=ServerlessSpec(cloud="aws", region="us-east-1"),
)
while not pc.describe_index(index_name).status["ready"]:
time.sleep(1)
index = pc.Index(index_name)
# initialize embeddings model + vector store
embeddings = OpenAIEmbeddings(model="text-embedding-3-large",api_key=os.environ.get("OPENAI_API_KEY"))
vector_store = PineconeVectorStore(index=index, embedding=embeddings)
# loading the PDF document
loader = PyPDFDirectoryLoader("documents/")
raw_documents = loader.load()
# splitting the document
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=800,
chunk_overlap=400,
length_function=len,
is_separator_regex=False,
)
# creating the chunks
documents = text_splitter.split_documents(raw_documents)
# generate unique id's
i = 0
uuids = []
while i < len(documents):
i += 1
uuids.append(f"id{i}")
# add to database
vector_store.add_documents(documents=documents, ids=uuids)