Class: Raif::Task

Direct Known Subclasses

Evals::LlmJudge

Constant Summary

Constants included from Concerns::LlmResponseParsing

Concerns::LlmResponseParsing::ASCII_CONTROL_CHARS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::JsonSchemaDefinition

#schema_for_instance

Methods included from Concerns::LlmResponseParsing

#parse_html_response, #parse_json_response, #parsed_response

Methods included from Concerns::HasRuntimeDuration

#runtime_duration, #runtime_duration_seconds, #runtime_ended_at

Methods included from Concerns::HasAvailableModelTools

#available_model_tools_map

Methods included from Concerns::HasRequestedLanguage

#requested_language_name, #system_prompt_language_preference

Methods included from Concerns::HasLlm

#default_llm_model_key, #llm

Methods inherited from ApplicationRecord

table_name_prefix, where_json_not_blank

Instance Attribute Details

#filesObject

Returns the value of attribute files.



75
76
77
# File 'app/models/raif/task.rb', line 75

def files
  @files
end

#imagesObject

Returns the value of attribute images.



75
76
77
# File 'app/models/raif/task.rb', line 75

def images
  @images
end

Class Method Details

.json_response_schema(&block) ⇒ Object



183
184
185
186
187
188
189
# File 'app/models/raif/task.rb', line 183

def self.json_response_schema(&block)
  if block_given?
    json_schema_definition(:json_response, &block)
  elsif schema_defined?(:json_response)
    schema_for(:json_response)
  end
end

.prompt(creator: nil, **args) ⇒ String

Returns the LLM prompt for the task.

Parameters:

  • creator (Object, nil) (defaults to: nil)

    The creator of the task (polymorphic association), optional

  • args (Hash)

    Additional arguments to pass to the instance of the task that is created.

Returns:

  • (String)

    The LLM prompt for the task.



170
171
172
# File 'app/models/raif/task.rb', line 170

def self.prompt(creator: nil, **args)
  new(creator:, **args).build_prompt
end

.run(creator: nil, available_model_tools: [], llm_model_key: nil, images: [], files: [], **args) ⇒ Raif::Task?

The primary interface for running a task. It will hit the LLM with the task’s prompt and system prompt and return a Raif::Task object. It will also create a new Raif::ModelCompletion record.

Parameters:

  • creator (Object, nil) (defaults to: nil)

    The creator of the task (polymorphic association), optional

  • available_model_tools (Array<Class>) (defaults to: [])

    Optional array of model tool classes that will be provided to the LLM for it to invoke.

  • llm_model_key (Symbol, String) (defaults to: nil)

    Optional key for the LLM model to use. If blank, Raif.config.default_llm_model_key will be used.

  • images (Array) (defaults to: [])

    Optional array of Raif::ModelImageInput objects to include with the prompt.

  • files (Array) (defaults to: [])

    Optional array of Raif::ModelFileInput objects to include with the prompt.

  • args (Hash)

    Additional arguments to pass to the instance of the task that is created.

Returns:

  • (Raif::Task, nil)

    The task instance that was created and run.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/raif/task.rb', line 102

def self.run(creator: nil, available_model_tools: [], llm_model_key: nil, images: [], files: [], **args)
  task = new(
    creator: creator,
    llm_model_key: llm_model_key,
    available_model_tools: available_model_tools,
    started_at: Time.current,
    images: images,
    files: files,
    **args
  )

  task.save!
  task.run
  task
rescue StandardError => e
  task&.failed!

  logger.error e.message
  logger.error e.backtrace.join("\n")

  if defined?(Airbrake)
    notice = Airbrake.build_notice(e)
    notice[:context][:component] = "raif_task"
    notice[:context][:action] = name

    Airbrake.notify(notice)
  end

  task
end

.system_prompt(creator: nil, **args) ⇒ String

Returns the LLM system prompt for the task.

Parameters:

  • creator (Object, nil) (defaults to: nil)

    The creator of the task (polymorphic association), optional

  • args (Hash)

    Additional arguments to pass to the instance of the task that is created.

Returns:

  • (String)

    The LLM system prompt for the task.



179
180
181
# File 'app/models/raif/task.rb', line 179

def self.system_prompt(creator: nil, **args)
  new(creator:, **args).build_system_prompt
end

Instance Method Details

#build_promptObject

Raises:

  • (NotImplementedError)


207
208
209
# File 'app/models/raif/task.rb', line 207

def build_prompt
  raise NotImplementedError, "Raif::Task subclasses must implement #build_prompt"
end

#build_system_promptObject



211
212
213
214
215
216
# File 'app/models/raif/task.rb', line 211

def build_system_prompt
  sp = Raif.config.task_system_prompt_intro
  sp = sp.call(self) if sp.respond_to?(:call)
  sp += system_prompt_language_preference if requested_language_key.present?
  sp
end

#json_response_schemaObject

Instance method to get the JSON response schema For instance-dependent schemas, builds the schema with this instance as context For class-level schemas, returns the class-level schema



194
195
196
# File 'app/models/raif/task.rb', line 194

def json_response_schema
  schema_for_instance(:json_response)
end

#messagesObject



161
162
163
# File 'app/models/raif/task.rb', line 161

def messages
  [{ "role" => "user", "content" => message_content }]
end

#prompt_studio_task_attributesHash

Returns additional attributes to assign when creating tasks in Prompt Studio (reruns, batch runs, and judge tasks). Override in your ApplicationTask or task subclass to include app-specific attributes.

Returns:

  • (Hash)

    additional attributes to assign to the new task



203
204
205
# File 'app/models/raif/task.rb', line 203

def prompt_studio_task_attributes
  {}
end

#re_runObject



156
157
158
159
# File 'app/models/raif/task.rb', line 156

def re_run
  update_columns(started_at: Time.current)
  run(skip_prompt_population: true)
end

#run(skip_prompt_population: false) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/raif/task.rb', line 133

def run(skip_prompt_population: false)
  update_columns(started_at: Time.current) if started_at.nil?

  populate_prompts unless skip_prompt_population

  mc = llm.chat(
    messages: messages,
    source: self,
    system_prompt: system_prompt,
    response_format: response_format.to_sym,
    available_model_tools: available_model_tools,
    temperature: self.class.temperature
  )

  self.raif_model_completion = mc.becomes(Raif::ModelCompletion)

  update(raw_response: raif_model_completion.raw_response)

  process_model_tool_invocations
  completed!
  self
end

#statusObject



80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/raif/task.rb', line 80

def status
  if completed_at?
    :completed
  elsif failed_at?
    :failed
  elsif started_at?
    :in_progress
  else
    :pending
  end
end