Class: ActionPrompt::Base
- Inherits:
-
Object
- Object
- ActionPrompt::Base
- 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
- #message ⇒ ActionPrompt::Message? readonly
Class Method Summary collapse
-
.default(**options) ⇒ void
Sets default LLM options for all actions within this class.
-
.method_missing(method_name, *args, &block) ⇒ ActionPrompt::Message
Intercepts calls to public instance methods (prompt actions) and returns an Message.
- .respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Instance Method Summary collapse
-
#prompt(options = {}) ⇒ ActionPrompt::Message
Declares per-action LLM options and finalises the Message for this action.
Instance Attribute Details
#message ⇒ ActionPrompt::Message? (readonly)
81 82 83 |
# File 'lib/action_prompt/base.rb', line 81 def 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.
52 53 54 |
# File 'lib/action_prompt/base.rb', line 52 def default(**) self. = .merge() 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.
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. 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
91 92 93 94 95 96 |
# File 'lib/action_prompt/base.rb', line 91 def prompt( = {}) = ActionPrompt.configuration. .merge(self.class.) .merge() = Message.new(self, @_action_name, ) end |