# `Imp.Retrieve`
[🔗](https://github.com/deepfates/imp/blob/v0.18.1/lib/imp/retrieve.ex#L1)

Retriever behaviour and dispatch boundary for RAG-style Imp programs.

A retriever is a struct whose module implements this behaviour, such a
module itself, or a two-argument function `(query, opts)`. All retrievers
return `{:ok, docs}` or `{:error, reason}`. Documents are normalized to maps,
so keyword-list documents are accepted while malformed rows fail before they
reach a RAG program.

# `t`

```elixir
@type t() ::
  struct()
  | module()
  | (String.t(), keyword() -&gt; {:ok, list()} | {:error, term()})
```

A retriever: a struct or module implementing this behaviour, or a function.

# `retrieve`

```elixir
@callback retrieve(
  retriever :: struct() | module(),
  query :: String.t(),
  opts :: keyword()
) ::
  {:ok, [map()]} | {:error, term()}
```

Returns the documents for `query`. `retriever` is the struct or the module
as it was given.

# `retrieve`

Calls a retriever and normalizes returned documents.

    iex> retriever = fn query, opts ->
    ...>   {:ok, [[text: "query=" <> query, k: opts[:k]]]}
    ...> end
    iex> Imp.Retrieve.retrieve(retriever, "beam", k: 1)
    {:ok, [%{text: "query=beam", k: 1}]}

    iex> Imp.Retrieve.retrieve(fn _query, _opts -> {:ok, [:bad_doc]} end, "beam")
    {:error, {:invalid_retriever_document, :bad_doc}}

---

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