View Source LangChain.MessageDelta (LangChain v0.1.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 can are used to construct a 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.
Function calling
:function_name
- A message from the LLM expressing the intent to execute a function that was previously declared available to it.The
arguments
will eventually be parsed from JSON. However, as deltas are streamed, the arguments come in as text. Once it is fully received it can be parsed as JSON, but it cannot be used before it is complete as it will not be valid JSON.
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
Functions
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,
...> function_name: nil,
...> role: :assistant,
...> arguments: nil,
...> status: :incomplete
...> }
iex> delta_2 =
...> %LangChain.MessageDelta{
...> content: "Hello",
...> index: 0,
...> function_name: nil,
...> role: :unknown,
...> arguments: nil,
...> status: :incomplete
...> }
iex> LangChain.MessageDelta.merge_delta(delta_1, delta_2)
%LangChain.MessageDelta{content: "Hello", status: :incomplete, index: 0, function_name: nil, role: :assistant, arguments: nil}
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.
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 MessageDelta
s.
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.