-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_init.py
76 lines (58 loc) · 2.52 KB
/
db_init.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
import os
from langchain.document_loaders import OnlinePDFLoader
import pinecone
from langchain.embeddings.cohere import CohereEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Pinecone
from langchain.document_loaders import ArxivLoader
# Read the pdf file from an online url
def read_pdf(url):
loader = OnlinePDFLoader(url)
data = loader.load()
return data
# Read the arxiv research paper pdf from the arxiv id
def read_arxiv(paper_id):
docs = ArxivLoader(query=paper_id, load_max_docs=2).load()
return docs[0].page_content
def initdb(file_url):
"""
This function initializes the pinecone index and uploads the document to the pinecone index using the Cohere Embeddings
:param file_url: In case of online pdf file, the url of the pdf file abd in case of arxiv paper, the arxiv id of the paper
:return: None
"""
os.environ["COHERE_API_KEY"] = "YOUR COHERE API KEY"
# Embedding model
embeddings = CohereEmbeddings(model="embed-english-v3.0")
# init Pinecone
PINECONE_API_KEY = os.getenv('PINECONE_API_KEY', 'YOUR PINECONE API KEY')
PINECONE_API_ENV = os.getenv('PINECONE_API_ENV', 'YOUR PINECONE API ENVIRONMENT')
pinecone.init(
api_key=PINECONE_API_KEY,
environment=PINECONE_API_ENV,
)
# index name
index_name = "rag-langchain-test"
# First, check if our index already exists. If it doesn't, we create it
if index_name not in pinecone.list_indexes():
# we create a new index
pinecone.create_index(name=index_name, metric="cosine", dimension=1024)
# Read the online pdf file
# pdf_text = read_pdf(file_url)
# Read the arxiv paper Hardcoded for now
pdf_text = read_arxiv(file_url)
# Chunk the text into 500 word chunks and upload to Pinecone in batches using Cohere Embeddings
splitter = RecursiveCharacterTextSplitter(
chunk_size=900,
chunk_overlap=50,
separators=["\n\n", "\n", " ", ""]
)
# In case of arxiv paper we use split_text
chunks = splitter.split_text(pdf_text)
# for online pdf file other than arxiv we split docs
# chunks = splitter.split_document(pdf_text)
# for online pdf file other than arxiv
# docsearch = Pinecone.from_documents(chunks, embeddings, index_name=index_name)
# for arxiv paper pdf since it is text based
docsearch = Pinecone.from_texts(chunks, embeddings, index_name=index_name)
print("Document search initialized")
# Link : https://arxiv.org/pdf/2005.11401.pdf