View Source LangChain.Message (LangChain v0.2.0)

Models a complete Message for a chat LLM.

Roles

  • :system - a system message. Typically just one and it occurs first as a primer for how the LLM should behave.

  • :user - The user or application responses. Typically represents the "human" element of the exchange.

  • :assistant - Responses coming back from the LLM. This includes one or more "tool calls", requesting that the system execute a tool on behalf of the LLM and return the response.

  • :tool - A message for returning the result of executing a tool request.

Tools

A tool_call comes from the :assistant role. The tool_id identifies which of the available tool's to execute.

Create a message of role :function to provide the function response.

  • :is_error - Boolean value used to track a tool response message as being an error or not. The error state may be returned to an LLM in different ways, whichever is most appropriate for the LLM. When a response is an error, the content explains the error to the LLM and depending on the situation, the LLM may choose try again.

User Content Parts

Some LLMs support multi-modal messages. This means the user's message content can be text and/or image data. Within the LLMs, these are often referred to as "Vision", meaning you can provide text like "Please identify the what this is an image of" and provide an image.

User Content Parts are implemented through LangChain.Message.ContentPart. A list of them can be supplied as the "content" for a message. Only a few LLMs support it, and they may require using specific models trained for it. See the documentation for the LLM or service for details on their level of support.

Examples

A basic system message example:

alias LangChain.Message

Message.new_system!("You are a helpful assistant.")

A basic user message:

Message.new_user!("Who is Prime Minister of the moon?")

A multi-part user message: alias LangChain.Message.ContentPart

Message.new_user!([
  ContentPart.text!("What is in this picture?"),
  ContentPart.image_url!("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg")
]

Summary

Functions

Append a ToolResult to a message. A result can only be added to a :tool role message.

Return if a Message is a tool_call.

Build a new message and return an :ok/:error tuple with the result.

Build a new message and return it or raise an error if invalid.

Create a new assistant message which represents a response from the AI or LLM.

Create a new assistant message which represents a response from the AI or LLM.

Create a new system message which can prime the AI/Assistant for how to respond.

Create a new system message which can prime the AI/Assistant for how to respond.

Create a new tool message to represent the result of a tool's execution.

Create a new tool response message to return the result of an executed tool.

Create a new user message which represents a human message or a message from the application.

Create a new user message which represents a human message or a message from the application.

Types

@type status() :: :complete | :cancelled | :length
@type t() :: %LangChain.Message{
  content: term(),
  index: term(),
  name: term(),
  role: term(),
  status: term(),
  tool_calls: term(),
  tool_results: term()
}

Functions

Link to this function

append_tool_result(message, result)

View Source
@spec append_tool_result(t(), LangChain.Message.ToolResult.t()) :: t() | no_return()

Append a ToolResult to a message. A result can only be added to a :tool role message.

Return if a Message is a tool_call.

@spec new(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Build a new message and return an :ok/:error tuple with the result.

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

Build a new message and return it or raise an error if invalid.

Link to this function

new_assistant(attrs \\ %{})

View Source
@spec new_assistant(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Create a new assistant message which represents a response from the AI or LLM.

@spec new_assistant!(String.t() | map()) :: t() | no_return()

Create a new assistant message which represents a response from the AI or LLM.

Link to this function

new_system(content \\ "You are a helpful assistant.")

View Source
@spec new_system(content :: String.t()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Create a new system message which can prime the AI/Assistant for how to respond.

Link to this function

new_system!(content \\ "You are a helpful assistant.")

View Source
@spec new_system!(content :: String.t()) :: t() | no_return()

Create a new system message which can prime the AI/Assistant for how to respond.

Link to this function

new_tool_result(attrs \\ %{})

View Source
@spec new_tool_result(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Create a new tool message to represent the result of a tool's execution.

Attributes

  • :tool_results - a list of tool ToolResult structs.
  • :content - Text content returned from the LLM.
Link to this function

new_tool_result!(attrs \\ %{})

View Source
@spec new_tool_result!(attrs :: map()) :: t() | no_return()

Create a new tool response message to return the result of an executed tool.

Attributes

  • :tool_results - a list of tool ToolResult structs.
  • :content - Text content returned from the LLM.
@spec new_user(content :: String.t() | [LangChain.Message.ContentPart.t()]) ::
  {:ok, t()} | {:error, Ecto.Changeset.t()}

Create a new user message which represents a human message or a message from the application.

@spec new_user!(content :: String.t() | [LangChain.Message.ContentPart.t()]) ::
  t() | no_return()

Create a new user message which represents a human message or a message from the application.

Link to this function

validate_tool_results_list_type(changeset)

View Source
Link to this function

validate_tool_results_required(changeset)

View Source