starflow

Types

Represents a flow of transformations from state to prompt to response and back to state.

Fields

  • model: The model configuration for API requests
  • prompt: Function to transform state into a prompt
  • parser: Function to transform response back into state

Example

// Create a flow for a number guessing game
type GameState {
  GameState(target: Int, guesses: List(Int))
}

let game_flow =
  new(model)
  |> with_prompt(fn(state) {
    [state.TextContent("Guess a number between 1 and 10")]
  })
  |> with_parser(fn(state, resp) {
    // Parse response and update game state
    let guess = extract_guess(resp)
    let new_guesses = [guess, ..state.any.guesses]
    state.State(..state, any: GameState(..state.any, guesses: new_guesses))
  })
pub type Flow(any) {
  Flow(
    model: model.Model,
    prompt: transform.Prompt(any),
    parser: transform.Parser(any),
  )
}

Constructors

  • Flow(
      model: model.Model,
      prompt: transform.Prompt(any),
      parser: transform.Parser(any),
    )
pub type HasModel
pub type NoModel

Functions

pub fn invoke(
  state: State(a),
  flow: Flow(a),
) -> Result(State(a), APIError)

Executes one step of the Flow:

  1. Transforms state into a prompt using the prompt transformer
  2. Sends the prompt to the model
  3. Transforms the response back into state using the parser

Examples

// Simple counter flow
let counter_flow =
  new(model)
  |> with_prompt(fn(count) {
    [state.TextContent("Add one to " <> int.to_string(count))]
  })
  |> with_parser(fn(state, resp) {
    // Parse response to get new count
    let new_count = state.any + 1
    state.State(..state, any: new_count)
  })

// Run one step
case invoke(state.new(0), counter_flow) {
  Ok(new_state) -> // Handle updated state
  Error(error) -> // Handle API error
}
pub fn new(model: Model) -> Flow(a)

Creates a new Flow with the given model and default transformers.

Examples

let flow = new(model)
// Creates a flow with:
// - The specified model
// - Default prompt transformer (string inspection)
// - Default response parser (message accumulation)
pub fn with_model(flow: Flow(a), model: Model) -> Flow(a)

Updates the model configuration of a Flow.

Examples

let updated_flow =
  flow
  |> with_model(new_model)
pub fn with_parser(
  flow: Flow(a),
  parser: fn(State(a), Response) -> State(a),
) -> Flow(a)

Sets a custom response parser for the Flow.

Examples

let flow =
  new(model)
  |> with_parser(fn(state, resp) {
    // Extract and process the response
    case resp.content {
      [state.TextContent(text)] -> {
        let count = parse_number(text)
        state.State(..state, any: count)
      }
      _ -> state
    }
  })
pub fn with_prompt(
  flow: Flow(a),
  prompt: fn(a) -> List(Content),
) -> Flow(a)

Sets a custom prompt transformer for the Flow.

Examples

let flow =
  new(model)
  |> with_prompt(fn(state) {
    [state.TextContent("Current count: " <> int.to_string(state))]
  })
Search Document