forked from PallaviPYXEDA/Chat_with_RAG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinecone_utils.py
More file actions
143 lines (109 loc) · 4.63 KB
/
pinecone_utils.py
File metadata and controls
143 lines (109 loc) · 4.63 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
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
import hashlib
import os
import uuid
from typing import List
from pinecone import Pinecone
import openai
import streamlit as st
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
from langchain.llms import OpenAI
from langchain.docstore.document import Document
MODEL = "text-embedding-ada-002"
# API keys
os.environ['OPENAI_API_KEY'] = st.secrets["OPENAI_API_KEY"]
os.environ['LANGCHAIN_HANDLER'] = 'langchain'
os.environ['PINECONE_API_KEY'] = "a8ef31c5-c456-4949-aa77-6d2425a29a04"
os.environ['PINECONE_INDEX_NAME'] = "rag-curriculum"
openai.api_key = os.environ['OPENAI_API_KEY']
pc = Pinecone(api_key = os.environ['PINECONE_API_KEY'])
pinecone_index = pc.Index(os.environ['PINECONE_INDEX_NAME'])
def upload_to_pinecone(text_document: str, file_name, chunk_size: int = 1000 ) -> None:
"""
Upload the text content to pinecone
@params
text_document: text content needs to upload
file_name: name of the filed to be included as metadata
chunk_size: chunk size to split the data
@return
None
"""
MODEL = "text-embedding-ada-002"
text_splitter = RecursiveCharacterTextSplitter(
# Set a really small chunk size, just to show.
chunk_size = chunk_size,
chunk_overlap = 20,
length_function = len,
is_separator_regex = False,
)
# text splitter
texts = text_splitter.create_documents([text_document])
for index, sub_docs in enumerate(texts):
document_hash = hashlib.md5(sub_docs.page_content.encode("utf-8"))
embedding = openai.embeddings.create(model= MODEL,input=sub_docs.page_content).data[0].embedding
metadata = {"doc_name":file_name, "chunk": str(uuid.uuid4()), "text": sub_docs.page_content, "doc_index":index}
pinecone_index.upsert([(document_hash.hexdigest(), embedding, metadata)])
print("{} ==> Done".format(index))
return True
def filter_matching_docs(question: str, top_chunks: int = 3, get_text: bool = False) -> List:
"""
Semnatic search between user content and vector DB
@param
question: user question
top_chunks: number of most similar content ot be filtered
get_text: if True, return only the text not the document
@return
list of similar content
"""
question_embed_call = openai.embeddings.create(input = question ,model = MODEL)
query_embeds = question_embed_call.data[0].embedding
response = pinecone_index.query(vector = query_embeds,top_k = top_chunks,include_metadata = True)
#get the data out
filtered_data = []
filtered_text = []
for content in response["matches"]:
#save the meta data as a dictionary
info = {}
info["id"] = content.get("id", "")
info["score"] = content.get("score", "")
# get the saved metadat info
content_metadata = content.get("metadata","")
# combine it it info
info["filename"] = content_metadata.get("doc_name", "")
info["chunk"] = content_metadata.get("chunk", "")
info["text"] = content_metadata.get("text", "")
filtered_text.append(content_metadata.get("text", ""))
#append the data
filtered_data.append(info)
if get_text:
similar_text = " ".join([text for text in filtered_text])
print(similar_text)
return similar_text
print(filtered_data)
return filtered_data
def QA_with_your_docs(user_question: str, text_list: List[str], chain_type: str = "stuff") -> str:
"""
This is the main function to chat with the content you have
@param
user_question: question or context user wants to figure out
text: list of similar texts
chat_type: Type of chain run (stuff is cost effective)
@return
answers from the LLM
"""
llm = OpenAI(temperature=0, openai_api_key = os.environ['OPENAI_API_KEY'])
chain = load_qa_with_sources_chain(llm, chain_type = chain_type, verbose = False)
all_docs = []
for doc_content in text_list:
metadata = {}
doc_text = doc_content.get("text", "")
metadata["id"] = doc_content.get("id", "")
metadata["score"] = doc_content.get("score", "")
metadata["filename"] = doc_content.get("filename", "")
metadata["chunk"] = doc_content.get("chunk", "")
chunk_name = doc_content.get("filename", "UNKNOWN")
offset=", OFFSET="+str(doc_content.get("chunk","UNKNOWN"))
metadata["source"] = chunk_name + offset
all_docs.append(Document(page_content = doc_text, metadata = metadata))
chain_response = chain.run(input_documents = all_docs, question = user_question )
return chain_response