Spaces:
Sleeping
Sleeping
Aniket Sirsikar commited on
Commit ·
2be51ac
1
Parent(s): 0b6870c
Migrate RAG from LangChain to LlamaIndex
Browse files- requirements.txt +2 -5
- src/rag.py +44 -31
requirements.txt
CHANGED
|
@@ -3,11 +3,8 @@ transformers
|
|
| 3 |
torch
|
| 4 |
torchaudio
|
| 5 |
gTTS
|
| 6 |
-
|
| 7 |
-
|
| 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
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
from
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
-
Loads
|
|
|
|
| 14 |
"""
|
| 15 |
-
global
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
else:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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
|
| 42 |
Returns a combined string of the retrieved context.
|
| 43 |
"""
|
| 44 |
-
global
|
| 45 |
-
|
| 46 |
-
if not
|
| 47 |
return "No knowledge base available."
|
| 48 |
-
|
| 49 |
try:
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 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}")
|