Class: ActionPrompt::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/action_prompt/base.rb

Overview

Base class for all prompt classes. Subclass this to define your prompts.

Each public instance method in a subclass is a "prompt action". Calling the method at the class level returns an Message that can be delivered synchronously via Message#deliver_now.

Defining a prompt

class ArticleSummarizerPrompt < ActionPrompt::Base
# Class-level defaults override the global config for this class only.
default model: "gpt-4o-mini", temperature: 0.5

def summarize(article)
  @article = article          # available in the ERB template as @article
  @word_limit = 150

  prompt(model: "gpt-4o")     # optional per-action override
end
end

Delivering a prompt

response = ArticleSummarizerPrompt.summarize(article).deliver_now

Template location

app/views/action_prompts/article_summarizer_prompt/summarize.text.erb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#messageActionPrompt::Message? (readonly)

The Message built by the most recent call to #prompt.



81
82
83
# File 'lib/action_prompt/base.rb', line 81

def message
  @message
end

Class Method Details

.default(**options) ⇒ void

This method returns an undefined value.

Sets default LLM options for all actions within this class. Options merge with the global config, and can be further overridden per-action by passing options to #prompt.

Examples:

class MyPrompt < ActionPrompt::Base
  default model: "gpt-4o", temperature: 0.3
end


52
53
54
# File 'lib/action_prompt/base.rb', line 52

def default(**options)
  self.default_options = default_options.merge(options)
end

.method_missing(method_name, *args, &block) ⇒ ActionPrompt::Message

Intercepts calls to public instance methods (prompt actions) and returns an Message. This is the primary entry point for building a prompt from outside the class.

Examples:

ArticleSummarizerPrompt.summarize(article)  # => ActionPrompt::Message


64
65
66
67
68
69
70
71
72
# File 'lib/action_prompt/base.rb', line 64

def method_missing(method_name, *args, &block)
  if public_method_defined?(method_name)
    instance = new
    instance.send(:process, method_name, *args, &block)
    instance.message
  else
    super
  end
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean



74
75
76
# File 'lib/action_prompt/base.rb', line 74

def respond_to_missing?(method_name, include_private = false)
  public_method_defined?(method_name) || super
end

Instance Method Details

#prompt(options = {}) ⇒ ActionPrompt::Message

Declares per-action LLM options and finalises the Message for this action. Call this at the end of every action method.

Options are merged in increasing priority order:

global config defaults < class-level defaults < per-action options


91
92
93
94
95
96
# File 'lib/action_prompt/base.rb', line 91

def prompt(options = {})
  merged_options = ActionPrompt.configuration.default_options
                               .merge(self.class.default_options)
                               .merge(options)
  @message = Message.new(self, @_action_name, merged_options)
end