Packages
langchain
0.8.12
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/message/tool_result.ex
defmodule LangChain.Message.ToolResult do
@moduledoc """
Represents a the result of running a requested tool. The LLM's requests a tool
use through a `ToolCall`. A `ToolResult` returns the answer or result from the
application back to the AI.
## Content
The `content` can be a string or a list of ContentParts for multi-modal
responses (text, images, etc.) that gets returned to the LLM as the result.
## Processed Content
The `processed_content` field is optional. When you want to keep the results
of the Elixir function call as a native Elixir data structure,
`processed_content` can hold it.
Advanced use: You can use the `options` field for LLM-specific features like
cache control.
To do this, the Elixir function's result should be a `{:ok, "String response
for LLM", native_elixir_data}`. See `LangChain.Function` for details and
examples.
"""
use Ecto.Schema
import Ecto.Changeset
alias __MODULE__
alias LangChain.LangChainError
alias LangChain.Utils
@primary_key false
embedded_schema do
field :type, Ecto.Enum, values: [:function], default: :function
# When a unique ID is given for a ToolCall, the same ID is used in the
# return to the a response to a specific request.
field :tool_call_id, :string
# the name of the tool that was run.
field :name, :string
# the content returned to the LLM/AI.
field :content, :any, virtual: true
# optional stored results of tool result
field :processed_content, :any, virtual: true
# Text to display in a UI for the result. Optional.
field :display_text, :string
# flag if the result is an error
field :is_error, :boolean, default: false
# flag indicating this tool result represents a paused/interrupted tool
field :is_interrupt, :boolean, default: false
# opaque interrupt data (not sent to LLM, virtual only)
field :interrupt_data, :any, virtual: true
# options potentially LLM specific (i.e. cache control for Anthropic)
field :options, :any, virtual: true
end
@type t :: %ToolResult{}
@update_fields [
:type,
:tool_call_id,
:name,
:content,
:processed_content,
:display_text,
:is_error,
:is_interrupt,
:interrupt_data,
:options
]
@create_fields @update_fields
@required_fields [:type, :tool_call_id, :content]
@doc """
Build a new ToolResult and return an `:ok`/`:error` tuple with the result.
"""
@spec new(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
def new(attrs \\ %{}) do
%ToolResult{}
|> cast(attrs, @create_fields)
|> Utils.migrate_to_content_parts()
|> common_validations()
|> apply_action(:insert)
end
@doc """
Build a new ToolResult and return it or raise an error if invalid.
"""
@spec new!(attrs :: map()) :: t() | no_return()
def new!(attrs \\ %{}) do
case new(attrs) do
{:ok, message} ->
message
{:error, changeset} ->
raise LangChainError, changeset
end
end
defp common_validations(changeset) do
validate_required(changeset, @required_fields)
end
end