VaibhavBhinge commited on
Commit
212618d
·
1 Parent(s): 247d084

removed ingest.py and updates this file code in direclty in rag file and removed filed preocessor

Browse files
Files changed (1) hide show
  1. backend/Rag.py +107 -0
backend/Rag.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from langchain_chroma import Chroma
4
+ from dotenv import load_dotenv
5
+ load_dotenv()
6
+ from langchain_community.document_loaders import DirectoryLoader, TextLoader
7
+ from langchain_huggingface import HuggingFaceEndpointEmbeddings
8
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
9
+
10
+ embedding_model=HuggingFaceEndpointEmbeddings(model="sentence-transformers/all-MiniLM-L6-v2")
11
+ load_dotenv()
12
+
13
+ # 2. Use same HuggingFace embeddings
14
+ # Note: Switched to HuggingFaceEmbeddings for local model execution
15
+ embedding_model = HuggingFaceEndpointEmbeddings(model="sentence-transformers/all-MiniLM-L6-v2")
16
+ #path configuration
17
+
18
+ PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
19
+
20
+ #DATA_DIR="./knowedge_base"
21
+ #CHROMA_DIR="./croma_db"
22
+
23
+ DATA_DIR = os.path.join(PROJECT_ROOT, "knowedge_base")
24
+ CHROMA_DIR= os.path.join(PROJECT_ROOT, "croma_db")
25
+
26
+
27
+ print("dirrrrrrrrrrrrrrrrrrrrrrrrrr",DATA_DIR)
28
+ print("cromammmmmmmmmmmmmmmm",CHROMA_DIR)
29
+
30
+ # text loading
31
+ def cunking_docs():
32
+ print("loading documnet fom knowedgebase:")
33
+ loader = DirectoryLoader(DATA_DIR, glob="**/*.txt", loader_cls=TextLoader)
34
+ # documents = loader.load()
35
+ # if not documents:
36
+ # print(" Add documnet to knowedge base")
37
+ # return
38
+ # print(f"Loaded doc length of doc: {len(documents)}")
39
+ # # text chunking
40
+ # print( "creating splitters:")
41
+ # text_splitter=RecursiveCharacterTextSplitter(chunk_size=500,chunk_overlap=40)
42
+ # chunks=text_splitter.split_documents(documents)
43
+ # print(f"Split into lenght: {len(chunks)} chunks.")
44
+
45
+ # # storing chunks in vector db by creating embedding
46
+ # print("embedding the chunks and storing in CromaDB: ")
47
+ # vectorstore=Chroma.from_documents(
48
+ # documents=chunks,
49
+ # embedding=embedding_model,
50
+ # persist_directory=CHROMA_DIR
51
+ # )
52
+ # print(f"stroing verctor at{CHROMA_DIR}: ")
53
+ documents = loader.load()
54
+
55
+ print("Documents loaded:", len(documents))
56
+
57
+ for doc in documents:
58
+ print("File:", doc.metadata)
59
+
60
+ if not documents:
61
+ print("No documents found!")
62
+
63
+ # text chunking
64
+ print( "creating splitters:")
65
+ text_splitter=RecursiveCharacterTextSplitter(chunk_size=500,chunk_overlap=40)
66
+ chunks=text_splitter.split_documents(documents)
67
+ print(f"Split into lenght: {len(chunks)} chunks.")
68
+
69
+ chunks = text_splitter.split_documents(documents)
70
+
71
+ print("Chunks created:", len(chunks))
72
+
73
+ vectorstore = Chroma.from_documents(
74
+ documents=chunks,
75
+ embedding=embedding_model,
76
+ persist_directory=CHROMA_DIR
77
+ )
78
+
79
+ print("Stored documents:", vectorstore._collection.count())
80
+
81
+ # 1. Force the absolute path to the root folder
82
+
83
+
84
+ # 4. Expose a function: get_retriever()
85
+ def get_retriever():
86
+ print(f"Loading ChromaDB from {CHROMA_DIR} and creating retriever...")
87
+
88
+ vectorstore = Chroma(
89
+ embedding_function=embedding_model,
90
+ persist_directory=CHROMA_DIR
91
+ )
92
+
93
+ # 3. Create retriever (k=3 most relevant chunks)
94
+ retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
95
+
96
+ return retriever
97
+
98
+ if __name__ == "__main__":
99
+ cunking_docs() # Create embeddings and store in Chroma
100
+
101
+ print("Testing the RAG retriever...")
102
+ test_retriever = get_retriever()
103
+
104
+ test_query = "What is your return policy?"
105
+ results = test_retriever.invoke(test_query)
106
+
107
+ print("Results found:", len(results))