Tuesday, July 14, 2026

RAG vs Superb-Tuning Defined: What They Really Do and When to Use Every


, I’ve written rather a lot 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 have now coated numerous floor on the RAG aspect of issues.

What we haven’t talked about as explicitly is the opposite main approach individuals attain for once they need to enhance an LLM app for a selected area. That’s fine-tuning. And specifically, we have now not talked about what occurs once you put the 2 aspect by aspect and take a look at to determine which one you really want.

In case you search “RAG vs fine-tuning” on-line, you’ll discover numerous 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 should not competing methods, however relatively methods that resolve totally different issues at totally different layers of an AI software. Understanding what each truly does is the prerequisite for making a superb choice.

So, let’s have a look!

🍨 DataCream is a publication about AI, information, and tech. If you’re involved in these matters, subscribe right here!

What’s RAG and what does it truly do?

In case you have adopted this collection, you have already got a stable 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 Era, is a method that enhances an LLM’s response by retrieving related exterior data at inference time and injecting it into the immediate. The mannequin itself will not be modified in any manner. What adjustments is what it sees as enter.

The pipeline appears one thing like this:

  • Firstly, exterior paperwork (the data base we need 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 related 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

shopper = 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 = shopper.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 primarily based?"
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 = shopper.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.decisions[0].message.content material)
# pialgorithms relies in Athens, Greece.

Let’s take a second to know what is admittedly taking place right here. In fact, the mannequin has no concept what pialgorithms is from its coaching, however as a result of we retrieved the suitable doc chunk and injected it into the immediate, the mannequin is ready to reply precisely. The data comes from outdoors the mannequin, in the meanwhile 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 primarily based on the way in which it really works, RAG does effectively on particular varieties of duties, as as an illustration:

  • Answering questions on paperwork, data bases, or information 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 realize precisely which doc chunk was retrieved
  • Dealing with non-public or proprietary data safely, with out together with that data 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?

Superb-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 in another way, whereas RAG adjustments the inputs of the mannequin, fine-tuning adjustments the mannequin itself.

Extra particularly, a base mannequin like GPT-4o-mini is pre-trained on a large common dataset. Superb-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 manner, 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 to be utilizing the OpenAI API:

from openai import OpenAI
import json

shopper = OpenAI(api_key="your_api_key")

# Step 1: put together coaching information 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 not less than 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 = shopper.information.create(file=f, goal="fine-tune")

# create the fine-tuning job
fine_tune_job = shopper.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 to 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 might 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 will then name it precisely like another mannequin, simply by passing that identifier within the mannequin parameter:

# as soon as the job is full, use the fine-tuned mannequin
response = shopper.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.decisions[0].message.content material)

The distinction is that this mannequin has already internalised the behaviour we skilled it on: in our instance, it should 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 type 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 kind. And that, in essence, is what fine-tuning does.

Discover how fine-tuning doesn’t have any impression on incorporating particular data within the mannequin. In contrast to what one may intuitively assume, fine-tuning a mannequin in your firm’s paperwork received’t make the mannequin “be taught” that data and have the ability to reply questions on it reliably. It could certainly outcome within the mannequin memorizing particular info from coaching examples right here and there, however this memorization is brittle and unreliable. Essentially the most possible end result can be a mannequin hallucinating about matters showing within the coaching examples, relatively than a mannequin precisely recalling particular particulars showing in these examples. Thus, if data retrieval is what you want, RAG is the suitable device, not fine-tuning.

Extra particularly, fine-tuning truly does effectively on the next:

  • “Instructing” the mannequin a constant output format, tone, or type
  • Bettering efficiency on a selected, slim job kind (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 suitable vocabulary

Nonetheless, fine-tuning doesn’t try this effectively in:

  • Including dependable factual data, the mannequin can recall precisely
  • Maintaining the mannequin updated with altering data
  • 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” kind of query in any respect.

RAG and fine-tuning function at totally different layers of an AI software. RAG operates on the data layer, which means it controls what data the mannequin has entry to. On the flip aspect, fine-tuning operates on the behaviour layer, which means it defines the way in which the mannequin processes the offered data and generates responses. These two layers are unbiased of one another, which implies you should use RAG, fine-tuning, or each, relying on what you are attempting to attain.

So, here’s a sensible choice framework for deciding what to make use of:

The situation the place we use each RAG and fine-tuning is definitely the most typical in actual manufacturing methods. The only option to hold the 2 straight is that this: fine-tune for behaviour, use RAG for data.

Think about, for instance, we’re constructing a buyer assist assistant for a software program product, and we’d like it to:

  1. All the time reply in a selected tone and format, per our software program model
  2. 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 be taught from examples of ideally suited buyer assist responses, educating it the suitable tone, the suitable degree of element, and the suitable output format. The second requirement can be coated by RAG: at inference time, essentially the most related data 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 are able to mix each fine-tuning and RAG by calling a fine-tuned mannequin the identical manner we might name another mannequin, but additionally inject retrieved context into the system immediate, precisely as we might do in a normal RAG pipeline.

# combining fine-tuned mannequin with RAG
response = shopper.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
        }
    ]
)

Superb-tuning makes the mannequin know the best way to appropriately reply, and RAG tells it what to say. Thus, this isn’t a “fine-tuning vs RAG” query, however relatively fine-tuning and RAG complement each other and do various things.

On my thoughts

What I discover most fascinating in regards to the RAG vs fine-tuning debate is how usually 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 resolve.

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 unsuitable 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! ✨


In case you made it this far, you may discover pialgorithms helpful: a platform we’ve been constructing that helps groups securely handle organisational data in a single place.


Cherished this put up? Be part of me on 💌 Substack and 💼 LinkedIn


All photos by the creator, besides the place talked about in any other case.

Related Articles

Latest Articles