All posts

DeepSeek RAG: build a retrieval-augmented pipeline (OpenAI-compatible)

· Tutorial· RAG· DeepSeek

RAG (retrieval-augmented generation) is how you make a model answer from *your* data. You search your documents for relevant chunks, paste them into the prompt, and let the model answer grounded in that context. The generation step is where a cheap, capable model like DeepSeek pays off: most RAG answers don't need a frontier model, they need good retrieval plus a solid, affordable generator.

The pipeline in four steps

  1. Chunk your documents into passages.
  2. Embed and index them in a vector store (any embedding model and vector DB you like).
  3. Retrieve the top passages for a query.
  4. Generate an answer with DeepSeek, grounded in those passages.

This guide focuses on step 4, the part DeepSeek handles, and treats retrieval as a black box (Qdrant, pgvector, FAISS, whatever you use).

The generation step

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["TURILOOP_API_KEY"],
    base_url="https://api.turiloop.com/v1",
)

def answer_with_context(question: str, passages: list[str]) -> str:
    context = "\n\n".join(f"[{i+1}] {p}" for i, p in enumerate(passages))
    system = (
        "Answer using ONLY the context below. "
        "Cite sources as [n]. If the context doesn't contain the answer, say so."
    )
    r = client.chat.completions.create(
        model="deepseek-v4-flash",   # cheap is fine for most RAG
        messages=[
            {"role": "system", "content": system + "\n\nContext:\n" + context},
            {"role": "user", "content": question},
        ],
        temperature=0.2,
    )
    return r.choices[0].message.content

Why DeepSeek V4-Flash for RAG

In RAG the retrieval does the heavy lifting. The model just has to read the passages and write a grounded answer, which is well within V4-Flash's range. At near-zero cost per token, you can run a high-traffic knowledge base for very little. Escalate to deepseek-v4-pro only for questions that need real synthesis or reasoning across many passages.

Two cost tricks

  • Put the system prompt and instructions first, the variable question last, so prompt caching kicks in and you stop paying full price for the same instructions on every call.
  • Keep `temperature` low (0.1–0.3) for factual grounding. You want faithful answers, not creativity.

Put it together

Retrieval from your vector DB plus DeepSeek for generation, all behind one OpenAI-compatible key on Turiloop. Start cheap on V4-Flash and escalate per query when you need to. Pay-as-you-go, international card, no Chinese phone number.

FAQ

Which DeepSeek model is best for RAG? For most RAG, DeepSeek V4-Flash: retrieval does the heavy lifting, so a cheap model that reads passages well is enough. Escalate to DeepSeek V4-Pro only when a query needs synthesis or reasoning across many passages. (See the model-by-model guide.)

Does DeepSeek work with existing RAG frameworks? Yes. Because the API is OpenAI-compatible, LangChain, LlamaIndex and similar tools work by pointing the base URL at Turiloop and setting the model to a DeepSeek id. One key reaches every model.

How do I cut RAG costs? Keep the system prompt and instructions first so prompt caching applies, retrieve fewer but better passages, and default to V4-Flash.