LLMAgent.Flows (llm_agent v0.2.0)

View Source

Provides standard flow definitions for common LLM agent patterns.

This module creates AgentForge flow compositions for different LLM agent use cases, such as conversational agents and task-based agents. It registers tools, creates appropriate handlers, and configures initial state.

Summary

Functions

Creates a standard conversation flow with the given system prompt and tools.

Maps a flow to a new flow by applying a transformation function.

Creates a flow for a simple question-answering agent.

Creates a task flow with the given task definition.

Creates a flow for a tool-using agent.

Adds middleware to a flow.

Functions

batch_processing(items, batch_handler, options \\ [])

Creates a batch processing flow.

A batch processing flow iterates over a collection of items and applies the batch handler to each item.

Parameters

  • items - The collection of items to process
  • batch_handler - The handler function to apply to each item
  • options - Additional options for batch processing

Returns

A function that can be used as a flow.

Examples

iex> items = [1, 2, 3]
iex> handler = fn signal, state -> {{:emit, signal}, state} end
iex> flow = LLMAgent.Flows.batch_processing(items, handler)
iex> is_function(flow)
true

conversation(system_prompt, tools \\ [], options \\ [])

Creates a standard conversation flow with the given system prompt and tools.

Parameters

  • system_prompt - The system prompt that defines the agent's behavior
  • tools - A list of tools that the agent can use
  • options - Additional options for configuring the agent

Returns

A tuple containing the flow and initial state for the agent.

Examples

iex> {flow, state} = LLMAgent.Flows.conversation("You are a helpful assistant.", [])
iex> is_function(flow) and is_map(state)
true

map_flow(flow, transform_fn)

Maps a flow to a new flow by applying a transformation function.

Useful for creating derived flows that modify signal or state handling.

Parameters

  • flow - The original flow
  • transform_fn - A function that transforms the flow's result

Returns

A new flow that applies the transformation.

Examples

iex> flow = fn signal, state -> {{:emit, signal}, state} end
iex> transform = fn {result, state} -> {result, Map.put(state, :transformed, true)} end
iex> new_flow = LLMAgent.Flows.map_flow(flow, transform)
iex> is_function(new_flow)
true

qa_agent(system_prompt, options \\ [])

Creates a flow for a simple question-answering agent.

This is a specialized flow for handling basic question-answering without tools, providing a streamlined implementation for simple use cases.

Parameters

  • system_prompt - The system prompt that defines the agent's behavior
  • options - Additional options for configuring the agent

Returns

A tuple containing the flow and initial state for the agent.

Examples

iex> {flow, state} = LLMAgent.Flows.qa_agent("You are a helpful assistant.")
iex> is_function(flow) and is_map(state)
true

task_flow(task_definition, options \\ [])

Creates a task flow with the given task definition.

A task flow executes a series of primitive operations defined in the task definition.

Parameters

  • task_definition - The task definition as a list of AgentForge primitives
  • options - Additional options for the task flow

Returns

A function that can be used as a flow.

Examples

iex> task_def = [
...>   fn signal, state -> {:ok, "Step 1", state} end
...> ]
iex> flow = LLMAgent.Flows.task_flow(task_def)
iex> is_function(flow)
true

tool_agent(system_prompt, tools, options \\ [])

Creates a flow for a tool-using agent.

Parameters

  • system_prompt - The system prompt that defines the agent's behavior
  • tools - A list of tools that the agent can use
  • options - Additional options for configuring the agent

Returns

A tuple containing the flow and initial state for the agent.

Examples

iex> tools = [%{name: "get_time", execute: fn _ -> %{time: DateTime.utc_now()} end}]
iex> {flow, state} = LLMAgent.Flows.tool_agent("You are a helpful assistant.", tools)
iex> is_function(flow) and is_map(state)
true

with_middleware(flow, middleware)

Adds middleware to a flow.

Middleware functions are executed before and after the main flow, allowing for consistent pre- and post-processing.

Parameters

  • flow - The original flow
  • middleware - A function that receives the signal, state, and a continuation

Returns

A new flow with the middleware applied.

Examples

iex> flow = fn signal, state -> {{:emit, signal}, state} end
iex> middleware = fn signal, state, continue ->
...>   {result, new_state} = continue.(signal, state)
...>   {result, Map.put(new_state, :middleware_applied, true)}
...> end
iex> new_flow = LLMAgent.Flows.with_middleware(flow, middleware)
iex> is_function(new_flow)
true