Packages
langchain
0.6.2
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/chains/llm_chain/modes/step.ex
defmodule LangChain.Chains.LLMChain.Modes.Step do
@moduledoc """
Execution mode that runs a single step at a time.
A "step" is: execute any pending tool calls, then call the LLM.
If no tools were pending, just call the LLM.
## Options
- `:should_continue?` — Function `(LLMChain.t() -> boolean())` called after
each step. If it returns `true`, another step runs. If `false` or not
provided, the mode returns after one step.
## Usage
# Single step
LLMChain.run(chain, mode: :step)
# Auto-loop with condition
LLMChain.run(chain, mode: :step,
should_continue?: fn chain ->
chain.needs_response && length(chain.exchanged_messages) < 10
end)
"""
@behaviour LangChain.Chains.LLMChain.Mode
alias LangChain.Chains.LLMChain
@impl true
def run(%LLMChain{} = chain, opts) do
should_continue_fn = Keyword.get(opts, :should_continue?)
case run_single_step(chain) do
{:ok, updated_chain} ->
if is_function(should_continue_fn, 1) && should_continue_fn.(updated_chain) do
run(updated_chain, opts)
else
{:ok, updated_chain}
end
{:error, _chain, _reason} = error ->
error
end
end
defp run_single_step(%LLMChain{} = chain) do
chain_after_tools = LLMChain.execute_tool_calls(chain)
# If no tools were executed, automatically run LLM
if chain_after_tools == chain do
LLMChain.execute_step(chain_after_tools)
else
{:ok, chain_after_tools}
end
end
end