LangEx (LangEx v0.11.3)

Copy Markdown View Source

LangEx — LangGraph for Elixir.

A graph-based agent orchestration library inspired by LangGraph. Build stateful, multi-step LLM workflows using nodes, edges, conditional routing, and composable state reducers.

Quick Start

alias LangEx.Graph
alias LangEx.Message

graph =
  Graph.new(messages: {[], &Message.add_messages/2})
  |> Graph.add_node(:greet, fn state ->
    name = hd(state.messages).content
    %{messages: [Message.ai("Hello, #{name}!")]}
  end)
  |> Graph.add_edge(:__start__, :greet)
  |> Graph.add_edge(:greet, :__end__)
  |> Graph.compile()

{:ok, result} = LangEx.invoke(graph, %{messages: [Message.human("World")]})

Summary

Functions

Deletes every checkpoint for the thread in the config.

Returns the latest (or a specific) checkpoint for a thread.

Returns the checkpoint history for a thread, most recent first.

Executes a compiled graph with the given input state.

Returns a lazy stream of execution events from the compiled graph.

Applies an update to checkpointed state, saving a new forked checkpoint.

Functions

delete_thread(graph, opts)

@spec delete_thread(
  LangEx.Graph.Compiled.t(),
  keyword()
) :: :ok | {:error, term()}

Deletes every checkpoint for the thread in the config.

get_state(graph, opts)

@spec get_state(
  LangEx.Graph.Compiled.t(),
  keyword()
) :: {:ok, LangEx.Checkpoint.t()} | :none | {:error, term()}

Returns the latest (or a specific) checkpoint for a thread.

get_state_history(graph, opts)

@spec get_state_history(
  LangEx.Graph.Compiled.t(),
  keyword()
) :: [LangEx.Checkpoint.t()]

Returns the checkpoint history for a thread, most recent first.

invoke(graph, input, opts \\ [])

@spec invoke(LangEx.Graph.Compiled.t(), map() | LangEx.Command.t(), keyword()) ::
  {:ok, map()} | {:interrupt, term(), map()} | {:error, term()}

Executes a compiled graph with the given input state.

stream(graph, input, opts \\ [])

Returns a lazy stream of execution events from the compiled graph.

update_state(graph, update, opts)

@spec update_state(LangEx.Graph.Compiled.t(), map(), keyword()) ::
  {:ok, LangEx.Checkpoint.t()} | {:error, term()}

Applies an update to checkpointed state, saving a new forked checkpoint.