DEV Community

Cover image for RAG vs Fine-Tuning: What Really Solved My AI Challenges
Bernard K
Bernard K

Posted on

RAG vs Fine-Tuning: What Really Solved My AI Challenges

I recently grappled with the choice between Retrieval-Augmented Generation (RAG) and fine-tuning a language model. The project was simple: integrate AI that's reliably intelligent on a budget, across over 2,500 IoT devices distributed in areas where internet connectivity is as steady as a shaky table. My mission was to enable these devices to answer user questions about local climate data,a feature that needed to be useful even when connections were unstable.

RAG: A smart choice

RAG turned out to be a lifesaver in my scenario. Its appeal lay in working well with limited resources while maintaining performance. For those unfamiliar, RAG involves pulling relevant documents from a dataset and generating a response based on that retrieved information. Think of it like a librarian who pulls the right book off the shelf before you even finish asking your question.

Why RAG? Maintaining a large language model locally on budget IoT hardware felt impractical. These devices don't have the processing power or memory for such a task. Streaming a lean model and outsourcing the heavy-lifting to RAG seemed smart and efficient.

How I implemented RAG

I used Haystack, a Python framework that integrates well with RAG. The setup was surprisingly straightforward. Here's a simplified version of the code I used:

from haystack.document_stores import InMemoryDocumentStore
from haystack.nodes import DensePassageRetriever, FARMReader
from haystack.pipelines import ExtractiveQAPipeline

# Initialize document store
document_store = InMemoryDocumentStore()

# Set up retriever and reader
retriever = DensePassageRetriever(document_store=document_store)
reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2")

# Pipeline for QA
pipeline = ExtractiveQAPipeline(reader, retriever)

def run_pipeline(question, documents):
    document_store.write_documents(documents)
    return pipeline.run(query=question)

# Sample output
result = run_pipeline("What's the local climate today?", [{"content": "The climate is sunny with high temperatures."}])
print(result['answers'][0]['answer'])  # Expected: "sunny with high temperatures"
Enter fullscreen mode Exit fullscreen mode

The result was consistent performance despite shaky connectivity. With RAG, not every query required a live internet connection, which drastically reduced latency issues and cut API costs. In numbers, I saw a 50% reduction in unnecessary internet fetches,a big win for us working on a tight budget.

Fine-tuning: an ambitious endeavor

Now, fine-tuning has its appeal. You can tailor a language model to your specific dataset. Sounds great, right? Unfortunately, it's a costly approach if each of your IoT devices has the computational power of a basic calculator.

For the same task, fine-tuning a model was like sending these devices to space without oxygen. Fine-tuning is ideal when constant connectivity is guaranteed or when working with larger cloud setups.

My attempt with fine-tuning

I tried using BERT, a popular choice known for its strong context understanding. With the dataset in hand, I attempted fine-tuning on a pre-trained model using Transformers:

from transformers import BertTokenizer, BertForQuestionAnswering, Trainer, TrainingArguments

# Tokenizer and model initialization
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForQuestionAnswering.from_pretrained('bert-base-uncased')

# A toy dataset split
train_encodings = tokenizer(["What's the weather?"], truncation=True, padding=True)
train_dataset = [{'input_ids': train_encodings['input_ids'][0], 'start_positions': [0], 'end_positions': [3]}]

# Trainer setup
training_args = TrainingArguments(per_device_train_batch_size=1)
trainer = Trainer(model=model, args=training_args, train_dataset=train_dataset)

# Mock training
trainer.train()
Enter fullscreen mode Exit fullscreen mode

Running this on a more robust setup was insightful, but trying to execute similar fine-tuned models on-field failed badly. Any connectivity hiccup meant retrieving data from the cloud sometimes took longer than the questions themselves. Not to mention, the cost wasn't cheap.

The decision

In a perfect world with limitless resources, fine-tuning would be the dream engine of AI. But here, keeping IoT functional in unreliable network zones with budget limitations paved a path where RAG shone like a beacon.

For IoT deployments in regions like Kenya, where budget constraints are as ever-present as the sunsets, RAG is a solid solution. If you're dealing with devices with the processing power of an old Nokia but want a system that performs under challenging conditions, RAG is the way to go.

For now, we're working to optimize RAG's document retrieval efficiency and exploring additional cloud computing solutions for occasional heavy lifting. Tech evolves fast, and staying ahead requires constant adjustment and experimentation.

Top comments (0)