, I’ve written quite a bit about RAG, beginning with the Hitchhiker’s Information to RAG with ChatGPT API and LangChain, after which exploring varied matters associated to RAG and AI, like chunking, hybrid search, reranking, contextual retrieval, and a three-part collection on evaluating retrieval high quality. In different phrases, we now have lined a number of floor on the RAG aspect of issues.
What we haven’t talked about as explicitly is the opposite main approach folks attain for after they wish to enhance an LLM app for a selected area. That’s fine-tuning. And specifically, we now have not talked about what occurs once you put the 2 aspect by aspect and check out to determine which one you really want.
Should you search “RAG vs fine-tuning” on-line, you’ll discover a number of content material that treats this as a contest with a winner. For some, RAG wins as a result of it’s cheaper to arrange, for some others, fine-tuning wins as a result of it produces higher outcomes, and so forth. The issue with this framing is that it’s essentially deceptive, since RAG and fine-tuning aren’t competing strategies, however reasonably strategies that clear up totally different issues at totally different layers of an AI utility. Understanding what each truly does is the prerequisite for making a very good determination.
So, let’s have a look!
🍨 DataCream is a publication about AI, knowledge, and tech. If you’re occupied with these matters, subscribe right here!
What’s RAG and what does it truly do?
When you’ve got adopted this collection, you have already got a strong instinct for RAG. However let’s state it yet another time, as a result of the exact definition issues for the comparability with fine-tuning that follows.
So, RAG, or Retrieval-Augmented Technology, is a way that enhances an LLM’s response by retrieving related exterior info at inference time and injecting it into the immediate. The mannequin itself isn’t modified in any method. What modifications is what it sees as enter.
The pipeline seems one thing like this:
- Firstly, exterior paperwork (the data base we wish to make the most of) are processed into vector embeddings and saved in a vector database.
- When a consumer submits a question, the question can also be transformed to an embedding, and essentially the most semantically comparable doc chunks are retrieved from the database.
- These chunks are then handed to the LLM together with the consumer’s question, so the mannequin can generate a response grounded in that particular retrieved context.
And that’s it.
Here’s a minimal RAG instance utilizing the OpenAI API:
from openai import OpenAI
import numpy as np
consumer = OpenAI(api_key="your_api_key")
# our tiny data base
paperwork = [
"pialgorithms is an AI-powered document management platform.",
"pialgorithms allows teams to search, extract, and automate document workflows.",
"pialgorithms was founded in Athens, Greece.",
]
# embed the data base
def embed(texts):
response = consumer.embeddings.create(
mannequin="text-embedding-3-small",
enter=texts
)
return [r.embedding for r in response.data]
doc_embeddings = embed(paperwork)
# embed the consumer question and retrieve essentially the most related chunk
question = "The place is pialgorithms based mostly?"
query_embedding = embed([query])[0]
# cosine similarity
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
similarities = [cosine_similarity(query_embedding, doc_emb) for doc_emb in doc_embeddings]
best_match = paperwork[np.argmax(similarities)]
# inject retrieved context into the immediate
response = consumer.chat.completions.create(
mannequin="gpt-4o-mini",
messages=[
{
"role": "system",
"content": f"Answer the user's question using only the following context:nn{best_match}"
},
{
"role": "user",
"content": query
}
]
)
print(response.selections[0].message.content material)
# pialgorithms relies in Athens, Greece.
Let’s take a second to grasp what is admittedly occurring right here. After all, the mannequin has no thought what pialgorithms is from its coaching, however as a result of we retrieved the correct doc chunk and injected it into the immediate, the mannequin is ready to reply precisely. The data comes from exterior the mannequin, in the mean time of the question, and the mannequin itself is untouched.
That is the core of what RAG does: it provides the mannequin entry to exterior data it was by no means skilled on, dynamically, at inference time.
And based mostly on the best way it really works, RAG does properly on particular varieties of duties, as as an illustration:
- Answering questions on paperwork, data bases, or knowledge that the mannequin has by no means seen
- Staying updated with out retraining, for the reason that data base could be independently up to date at any time
- Offering traceable, citable solutions, since you understand precisely which doc chunk was retrieved
- Dealing with non-public or proprietary info safely, with out together with that info within the mannequin
On the flip aspect, here’s what RAG received’t do: it’s not going to alter the mannequin’s behaviour, tone, reasoning type, or job efficiency. In case your mannequin tends to be verbose, RAG received’t make it extra concise. If it struggles with a specific output format, RAG is not going to repair that.
What’s fine-tuning and what does it truly do?
High-quality-tuning is the method of taking a pre-trained mannequin and persevering with to coach it on a brand new, task-specific dataset, updating its weights within the course of. To place it otherwise, whereas RAG modifications the inputs of the mannequin, fine-tuning modifications the mannequin itself.
Extra particularly, a base mannequin like GPT-4o-mini is pre-trained on a large common dataset. High-quality-tuning takes that mannequin and runs a further, shorter coaching loop on particular examples related to our particular use case. These examples are usually within the type of input-output pairs. On this method, the mannequin’s weights are adjusted in order that it produces outputs that look extra like these instance pairs.
Here’s what a fine-tuning job would appear like utilizing the OpenAI API:
from openai import OpenAI
import json
consumer = OpenAI(api_key="your_api_key")
# Step 1: put together coaching knowledge as a JSONL file
# every instance is a dialog with a desired output
training_examples = [
{
"messages": [
{"role": "system", "content": "You are a concise technical assistant. Always respond in one sentence."},
{"role": "user", "content": "What is a vector database?"},
{"role": "assistant", "content": "A vector database stores and retrieves data as high-dimensional numerical vectors, enabling fast semantic similarity search."}
]
},
{
"messages": [
{"role": "system", "content": "You are a concise technical assistant. Always respond in one sentence."},
{"role": "user", "content": "What is chunking in RAG?"},
{"role": "assistant", "content": "Chunking is the process of splitting large documents into smaller pieces before embedding them, so they fit within model context limits and improve retrieval precision."}
]
},
# in apply you'll need at the very least 50-100 examples
]
# save as JSONL
with open("training_data.jsonl", "w") as f:
for instance in training_examples:
f.write(json.dumps(instance) + "n")
# add the coaching file
with open("training_data.jsonl", "rb") as f:
training_file = consumer.recordsdata.create(file=f, objective="fine-tune")
# create the fine-tuning job
fine_tune_job = consumer.fine_tuning.jobs.create(
training_file=training_file.id,
mannequin="gpt-4o-mini-2024-07-18"
)
print(fine_tune_job.id)
As soon as the fine-tuning job completes, OpenAI returns a novel mannequin identifier in your newly fine-tuned mannequin, within the format ft:base-model:your-org:your-suffix:unique-id. That is now a definite mannequin that lives in your OpenAI account, separate from the bottom gpt-4o-mini.
On print, we’d get again an id for that fine-tuned mannequin, trying one thing like this:
ft:gpt-4o-mini-2024-07-18:your-org:your-suffix:abc123
We are able to then name it precisely like every other mannequin, simply by passing that identifier within the mannequin parameter:
# as soon as the job is full, use the fine-tuned mannequin
response = consumer.chat.completions.create(
mannequin="ft:gpt-4o-mini-2024-07-18:your-org:your-suffix:abc123",
messages=[
{"role": "user", "content": "What is prompt caching?"}
]
)
print(response.selections[0].message.content material)
The distinction is that this mannequin has already internalised the behaviour we skilled it on: in our instance, it is going to now persistently reply in a single concise sentence, with out us having to instruct it to take action in each system immediate. That’s the sort of factor fine-tuning is genuinely good at: constant formatting, particular tone, adherence to a specific output construction, or improved efficiency on a really particular job sort. And that, in essence, is what fine-tuning does.
Discover how fine-tuning doesn’t have any impression on incorporating particular info within the mannequin. In contrast to what one would possibly intuitively assume, fine-tuning a mannequin in your firm’s paperwork received’t make the mannequin “study” that info and be capable of reply questions on it reliably. It could certainly end result within the mannequin memorizing particular information from coaching examples right here and there, however this memorization is brittle and unreliable. Essentially the most possible final result can be a mannequin hallucinating about matters showing within the coaching examples, reasonably than a mannequin precisely recalling particular particulars showing in these examples. Thus, if data retrieval is what you want, RAG is the correct software, not fine-tuning.
Extra particularly, fine-tuning truly does properly on the next:
- “Instructing” the mannequin a constant output format, tone, or type
- Bettering efficiency on a selected, slender job sort (e.g. at all times producing legitimate JSON, at all times summarising in three bullet factors, and so forth)
- Lowering the necessity for lengthy, repetitive system prompts by integrating these directions into the mannequin
- Adapting the mannequin to domain-specific language or terminology, so it understands and makes use of the correct vocabulary
Nonetheless, fine-tuning doesn’t do this properly in:
- Including dependable factual data, the mannequin can recall precisely
- Holding the mannequin updated with altering info
- Offering traceable, citable solutions
So, when can we use every and when can we use each?
Now that we perceive what every approach truly does, the “RAG vs fine-tuning” query turns into a lot simpler to reply, as a result of typically it’s not actually a “vs” sort of query in any respect.
RAG and fine-tuning function at totally different layers of an AI utility. RAG operates on the data layer, that means it controls what info the mannequin has entry to. On the flip aspect, fine-tuning operates on the behaviour layer, that means it defines the best way the mannequin processes the supplied info and generates responses. These two layers are unbiased of one another, which suggests you should utilize RAG, fine-tuning, or each, relying on what you are attempting to attain.
So, here’s a sensible determination framework for deciding what to make use of:

