Module: PromptDeclarations::ClassMethods

Defined in:
lib/raix/prompt_declarations.rb

Overview

This module provides a way to chain prompts and handle user responses in a serialized manner (in the order they were defined), with support for functions if the FunctionDispatch module is also included.

Instance Method Summary collapse

Instance Method Details

#prompt(system: nil, text:, success: nil, params: {}) ⇒ Object

Adds a prompt to the list of prompts.

Parameters:

  • system (Proc) (defaults to: nil)

    A lambda that generates the system message.

  • text (Proc)

    A lambda that generates the prompt text. (Required)

  • success (Proc) (defaults to: nil)

    The block of code to execute when the prompt is answered.

  • parameters (Hash)

    Additional parameters for the completion API call



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/raix/prompt_declarations.rb', line 20

def prompt(system: nil, text:, success: nil, params: {})
  name = Digest::SHA256.hexdigest(text.inspect)[0..7]
  prompts << begin
    open_struct = OpenStruct.new({ name:, system:, text:, success:, params: })
  end

  define_method(name) do |response|
    if Rails.env.local?
      puts "_" * 80
      puts "PromptDeclarations#response:"
      puts "#{text.source_location} (#{name})"
      puts response
      puts "_" * 80
    end

    return response if success.nil?

    instance_exec(response, &success)
  end
end

#promptsObject

the list of prompts declared at class level



42
43
44
# File 'lib/raix/prompt_declarations.rb', line 42

def prompts
  @prompts ||= []
end

#system_prompt(prompt = nil) ⇒ Object

getter/setter for system prompt declared at class level



47
48
49
# File 'lib/raix/prompt_declarations.rb', line 47

def system_prompt(prompt=nil)
  prompt ? @system_prompt = prompt.squish : @system_prompt
end