Packages
langchain
0.5.1
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.
"""
require Logger
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. Cycle through them to
# execute the named callback with the arguments if assigned.
Enum.each(callbacks, fn handlers_map ->
# find if the callback is in the handler map
case Map.get(handlers_map, callback_name) do
nil ->
# no handler attached
:ok
callback_fn when is_function(callback_fn) ->
try do
# execute the function
apply(callback_fn, arguments)
rescue
err ->
msg =
"Callback handler for #{inspect(callback_name)} raised an exception: #{LangChainError.format_exception(err, __STACKTRACE__, :short)}"
Logger.error(msg)
raise LangChainError, msg
end
other ->
msg =
"Unexpected callback handler. Callback #{inspect(callback_name)} was assigned #{inspect(other)}"
Logger.error(msg)
raise LangChainError, msg
end
end)
end
end