vsubu1 commited on
Commit
d143d9e
·
1 Parent(s): 9ddea88

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +73 -0
utils.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
2
+ from langchain.vectorstores import Pinecone
3
+ from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
4
+ import pinecone
5
+ import asyncio
6
+ from langchain.document_loaders.sitemap import SitemapLoader
7
+
8
+ import ab2
9
+
10
+ #Function to fetch data from website
11
+ #https://python.langchain.com/docs/modules/data_connection/document_loaders/integrations/sitemap
12
+ def get_website_data(sitemap_url):
13
+
14
+ loop = asyncio.new_event_loop()
15
+ asyncio.set_event_loop(loop)
16
+ loader = SitemapLoader(
17
+ sitemap_url
18
+ )
19
+
20
+ docs = loader.load()
21
+
22
+ return docs
23
+
24
+ #Function to split data into smaller chunks
25
+ def split_data(docs):
26
+
27
+ text_splitter = RecursiveCharacterTextSplitter(
28
+ chunk_size = 1000,
29
+ chunk_overlap = 200,
30
+ length_function = len,
31
+ )
32
+
33
+ docs_chunks = text_splitter.split_documents(docs)
34
+ return docs_chunks
35
+
36
+ #Function to create embeddings instance
37
+ def create_embeddings():
38
+
39
+ embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
40
+ return embeddings
41
+
42
+ #Function to push data to Pinecone
43
+ def push_to_pinecone(pinecone_apikey,pinecone_environment,pinecone_index_name,embeddings,docs):
44
+
45
+ pinecone.init(
46
+ api_key=pinecone_apikey,
47
+ environment=pinecone_environment
48
+ )
49
+
50
+ index_name = pinecone_index_name
51
+ index = Pinecone.from_documents(docs, embeddings, index_name=index_name)
52
+ return index
53
+
54
+ #Function to pull index data from Pinecone
55
+ def pull_from_pinecone(pinecone_apikey,pinecone_environment,pinecone_index_name,embeddings):
56
+
57
+ pinecone.init(
58
+ api_key=pinecone_apikey,
59
+ environment=pinecone_environment
60
+ )
61
+
62
+ index_name = pinecone_index_name
63
+
64
+ index = Pinecone.from_existing_index(index_name, embeddings)
65
+ return index
66
+
67
+ #This function will help us in fetching the top relevent documents from our vector store - Pinecone Index
68
+ def get_similar_docs(index,query,k=2):
69
+
70
+ similar_docs = index.similarity_search(query, k=k)
71
+ return similar_docs
72
+
73
+