Class: Lluminary::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/lluminary/task.rb

Overview

Base class for all Lluminary tasks. Provides the core functionality for defining and running LLM-powered tasks.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = {}) ⇒ Task

Returns a new instance of Task.



93
94
95
96
97
98
# File 'lib/lluminary/task.rb', line 93

def initialize(input = {})
  @input = self.class.input_schema_model.new(input)
  @input.task_instance = self
  @validation_failed = false
  define_input_methods
end

Class Attribute Details

.providerObject



55
56
57
58
59
60
61
# File 'lib/lluminary/task.rb', line 55

def provider
  @provider ||=
    begin
      require_relative "providers/test"
      Providers::Test.new
    end
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



90
91
92
# File 'lib/lluminary/task.rb', line 90

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



90
91
92
# File 'lib/lluminary/task.rb', line 90

def output
  @output
end

#parsed_responseObject (readonly)

Returns the value of attribute parsed_response.



90
91
92
# File 'lib/lluminary/task.rb', line 90

def parsed_response
  @parsed_response
end

#validation_failedObject

Returns the value of attribute validation_failed.



91
92
93
# File 'lib/lluminary/task.rb', line 91

def validation_failed
  @validation_failed
end

Class Method Details

.call(input = {}) ⇒ Object



47
48
49
# File 'lib/lluminary/task.rb', line 47

def call(input = {})
  new(input).call
end

.call!(input = {}) ⇒ Object



51
52
53
# File 'lib/lluminary/task.rb', line 51

def call!(input = {})
  new(input).call!
end

.input_custom_validationsObject



85
86
87
# File 'lib/lluminary/task.rb', line 85

def input_custom_validations
  @input_schema&.custom_validations || []
end

.input_fieldsObject



65
66
67
# File 'lib/lluminary/task.rb', line 65

def input_fields
  @input_schema&.fields || {}
end

.input_schema(&block) ⇒ Object



12
13
14
15
# File 'lib/lluminary/task.rb', line 12

def input_schema(&block)
  @input_schema = Schema.new
  @input_schema.instance_eval(&block)
end

.input_schema_modelObject



73
74
75
# File 'lib/lluminary/task.rb', line 73

def input_schema_model
  @input_schema&.schema_model || Schema.new.schema_model
end

.output_custom_validationsObject



81
82
83
# File 'lib/lluminary/task.rb', line 81

def output_custom_validations
  @output_schema&.custom_validations || []
end

.output_fieldsObject



69
70
71
# File 'lib/lluminary/task.rb', line 69

def output_fields
  @output_schema&.fields || {}
end

.output_schema(&block) ⇒ Object



17
18
19
20
# File 'lib/lluminary/task.rb', line 17

def output_schema(&block)
  @output_schema = Schema.new
  @output_schema.instance_eval(&block)
end

.output_schema_modelObject



77
78
79
# File 'lib/lluminary/task.rb', line 77

def output_schema_model
  @output_schema&.schema_model || Schema.new.schema_model
end

.use_provider(provider_name, **config) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lluminary/task.rb', line 22

def use_provider(provider_name, **config)
  provider_class =
    case provider_name
    when :anthropic
      require_relative "providers/anthropic"
      Providers::Anthropic
    when :bedrock
      require_relative "providers/bedrock"
      Providers::Bedrock
    when :google
      require_relative "providers/google"
      Providers::Google
    when :openai
      require_relative "providers/openai"
      Providers::OpenAI
    when :test
      require_relative "providers/test"
      Providers::Test
    else
      raise ArgumentError, "Unknown provider: #{provider_name}"
    end

  @provider = provider_class.new(**config)
end

Instance Method Details

#callObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/lluminary/task.rb', line 100

def call
  if valid?
    response = self.class.provider.call(prompt, self)
    process_response(response)
  else
    @parsed_response = nil
    @output = nil
  end

  self
end

#call!Object



112
113
114
115
116
117
118
# File 'lib/lluminary/task.rb', line 112

def call!
  validate_input!
  response = self.class.provider.call(prompt, self)
  process_response(response)

  self
end

#errorsObject

Helper for validation methods to add errors



138
139
140
141
142
143
144
145
# File 'lib/lluminary/task.rb', line 138

def errors
  # Points to the current model being validated - used by custom validation methods
  if @current_model == :output && @output
    @output.errors
  else
    @input.errors
  end
end

#promptObject



129
130
131
# File 'lib/lluminary/task.rb', line 129

def prompt
  @prompt ||= self.class.provider.model.format_prompt(self)
end

#task_promptObject

Raises:

  • (NotImplementedError)


133
134
135
# File 'lib/lluminary/task.rb', line 133

def task_prompt
  raise NotImplementedError, "Subclasses must implement task_prompt"
end

#valid?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/lluminary/task.rb', line 120

def valid?
  @input.valid?
end

#validate_input!Object

Raises:



124
125
126
127
# File 'lib/lluminary/task.rb', line 124

def validate_input!
  return if @input.valid?
  raise ValidationError, @input.errors.full_messages.join(", ")
end