跳到正文
DEV Community· Juan Carlos Isaza·· 2 小時前AI 評分36

RAG 解説:如何讓 LLM 使用你的專屬資訊

RAG explained: how to give an LLM your own information | Xiliux Blog

AI 導讀

RAG(檢索增強生成)會在收到問題時搜尋相關文件片段,連同問題提供給 LLM,讓模型根據可控的實際內容作答,而非只依賴既有知識。文件切分、嵌入向量和向量資料庫都會影響效果;更新文件即可改善助理,無須重新訓練,並可引用資料來源以供核實。

正文

Juan Carlos Isaza

A language model knows a great deal about the world, but knows nothing about your company: your manuals, your policies, your products. And if you ask it about something it doesn't know, it may invent an answer that sounds convincing. RAG (Retrieval-Augmented Generation) solves both problems.

The idea

Instead of expecting the model to "already know" your information, you give it to it at the moment of the question:

  1. Index your documents: split them into fragments and turn them into embeddings (numeric vectors that capture the meaning).
  2. When a question comes in, search for the fragments most similar to that question.
  3. Pass the LLM the question together with those fragments and ask it to answer using them.

The model no longer guesses: it answers from a real context that you control. And you can ask it to cite where each fact came from.

The pattern in Python

# 1. Indexado (una vez): fragmentos -> embeddings -> base vectorial
#    (con librerías como sentence-transformers + FAISS, o un servicio gestionado)

# 2. En cada pregunta: recuperar los fragmentos relevantes
fragmentos = base_vectorial.buscar(pregunta, k=4)
contexto = "\n\n".join(fragmentos)

# 3. Generar la respuesta con el contexto
from anthropic import Anthropic
client = Anthropic()

resp = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    system="Responde SOLO con la información del contexto. Si no está, dilo.",
    messages=[{
        "role": "user",
        "content": f"Contexto:\n{contexto}\n\nPregunta: {pregunta}",
    }],
)
print(resp.content[0].text)

Note the instruction in the system prompt: asking it to answer only with the context and to admit when it doesn't know is what drastically reduces hallucinations.

What is it good for?

  • Support assistants that answer with your real documentation.
  • Internal search in natural language over your manuals or wikis.
  • Onboarding: a chatbot that knows your processes.

Details that make the difference

  • How you split the documents (chunking) has a big effect on quality.
  • Good embeddings and a suitable vector database matter more than the model.
  • Citing the sources builds trust and allows verification.

RAG is today one of the most cost-effective ways to apply AI in a business: you retrain nothing, and the assistant improves just by updating your documents.

Want an assistant with your data?

At Xiliux I build RAG systems in Python from end to end. Tell me what information you want to put to work: contact.

來源:DEV Community · dev.to