Aniket Sirsikar commited on
Commit
2be51ac
·
1 Parent(s): 0b6870c

Migrate RAG from LangChain to LlamaIndex

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -5
  2. src/rag.py +44 -31
requirements.txt CHANGED
@@ -3,11 +3,8 @@ transformers
3
  torch
4
  torchaudio
5
  gTTS
6
- langchain
7
- langchain-chroma
8
- langchain-huggingface
9
- chromadb
10
  sentence-transformers
11
  huggingface_hub
12
  pydub
13
- langchain-community
 
3
  torch
4
  torchaudio
5
  gTTS
6
+ llama-index
7
+ llama-index-embeddings-huggingface
 
 
8
  sentence-transformers
9
  huggingface_hub
10
  pydub
 
src/rag.py CHANGED
@@ -1,54 +1,67 @@
1
  import os
2
- from langchain_chroma import Chroma
3
- from langchain_huggingface import HuggingFaceEmbeddings
4
- from langchain_community.document_loaders import TextLoader, CSVLoader
5
- from langchain.text_splitter import RecursiveCharacterTextSplitter
 
6
 
7
- # Initialize embeddings
8
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
9
- vector_store = None
10
 
11
- def init_vector_store(data_path="data/faq.txt"):
 
 
12
  """
13
- Loads text data, chunks it, and initializes the local Chroma vector store.
 
14
  """
15
- global vector_store
16
-
17
  if not os.path.exists(data_path):
18
  print(f"Warning: {data_path} not found. Vector store will be empty.")
19
  return
20
-
21
  try:
 
 
22
  if data_path.endswith('.csv'):
23
- loader = CSVLoader(file_path=data_path)
 
 
 
 
 
24
  else:
25
- loader = TextLoader(data_path)
26
-
27
- documents = loader.load()
28
-
29
- # Split text into manageable chunks
30
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
31
- docs = text_splitter.split_documents(documents)
32
-
33
- # Create vector store in memory
34
- vector_store = Chroma.from_documents(docs, embeddings)
35
- print(f"Successfully loaded {len(docs)} chunks into ChromaDB.")
36
  except Exception as e:
37
  print(f"Error initializing vector store: {e}")
38
 
 
39
  def retrieve_context(query, k=3):
40
  """
41
- Retrieves the top k most relevant documents for a given English query.
42
  Returns a combined string of the retrieved context.
43
  """
44
- global vector_store
45
-
46
- if not vector_store:
47
  return "No knowledge base available."
48
-
49
  try:
50
- results = vector_store.similarity_search(query, k=k)
51
- context = "\n\n".join([doc.page_content for doc in results])
 
52
  return context
53
  except Exception as e:
54
  print(f"Error retrieving context: {e}")
 
1
  import os
2
+ from llama_index.core import VectorStoreIndex, Settings
3
+ from llama_index.core.node_parser import SentenceSplitter
4
+ from llama_index.core.schema import Document
5
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
6
+ import csv
7
 
8
+ # Configure LlamaIndex to use a local HuggingFace embedding model (no OpenAI needed)
9
+ Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
10
+ Settings.llm = None # We handle LLM ourselves in src/llm.py
11
 
12
+ index = None
13
+
14
+ def init_vector_store(data_path="data/BankFAQs.csv"):
15
  """
16
+ Loads the banking FAQ data, chunks it with a sentence-aware splitter,
17
+ and builds an in-memory LlamaIndex VectorStoreIndex.
18
  """
19
+ global index
20
+
21
  if not os.path.exists(data_path):
22
  print(f"Warning: {data_path} not found. Vector store will be empty.")
23
  return
24
+
25
  try:
26
+ documents = []
27
+
28
  if data_path.endswith('.csv'):
29
+ with open(data_path, newline='', encoding='utf-8') as f:
30
+ reader = csv.DictReader(f)
31
+ for row in reader:
32
+ # Combine Question + Answer into a single rich document
33
+ text = f"Q: {row.get('Question', '').strip()}\nA: {row.get('Answer', '').strip()}"
34
+ documents.append(Document(text=text))
35
  else:
36
+ with open(data_path, 'r', encoding='utf-8') as f:
37
+ documents.append(Document(text=f.read()))
38
+
39
+ # Sentence-aware splitter: smarter than fixed character chunking
40
+ splitter = SentenceSplitter(chunk_size=256, chunk_overlap=32)
41
+ nodes = splitter.get_nodes_from_documents(documents)
42
+
43
+ # Build the in-memory vector index
44
+ index = VectorStoreIndex(nodes)
45
+ print(f"Successfully loaded {len(nodes)} chunks into LlamaIndex VectorStore.")
46
+
47
  except Exception as e:
48
  print(f"Error initializing vector store: {e}")
49
 
50
+
51
  def retrieve_context(query, k=3):
52
  """
53
+ Retrieves the top k most relevant FAQ chunks for a given English query.
54
  Returns a combined string of the retrieved context.
55
  """
56
+ global index
57
+
58
+ if not index:
59
  return "No knowledge base available."
60
+
61
  try:
62
+ retriever = index.as_retriever(similarity_top_k=k)
63
+ results = retriever.retrieve(query)
64
+ context = "\n\n".join([node.get_content() for node in results])
65
  return context
66
  except Exception as e:
67
  print(f"Error retrieving context: {e}")