-
Notifications
You must be signed in to change notification settings - Fork 0
/
with_pinecone.py
185 lines (146 loc) · 6.3 KB
/
with_pinecone.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
import pinecone
import requests
import mimetypes
from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlsplit
from dotenv import load_dotenv
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Pinecone
from langchain.chat_models import ChatOpenAI
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage
)
from langchain.document_loaders import (
PyPDFLoader,
CSVLoader,
UnstructuredWordDocumentLoader,
WebBaseLoader,
)
load_dotenv()
# Get the Variables from the .env file
OPENAI_API_KEY = os.getenv('OPEN_AI_KEY')
PINECONE_API_KEY = os.getenv('PINECONE_API_KEY')
PINECONE_ENVIRONMENT = os.getenv('PINECONE_ENVIRONMENT')
PINECONE_INDEX_NAME = os.getenv('PINECONE_INDEX_NAME')
WEBSITE_URL = os.getenv('WEBSITE_URLS')
WEBSITE_URLS = WEBSITE_URL.split(",")
embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
chat = ChatOpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)
class PineconeManager:
def __init__(self, api_key, environment):
pinecone.init(
api_key=api_key,
environment=environment
)
def list_indexes(self):
return pinecone.list_indexes()
def create_index(self, index_name, dimension, metric):
pinecone.create_index(name=index_name, dimension=dimension, metric=metric)
def delete_index(self, index_name):
pinecone.deinit()
class URLHandler:
@staticmethod
def is_valid_url(url):
parsed_url = urlsplit(url)
return bool(parsed_url.scheme) and bool(parsed_url.netloc)
@staticmethod
def extract_links(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
links = []
for link in soup.find_all('a'):
href = link.get('href')
if href:
absolute_url = urljoin(url, href)
if URLHandler.is_valid_url(absolute_url):
links.append(absolute_url)
return links
@staticmethod
def extract_links_from_websites(websites):
all_links = []
for website in websites:
links = URLHandler.extract_links(website)
all_links.extend(links)
return all_links
class DocumentLoaderFactory:
@staticmethod
def get_loader(file_path_or_url):
if file_path_or_url.startswith("http://") or file_path_or_url.startswith("https://"):
handle_website = URLHandler()
return WebBaseLoader(handle_website.extract_links_from_websites([file_path_or_url]))
else:
mime_type, _ = mimetypes.guess_type(file_path_or_url)
if mime_type == 'application/pdf':
return PyPDFLoader(file_path_or_url)
elif mime_type == 'text/csv':
return CSVLoader(file_path_or_url)
elif mime_type in ['application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document']:
return UnstructuredWordDocumentLoader(file_path_or_url)
else:
raise ValueError(f"Unsupported file type: {mime_type}")
class PineconeIndexManager:
def __init__(self, pinecone_manager, index_name):
self.pinecone_manager = pinecone_manager
self.index_name = index_name
def index_exists(self):
active_indexes = self.pinecone_manager.list_indexes()
return self.index_name in active_indexes
def create_index(self, dimension, metric):
self.pinecone_manager.create_index(self.index_name, dimension, metric)
def delete_index(self):
self.pinecone_manager.delete_index(self.index_name)
def train_or_load_model(train, pinecone_index_manager, file_path, name_space):
if train:
loader = DocumentLoaderFactory.get_loader(file_path)
pages = loader.load_and_split()
if pinecone_index_manager.index_exists():
print("Updating the model")
pinecone_index = Pinecone.from_documents(pages, embeddings, index_name=pinecone_index_manager.index_name,
namespace=name_space)
else:
print("Training the model")
pinecone_index_manager.create_index(dimension=1531, metric="cosine")
pinecone_index = Pinecone.from_documents(documents=pages, embedding=embeddings,
index_name=pinecone_index_manager.index_name,
namespace=name_space)
return pinecone_index
else:
pinecone_index = Pinecone.from_existing_index(index_name=pinecone_index_manager.index_name,
namespace=name_space, embedding=embeddings)
return pinecone_index
def answer_questions(pinecone_index):
messages = [
SystemMessage(
content='I want you to act as a document that I am having a conversation with. Your name is "AI '
'Assistant". You will provide me with answers from the given info. If the answer is not included, '
'say exactly "Hmm, I am not sure." and stop after that. Refuse to answer any question not about '
'the info. Never break character.')
]
while True:
question = input("Ask a question (type 'stop' to end): ")
if question.lower() == "stop":
break
docs = pinecone_index.similarity_search(query=question, k=1)
main_content = question + "\n\n"
for doc in docs:
main_content += doc.page_content + "\n\n"
messages.append(HumanMessage(content=main_content))
ai_response = chat(messages).content
messages.pop()
messages.append(HumanMessage(content=question))
messages.append(AIMessage(content=ai_response))
print(ai_response)
def main():
pinecone_manager = PineconeManager(PINECONE_API_KEY, PINECONE_ENVIRONMENT)
pinecone_index_manager = PineconeIndexManager(pinecone_manager, PINECONE_INDEX_NAME)
file_path = "data/motor1.pdf"
name_space = "insurance"
train = int(input("Do you want to train the model? (1 for yes, 0 for no): "))
pinecone_index = train_or_load_model(train, pinecone_index_manager, file_path, name_space)
answer_questions(pinecone_index)
if __name__ == "__main__":
main()