Imp.Retrieve behaviour (Imp v0.5.0)

Copy Markdown View Source

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.

Summary

Types

t()

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

Callbacks

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

Functions

Calls a retriever and normalizes returned documents.

Types

t()

@type t() ::
  struct()
  | module()
  | (String.t(), keyword() -> {:ok, list()} | {:error, term()})

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

Callbacks

retrieve(retriever, query, opts)

@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.

Functions

retrieve(retriever, query, opts \\ [])

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}}