Packages
langchain
0.13.0
0.16.1
0.16.0
0.15.9
0.15.8
0.15.7
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.0
0.9.7
0.9.6
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.14
0.8.13
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.4.0-rc.3
0.4.0-rc.2
0.4.0-rc.1
0.4.0-rc.0
0.3.3
0.3.2
0.3.1
0.3.0
0.3.0-rc.2
0.3.0-rc.1
0.3.0-rc.0
0.2.0
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Elixir implementation of a LangChain style framework that lets Elixir projects integrate with and leverage LLMs.
Current section
Files
Jump to
Current section
Files
lib/callbacks.ex
defmodule LangChain.Callbacks do
@moduledoc """
Defines the structure of callbacks and provides utilities for executing them.
See `LangChain.Chains.ChainCallbacks` for the list of callbacks that can be
used.
"""
alias LangChain.LangChainError
@doc """
Fire a named callback with the list of arguments to pass. Takes a list of
callback handlers and will execute the callback for each handler that defines
a handler function for it.
"""
@spec fire([map()], atom(), [any()]) :: :ok | no_return()
def fire(callbacks, callback_name, arguments)
def fire(callbacks, :on_llm_new_message, [messages]) when is_list(messages) do
Enum.each(messages, fn m ->
fire(callbacks, :on_llm_new_message, [m])
end)
end
def fire(callbacks, callback_name, arguments) when is_list(callbacks) do
# A model may contain multiple callback handler maps. Every handler gets the
# same arguments and whatever it returns is discarded, which is `reduce/4`
# with nothing to accumulate.
reduce(callbacks, callback_name, :ok, fn invoke, acc ->
invoke.(arguments)
{:cont, acc}
end)
end
@doc """
Fold a decision-returning callback across the attached handler maps.
Where `fire/3` discards what a handler returns, this collects it. A handler
map that does not define `callback_name` is skipped without consulting the
reducer.
`reducer` receives two arguments: an `invoke` function that applies the handler
to a list of arguments, and the current accumulator. It returns `{:cont, acc}`
to consult the next handler or `{:halt, acc}` to stop. Passing `invoke` rather
than the raw handler lets the reducer choose the arguments per handler, so a
handler can be shown what an earlier one changed, while the exception wrapping
stays here.
This is the primitive the module is built on. `fire/3` is this function with
the same arguments given to every handler and nothing accumulated.
## Example
Callbacks.reduce(callbacks, :on_thing_reviewed, :allowed, fn invoke, acc ->
case invoke.([subject]) do
:ok -> {:cont, acc}
{:denied, _reason} = denial -> {:halt, denial}
end
end)
"""
@spec reduce([map()], atom(), acc, (([any()] -> any()), acc -> {:cont, acc} | {:halt, acc})) ::
acc
when acc: term()
def reduce(callbacks, callback_name, acc, reducer)
when is_list(callbacks) and is_function(reducer, 2) do
Enum.reduce_while(callbacks, acc, fn handlers_map, acc ->
case Map.get(handlers_map, callback_name) do
nil ->
{:cont, acc}
callback_fn when is_function(callback_fn) ->
reducer.(invoker(callback_name, callback_fn), acc)
other ->
raise LangChainError,
"Unexpected callback handler. Callback #{inspect(callback_name)} was assigned #{inspect(other)}"
end
end)
end
defp invoker(callback_name, callback_fn) do
fn arguments ->
try do
apply(callback_fn, arguments)
rescue
err ->
raise LangChainError,
"Callback handler for #{inspect(callback_name)} raised an exception: #{LangChainError.format_exception(err, __STACKTRACE__, :short)}"
end
end
end
end