Spaces:
Sleeping
Sleeping
Update retrival.py
Browse files- retrival.py +128 -127
retrival.py
CHANGED
|
@@ -1,127 +1,128 @@
|
|
| 1 |
-
from langchain_community.document_loaders import DirectoryLoader
|
| 2 |
-
from langchain.embeddings import HuggingFaceInstructEmbeddings,HuggingFaceEmbeddings # for embedding task
|
| 3 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter # for converting the large documents into smaller chunks
|
| 4 |
-
from langchain.schema import Document
|
| 5 |
-
from langchain_core.documents import Document
|
| 6 |
-
from langchain_openai import OpenAIEmbeddings
|
| 7 |
-
from langchain_community.vectorstores import Chroma
|
| 8 |
-
import openai
|
| 9 |
-
import openai
|
| 10 |
-
import os
|
| 11 |
-
import shutil
|
| 12 |
-
import uuid
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
os.makedirs(
|
| 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 |
-
# db.
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
from langchain_community.document_loaders import DirectoryLoader
|
| 2 |
+
from langchain.embeddings import HuggingFaceInstructEmbeddings,HuggingFaceEmbeddings # for embedding task
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter # for converting the large documents into smaller chunks
|
| 4 |
+
from langchain.schema import Document
|
| 5 |
+
from langchain_core.documents import Document
|
| 6 |
+
from langchain_openai import OpenAIEmbeddings
|
| 7 |
+
from langchain_community.vectorstores import Chroma
|
| 8 |
+
import openai
|
| 9 |
+
import openai
|
| 10 |
+
import os
|
| 11 |
+
import shutil
|
| 12 |
+
import uuid
|
| 13 |
+
import asyncio # async
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# Configurations
|
| 17 |
+
UPLOAD_FOLDER = "./uploads"
|
| 18 |
+
VECTOR_DB_FOLDER = "./VectorDB"
|
| 19 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 20 |
+
os.makedirs(VECTOR_DB_FOLDER, exist_ok=True)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def load_document(data_path):
|
| 24 |
+
|
| 25 |
+
# Load documents
|
| 26 |
+
loader = DirectoryLoader(data_path, glob="*.*")
|
| 27 |
+
print("loader",loader)
|
| 28 |
+
document = loader.load()
|
| 29 |
+
return document
|
| 30 |
+
|
| 31 |
+
# Creating the chunks of Data from the knowledge
|
| 32 |
+
def split_text(documents: list[Document]):
|
| 33 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 34 |
+
chunk_size = 1000,
|
| 35 |
+
chunk_overlap = 500,
|
| 36 |
+
length_function = len,
|
| 37 |
+
add_start_index=True,
|
| 38 |
+
)
|
| 39 |
+
chunks = text_splitter.split_documents(documents)
|
| 40 |
+
print(f"Split {len(documents)} documents into {len(chunks)} chunks.")
|
| 41 |
+
|
| 42 |
+
return chunks
|
| 43 |
+
|
| 44 |
+
# # Chroma for creating the vector db whcch we will use for the searching relvant data.
|
| 45 |
+
# def save_to_chroma(chunks: list[Document],name: str):
|
| 46 |
+
# print
|
| 47 |
+
# CHROMA_PATH = f"./VectorDB/chroma_{name}"
|
| 48 |
+
# # Clear out the database first.
|
| 49 |
+
# if os.path.exists(CHROMA_PATH):
|
| 50 |
+
# shutil.rmtree(CHROMA_PATH)
|
| 51 |
+
|
| 52 |
+
# # Initialize SBERT embedding function
|
| 53 |
+
# embedding_function = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 54 |
+
# db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
|
| 55 |
+
|
| 56 |
+
# # Add documents and persist the database
|
| 57 |
+
# db.add_documents(chunks)
|
| 58 |
+
# db.persist()
|
| 59 |
+
# # Return the database instance or a success status
|
| 60 |
+
# return db
|
| 61 |
+
|
| 62 |
+
def save_to_chroma(chunks: list[Document], name: str):
|
| 63 |
+
CHROMA_PATH = f"./VectorDB/chroma_{name}"
|
| 64 |
+
|
| 65 |
+
# Clear out the database first
|
| 66 |
+
if os.path.exists(CHROMA_PATH):
|
| 67 |
+
shutil.rmtree(CHROMA_PATH)
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
# Initialize SBERT embedding function
|
| 71 |
+
embedding_function = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 72 |
+
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
|
| 73 |
+
|
| 74 |
+
# Add documents and persist the database
|
| 75 |
+
print("Adding documents to the database...")
|
| 76 |
+
db.add_documents(chunks)
|
| 77 |
+
print("Persisting the database...")
|
| 78 |
+
db.persist()
|
| 79 |
+
print("Database successfully saved.")
|
| 80 |
+
|
| 81 |
+
return db
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print("Error while saving to Chroma:", e)
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
def get_unique_sources(chroma_path):
|
| 87 |
+
# Load the Chroma database
|
| 88 |
+
db = Chroma(persist_directory=chroma_path)
|
| 89 |
+
|
| 90 |
+
# Retrieve all metadata from the database
|
| 91 |
+
metadata_list = db.get()['metadatas']
|
| 92 |
+
|
| 93 |
+
# Extract unique sources from metadata
|
| 94 |
+
unique_sources = {metadata['source'] for metadata in metadata_list if 'source' in metadata}
|
| 95 |
+
return list(unique_sources)
|
| 96 |
+
|
| 97 |
+
def generate_data_store(file_path,db_name):
|
| 98 |
+
CHROMA_PATH = f"./VectorDB/chroma_{db_name}"
|
| 99 |
+
print(f"filepath===>{file_path} db_name =====>{db_name}")
|
| 100 |
+
try:
|
| 101 |
+
documents = load_document(file_path)
|
| 102 |
+
print("Documents loaded successfully.")
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"Error loading documents: {e}")
|
| 105 |
+
return
|
| 106 |
+
|
| 107 |
+
try:
|
| 108 |
+
chunks = split_text(documents)
|
| 109 |
+
print(f"Text split into {len(chunks)} chunks.")
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"Error splitting text: {e}")
|
| 112 |
+
return
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
asyncio.run(save_to_chroma(chunks, db_name))
|
| 116 |
+
print(f"Data saved to Chroma for database {db_name}.")
|
| 117 |
+
except Exception as e:
|
| 118 |
+
print(f"Error saving to Chroma: {e}")
|
| 119 |
+
return
|
| 120 |
+
# def main():
|
| 121 |
+
# data_path = "H:\\DEV PATEL\\RAG Project\\data1"
|
| 122 |
+
# db_name = "Product_data"
|
| 123 |
+
# generate_data_store(data_path,db_name)
|
| 124 |
+
|
| 125 |
+
# if __name__ == "__main__":
|
| 126 |
+
# main()
|
| 127 |
+
|
| 128 |
+
|