The state of affairs the place we use each RAG and fine-tuning is definitely the most typical in actual manufacturing programs. The only approach to preserve the 2 straight is that this: fine-tune for behaviour, use RAG for data.
Think about, for instance, we’re constructing a buyer help assistant for a software program product, and we want it to:
- All the time reply in a selected tone and format, in keeping with our software program model
- Have correct, up-to-date data of our product’s documentation
For such a job, we would wish to make the most of each RAG and fine-tuning. Particularly, fine-tuning would deal with the primary requirement by permitting the mannequin to study from examples of preferrred buyer help responses, educating it the correct tone, the correct degree of element, and the correct output format. The second requirement can be lined by RAG: at inference time, essentially the most related info from the product’s documentation is retrieved and injected into the immediate, permitting the mannequin to offer dependable solutions grounded within the documentation.
So, in apply, we will mix each fine-tuning and RAG by calling a fine-tuned mannequin the identical method we’d name every other mannequin, but in addition inject retrieved context into the system immediate, precisely as we’d do in a normal RAG pipeline.
# combining fine-tuned mannequin with RAG
response = consumer.chat.completions.create(
mannequin="ft:gpt-4o-mini-2024-07-18:your-org:support-style:abc123", # fine-tuned for tone/format
messages=[
{
"role": "system",
"content": f"You are a helpful support assistant for pialgorithms. "
f"Use only the following documentation to answer:nn{retrieved_context}" # RAG context
},
{
"role": "user",
"content": user_question
}
]
)
High-quality-tuning makes the mannequin know easy methods to appropriately reply, and RAG tells it what to say. Thus, this isn’t a “fine-tuning vs RAG” query, however reasonably fine-tuning and RAG complement each other and do various things.
On my thoughts
What I discover most attention-grabbing concerning the RAG vs fine-tuning debate is how typically it’s framed as a query about which approach is best, when the extra helpful query is what drawback you might be truly making an attempt to unravel.
RAG and fine-tuning deal with totally different failure modes of a base LLM. If a base mannequin fails as a result of it doesn’t know one thing, that could be a data drawback, and RAG solves it. If a base mannequin fails as a result of it behaves inconsistently or produces outputs within the flawed format, that could be a behaviour drawback, and fine-tuning solves it. In case your mannequin is failing for each causes without delay, you could genuinely want each.
✨ Thanks for studying! ✨
Should you made it this far, you would possibly discover pialgorithms helpful: a platform we’ve been constructing that helps groups securely handle organisational data in a single place.
Liked this put up? Be a part of me on 💌 Substack and 💼 LinkedIn
All pictures by the writer, besides the place talked about in any other case.















