# `Imp.Predict.RAG`
[🔗](https://github.com/deepfates/imp/blob/v0.18.1/lib/imp/predict/rag.ex#L1)

Retrieval-augmented program wrapper.

`RAG` composes an ordinary Imp program with a retriever. On each call it
retrieves documents for the input query, writes a rendered context field into
the program inputs, calls the wrapped program, and attaches retrieval metadata
to the returned prediction. Set `hops: 2` or higher for iterative multi-hop
retrieval: each hop expands the original query with previously retrieved
passages before retrieving again.

Use RAG when retrieval is part of the program, not when a caller has already
prepared all context. The wrapped program remains an ordinary Imp executable
module, so it can still be evaluated, optimized, streamed, and saved when the
retriever is portable.

# `call`

Runs retrieval, injects context, calls the wrapped program, and records metadata.

    iex> lm = Imp.LM.Static.new(handler: fn messages, _opts ->
    ...>   prompt = Enum.map_join(messages, " ", & &1.content)
    ...>   if prompt =~ "France has capital Paris", do: %{answer: "Paris"}, else: %{answer: "unknown"}
    ...> end)
    iex> base = Imp.Predict.new("question, context -> answer", lm: lm)
    iex> retriever = Imp.Retrieve.Memory.new([%{text: "France has capital Paris"}], k: 1)
    iex> rag = Imp.Predict.RAG.new(base, retriever, k: 1)
    iex> {:ok, prediction} = Imp.Predict.RAG.call(rag, %{question: "capital France"})
    iex> {Imp.Prediction.get(prediction, :answer), prediction.metadata.retrieval.count}
    {"Paris", 1}

# `new`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
