I’m trying to build a RAG using Ollama, Pyspark. I wish to use my delta table as the source for this POC. Here’s what I have done so far
import pyspark
from delta import *
from langchain_community.document_loaders import PySparkDataFrameLoader
from langchain_community.llms import Ollama
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import OllamaEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import RetrievalQA
from langchain.text_splitter import CharacterTextSplitter
ollama_llm = Ollama(model="phi")
ollama_emb = OllamaEmbeddings(model="mxbai-embed-large")
# Read and Create df
builder = pyspark.sql.SparkSession.builder.appName("DeltaTutorial")
.config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension")
.config("spark.sql.catalog.spark_catalog", "org.apache.spark.sql.delta.catalog.DeltaCatalog")
spark = configure_spark_with_delta_pip(builder).getOrCreate()
df = spark.createDataFrame(
[
(1, "foo", "hello"),
(2, "bar", "world"),
(3, "baz", "!!!"),
(4, "000", "why"),
(5, "111", "who"),
],
["id", "label", "col3"]
)
# load documents
loader = PySparkDataFrameLoader(spark, df, page_content_column="id")
documents = loader.load()
print(documents)
#[Document(page_content='1', metadata={'label': 'foo', 'col3': 'hello'}), Document(page_content='2', metadata={'label': 'bar', 'col3': 'world'}), Document(page_content='3', metadata={'label': 'baz', 'col3': '!!!'}), Document(page_content='4', metadata={'label': '000', 'col3': 'why'}), Document(page_content='5', metadata={'label': '111', 'col3': 'who'})]
print(f"Number of documents: {len(documents)}")
#5
# Split and chunk documents
text_splitter = CharacterTextSplitter(chunk_size=50, chunk_overlap=0, length_function=len)
chunks = text_splitter.split_documents(documents)
print(f"Number of chunks: {len(chunks)}")
#5
for i, _ in enumerate(chunks):
print(f"chunk # {i} :: {chunks[i]}")
#chunk # 0 :: page_content='1' metadata={'label': 'foo', 'col3': 'hello'}
#chunk # 1 :: page_content='2' metadata={'label': 'bar', 'col3': 'world'}
#chunk # 2 :: page_content='3' metadata={'label': 'baz', 'col3': '!!!'}
#chunk # 3 :: page_content='4' metadata={'label': '000', 'col3': 'why'}
#chunk # 4 :: page_content='5' metadata={'label': '111', 'col3': 'who'}
# Embedding
db = FAISS.from_documents(documents, ollama_emb)
retrieval_qa = RetrievalQA.from_chain_type(llm=ollama_llm
, chain_type="stuff"
, retriever=db.as_retriever()
, return_source_documents=True)
# Retrieve response.
query = "what is the label for col3 who"
result = retrieval_qa({"query": query})
print(result)
#{'query': 'what is the label for col3 who', 'result': " I'm sorry, but I cannot determine the label for col3 based on the given information. Please provide more details or context about col3.n", 'source_documents': [Document(page_content='5', metadata={'label': '111', 'col3': 'who'}), Document(page_content='4', metadata={'label': '000', 'col3': 'why'}), Document(page_content='2', metadata={'label': 'bar', 'col3': 'world'}), Document(page_content='3', metadata={'label': 'baz', 'col3': '!!!'})]}
print("Result:", result["result"])
#Result: I'm sorry, but I cannot determine the label for col3 based on the given information. Please provide more details or context about col3.
Although the df
looks simple, yet the RetrievalQA
is unable to produce the correct output.
My questions are:
- Am I missing something here? How do I get my RAG to produce the correct result
- CharacterTextSplitter does not seem to be splitting the document into chunks. How do I split the Document into chunks? I have tried playing around with
chunk_size
. No matter what, I always end up with 5 chunks. - What could be the reason for not getting the correct Results for my simple question?
Below are my App Specs. I chose Phi due to laptop limitations. I’m on Mac 8GB. Unfortunately, the LLama2 model doxed my laptop
Tool | Values |
---|---|
LLM | Ollama (Phi) |
Embedding | mxbai-embed-large |
VectorDB | FAISS |
Langchain | 0.2.3 |
Python | 3.9 |
Pyspark | 3.5.1 |
Any insights on this or directions to guide me will be helpful.