View Source LangChain.Chains.ChainCallbacks (LangChain v0.3.1)

Defines the callbacks fired by an LLMChain and LLM module.

A callback handler is a map that defines the specific callback event with a function to execute for that event.

Example

A sample configured callback handler that forwards received data to a specific LiveView.

live_view_pid = self()

my_handlers = %{
  on_llm_new_message: fn _context, new_message -> send(live_view_pid, {:received_message, new_message}),

  handle_chain_error_message_created: fn _context, new_message -> send(live_view_pid, {:received_message, new_message})
}

model = SomeLLM.new!(%{...})

chain =
  %{llm: model}
  |> LLMChain.new!()
  |> LLMChain.add_callback(my_handlers)

Summary

Types

The supported set of callbacks for an LLM module.

Executed when an LLMChain, in response to an error from the LLM, generates a new, automated response message intended to be returned to the LLM.

Executed when an LLMChain has completed processing a received assistant message.

Executed when processing a received message errors or fails. The erroring message is included in the callback with the state of processing that was completed before erroring.

Executed when the chain failed multiple times used up the max_retry_count resulting in the process aborting and returning an error.

Executed when the chain uses one or more tools and the resulting ToolResults are generated as part of a tool response message.

Executed when an LLM is streaming a response and a new MessageDelta (or token) was received.

Executed when an LLM is not streaming and a full message was received.

Executed when an LLM (typically a service) responds with rate limiting information.

Executed when an LLM response reports the token usage in a LangChain.TokenUsage struct. The data returned depends on the LLM.

Types

Link to this type

chain_callback_handler()

View Source
@type chain_callback_handler() :: %{
  optional(:on_llm_new_delta) => llm_new_delta(),
  optional(:on_llm_new_message) => llm_new_message(),
  optional(:on_llm_ratelimit_info) => llm_ratelimit_info(),
  optional(:on_llm_token_usage) => llm_token_usage(),
  optional(:on_message_processed) => chain_message_processed(),
  optional(:on_message_processing_error) => chain_message_processing_error(),
  optional(:on_error_message_created) => chain_error_message_created(),
  optional(:on_tool_response_created) => chain_tool_response_created(),
  optional(:on_retries_exceeded) => chain_retries_exceeded()
}

The supported set of callbacks for an LLM module.

Link to this type

chain_error_message_created()

View Source
@type chain_error_message_created() :: (LangChain.Chains.LLMChain.t(),
                                  LangChain.Message.t() ->
                                    any())

Executed when an LLMChain, in response to an error from the LLM, generates a new, automated response message intended to be returned to the LLM.

The handler's return value is discarded.

Example

A function declaration that matches the signature.

def handles_chain_error_message_created(chain, new_message) do
  IO.inspect(new_message)
end
Link to this type

chain_message_processed()

View Source
@type chain_message_processed() :: (LangChain.Chains.LLMChain.t(),
                              LangChain.Message.t() ->
                                any())

Executed when an LLMChain has completed processing a received assistant message.

The handler's return value is discarded.

Example

A function declaration that matches the signature.

def handle_chain_message_processed(chain, message) do
  IO.inspect(message)
end
Link to this type

chain_message_processing_error()

View Source
@type chain_message_processing_error() :: (LangChain.Chains.LLMChain.t(),
                                     LangChain.Message.t() ->
                                       any())

Executed when processing a received message errors or fails. The erroring message is included in the callback with the state of processing that was completed before erroring.

The handler's return value is discarded.

Example

A function declaration that matches the signature.

def handle_chain_message_processing_error(chain, new_message) do
  IO.inspect(new_message)
end
Link to this type

chain_retries_exceeded()

View Source
@type chain_retries_exceeded() :: (LangChain.Chains.LLMChain.t() -> any())

Executed when the chain failed multiple times used up the max_retry_count resulting in the process aborting and returning an error.

The handler's return value is discarded.

Example

A function declaration that matches the signature.

def handle_retries_exceeded(chain) do
  IO.inspect(chain)
end
Link to this type

chain_tool_response_created()

View Source
@type chain_tool_response_created() :: (LangChain.Chains.LLMChain.t(),
                                  LangChain.Message.t() ->
                                    any())

Executed when the chain uses one or more tools and the resulting ToolResults are generated as part of a tool response message.

The handler's return value is discarded.

Example

A function declaration that matches the signature.

def handle_chain_tool_response_created(chain, new_message) do
  IO.inspect(new_message)
end
@type llm_new_delta() :: (LangChain.Chains.LLMChain.t(), LangChain.MessageDelta.t() ->
                      any())

Executed when an LLM is streaming a response and a new MessageDelta (or token) was received.

  • :index is optionally present if the LLM supports sending n versions of a response.

The return value is discarded.

Example

A function declaration that matches the signature.

def handle_llm_new_delta(chain, delta) do
  IO.write(delta)
end
@type llm_new_message() :: (LangChain.Chains.LLMChain.t(), LangChain.Message.t() ->
                        any())

Executed when an LLM is not streaming and a full message was received.

The return value is discarded.

Example

A function declaration that matches the signature.

def handle_llm_new_message(chain, message) do
  IO.inspect(message)
end
@type llm_ratelimit_info() :: (LangChain.Chains.LLMChain.t(),
                         info :: %{required(String.t()) => any()} ->
                           any())

Executed when an LLM (typically a service) responds with rate limiting information.

The specific rate limit information depends on the LLM. It returns a map with all the available information included.

The return value is discarded.

Example

A function declaration that matches the signature.

def handle_llm_ratelimit_info(chain, %{} = info) do
  IO.inspect(info)
end
@type llm_token_usage() :: (LangChain.Chains.LLMChain.t(), LangChain.TokenUsage.t() ->
                        any())

Executed when an LLM response reports the token usage in a LangChain.TokenUsage struct. The data returned depends on the LLM.

The return value is discarded.

Example

A function declaration that matches the signature.

def handle_llm_token_usage(chain, %TokenUsage{} = usage) do
  IO.inspect(usage)
end