Packages
llm_composer
0.16.0
0.20.2
0.20.1
0.20.0
0.19.6
0.19.5
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.2
0.18.1
0.18.0
0.17.1
0.17.0
0.16.2
0.16.1
0.16.0
0.15.0
0.14.2
0.14.1
0.14.0
0.13.1
0.13.0
0.12.3
0.12.2
0.12.0
0.11.2
0.11.1
0.11.0
0.10.0
0.8.0
0.7.0
0.6.0
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.0
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.0
LlmComposer is an Elixir library that facilitates chat interactions with language models, providing tools to handle user messages, generate responses, and execute functions automatically based on model outputs.
Current section
Files
Jump to
Current section
Files
lib/llm_composer/function_call_helpers.ex
defmodule LlmComposer.FunctionCallHelpers do
@moduledoc """
Helpers for building assistant messages and tool-result messages when handling
function (tool) calls returned by LLM providers.
This module provides a default implementation for composing the assistant
message that preserves the original assistant response and attaches the
`tool_calls` metadata. Providers can optionally implement
`build_assistant_with_tools/3` to customize behavior.
"""
alias LlmComposer.LlmResponse
alias LlmComposer.Message
@doc """
Build an assistant message that preserves the original assistant response and
attaches `tool_calls` so it can be sent back to the provider along with
tool result messages.
If `provider_mod` exports `build_assistant_with_tools/3`, this function will
delegate to that implementation; otherwise it uses a sensible default.
"""
@spec build_assistant_with_tools(module(), LlmResponse.t(), Message.t(), keyword()) ::
Message.t()
def build_assistant_with_tools(
provider_mod,
%LlmResponse{} = resp,
%Message{} = user_msg,
opts \\ []
) do
if function_exported?(provider_mod, :build_assistant_with_tools, 3) do
provider_mod.build_assistant_with_tools(resp, user_msg, opts)
else
%Message{
type: :assistant,
content: resp.main_response.content || "Using tool results",
metadata: %{
original: resp.main_response.metadata[:original],
tool_calls: resp.function_calls
}
}
end
end
@doc """
Convert executed function-call results into `:tool_result` messages which
include the mapping back to the tool call id in `metadata["tool_call_id"]`.
"""
@spec build_tool_result_messages(list()) :: list(Message.t())
def build_tool_result_messages(executed_calls) when is_list(executed_calls) do
Enum.map(executed_calls, fn call ->
%Message{
type: :tool_result,
content: to_string(call.result),
metadata: %{"tool_call_id" => call.id}
}
end)
end
end