Class: ActionPrompt::Message

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

Overview

Represents a fully prepared prompt ready for delivery to an LLM adapter. Analogous to Mail::Message in ActionMailer.

Instances are returned by calling a class-level action method:

message = ArticleSummarizerPrompt.summarize(article)
message.body       # => rendered ERB string (lazily evaluated)
message.deliver_now  # => LLM response string

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt_instance, action_name, options = {}) ⇒ Message



28
29
30
31
32
# File 'lib/action_prompt/message.rb', line 28

def initialize(prompt_instance, action_name, options = {})
  @prompt_instance = prompt_instance
  @action_name = action_name
  @options = options
end

Instance Attribute Details

#action_nameString (readonly)

The name of the action that built this message.



19
20
21
# File 'lib/action_prompt/message.rb', line 19

def action_name
  @action_name
end

#optionsHash (readonly)

Merged LLM options (global defaults < class defaults < per-action options).



23
24
25
# File 'lib/action_prompt/message.rb', line 23

def options
  @options
end

#prompt_instanceActionPrompt::Base (readonly)

The prompt instance that built this message.



15
16
17
# File 'lib/action_prompt/message.rb', line 15

def prompt_instance
  @prompt_instance
end

Instance Method Details

#bodyString

The rendered prompt body. Evaluated lazily on first access.



37
38
39
# File 'lib/action_prompt/message.rb', line 37

def body
  @body ||= Renderer.new(prompt_instance, action_name).render
end

#deliver_later(**_kwargs) ⇒ Object

Note:

Full ActiveJob integration is planned for a future release. This currently raises NotImplementedError.

Enqueues the prompt for asynchronous delivery via ActiveJob.

Raises:

  • (NotImplementedError)


55
56
57
58
59
# File 'lib/action_prompt/message.rb', line 55

def deliver_later(**_kwargs)
  raise NotImplementedError,
        "deliver_later is not yet implemented. Use deliver_now. " \
        "ActiveJob integration is planned for a future release."
end

#deliver_nowString?

Delivers the prompt to the configured adapter synchronously and returns the LLM response.



45
46
47
48
# File 'lib/action_prompt/message.rb', line 45

def deliver_now
  log_delivery
  ActionPrompt.configuration.adapter.complete(body, options)
end

#inspectString Also known as: to_s



62
63
64
# File 'lib/action_prompt/message.rb', line 62

def inspect
  "#<ActionPrompt::Message action=#{action_name.inspect} options=#{options.inspect}>"
end