AshAi.Actions (ash_ai v1.1.0)

Copy Markdown View Source

Builtin generic action implementations.

ReqLLM-based Prompt Actions

The prompt/2 macro accepts ReqLLM-compatible model specifications and uses ReqLLM for structured output generation.

Examples

action :analyze_sentiment, Sentiment do
  argument :text, :string, allow_nil?: false

  run prompt("openai:gpt-4o",
    prompt: [
      %{role: "system", content: "You analyze sentiment."},
      %{role: "user", content: "Analyze: <%= @input.arguments.text %>"}
    ]
  )
end

Prompt Formats

The :prompt option supports multiple formats:

  1. String (EEx template): "Analyze this: <%= @input.arguments.text %>"
  2. {System, User} tuple: {"You are an expert", "Analyze: <%= @input.arguments.text %>"}
  3. ReqLLM.Context: Pass a context directly (canonical format)
  4. List of messages: Maps, ReqLLM.Message structs, or mixed
  5. Function returning any of the above: fn input, context -> ... end
import ReqLLM.Context

run prompt("openai:gpt-4o",
  prompt: fn input, _ctx ->
    ReqLLM.Context.new([
      system("You are an OCR expert"),
      user([
        ReqLLM.Message.ContentPart.text("Extract text"),
        ReqLLM.Message.ContentPart.image_url(input.arguments.image_url)
      ])
    ])
  end
)

Summary

Functions

evaluate(model, opts \\ [])

(macro)

Configures an evaluation action backed by AshAi.Actions.Evaluate.

Evaluation models such as TypeSafe's Jev answer typed questions about the action's inputs instead of generating text. The return type must be an answer type (AshAi.Evaluate.Choice, AshAi.Evaluate.Noul, AshAi.Evaluate.Score) or AshAi.Evaluate.Judgments.

action :urgent, AshAi.Evaluate.Noul do
  description "Does `ticket` convey urgency?"
  argument :ticket, :string, allow_nil?: false

  run evaluate("typesafe:jev-latest")
end

prompt(model, opts \\ [])

(macro)