View Source LangChain.MessageDelta (LangChain v0.2.0)

Models a "delta" message from a chat LLM. A delta is a small chunk, or piece of a much larger complete message. A series of deltas are used to construct the complete message.

Delta messages must be applied in order for them to be valid. Delta messages can be combined and transformed into a LangChain.Message once the final piece is received.

Roles

  • :unknown - The role data is missing for the delta.
  • :assistant - Responses coming back from the LLM.

Tool Usage

Tools can be used or called by the assistant (LLM). A tool call is also split across many message deltas and must be fully assembled before it can be executed.

Summary

Functions

Merge two MessageDelta structs. The first MessageDelta is the primary one that smaller deltas are merged into.

Create a new MessageDelta that represents a message chunk.

Create a new MessageDelta that represents a message chunk and return it or raise an error if invalid.

Convert the MessageDelta to a Message. Can only convert a fully complete MessageDelta.

Types

@type t() :: %LangChain.MessageDelta{
  content: term(),
  index: term(),
  role: term(),
  status: term(),
  tool_calls: term()
}

Functions

Link to this function

merge_delta(primary, delta_part)

View Source
@spec merge_delta(nil | t(), t()) :: t()

Merge two MessageDelta structs. The first MessageDelta is the primary one that smaller deltas are merged into.

iex> delta_1 =
...>   %LangChain.MessageDelta{
...>     content: nil,
...>     index: 0,
...>     tool_calls: [],
...>     role: :assistant,
...>     status: :incomplete
...>   }
iex> delta_2 =
...>   %LangChain.MessageDelta{
...>     content: "Hello",
...>     index: 0,
...>     tool_calls: [],
...>     role: :unknown,
...>     status: :incomplete
...>   }
iex> LangChain.MessageDelta.merge_delta(delta_1, delta_2)
%LangChain.MessageDelta{content: "Hello", status: :incomplete, index: 0, role: :assistant, tool_calls: []}

A set of deltas can be easily merged like this:

[first | rest] = list_of_delta_message

Enum.reduce(rest, first, fn new_delta, acc ->
  MessageDelta.merge_delta(acc, new_delta)
end)
@spec new(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Create a new MessageDelta that represents a message chunk.

@spec new!(attrs :: map()) :: t() | no_return()

Create a new MessageDelta that represents a message chunk and return it or raise an error if invalid.

@spec to_message(t()) :: {:ok, LangChain.Message.t()} | {:error, String.t()}

Convert the MessageDelta to a Message. Can only convert a fully complete MessageDelta.

This is assumed to be the result of merging all the received MessageDeltas. An error is returned if the status is :incomplete.

If the MessageDelta fails to convert to a LangChain.Message, an error is returned with the reason.