Class: Raif::Task
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Raif::Task
- Includes:
- Concerns::HasAvailableModelTools, Concerns::HasLlm, Concerns::HasRequestedLanguage, Concerns::InvokesModelTools, Concerns::JsonSchemaDefinition, Concerns::LlmResponseParsing, Concerns::LlmTemperature, Concerns::RunWith
- Defined in:
- app/models/raif/task.rb
Direct Known Subclasses
Constant Summary
Constants included from Concerns::LlmResponseParsing
Concerns::LlmResponseParsing::ASCII_CONTROL_CHARS
Instance Attribute Summary collapse
-
#files ⇒ Object
Returns the value of attribute files.
-
#images ⇒ Object
Returns the value of attribute images.
Class Method Summary collapse
- .json_response_schema(&block) ⇒ Object
-
.prompt(creator: nil, **args) ⇒ String
Returns the LLM prompt for the task.
-
.run(creator: nil, available_model_tools: [], llm_model_key: nil, images: [], files: [], **args) ⇒ Raif::Task?
The primary interface for running a task.
-
.system_prompt(creator: nil, **args) ⇒ String
Returns the LLM system prompt for the task.
Instance Method Summary collapse
- #build_prompt ⇒ Object
- #build_system_prompt ⇒ Object
- #messages ⇒ Object
- #re_run ⇒ Object
- #run(skip_prompt_population: false) ⇒ Object
- #status ⇒ Object
Methods included from Concerns::LlmResponseParsing
#parse_html_response, #parse_json_response, #parsed_response
Methods included from Concerns::HasAvailableModelTools
Methods included from Concerns::HasRequestedLanguage
#requested_language_name, #system_prompt_language_preference
Methods included from Concerns::HasLlm
Methods inherited from ApplicationRecord
Instance Attribute Details
#files ⇒ Object
Returns the value of attribute files.
73 74 75 |
# File 'app/models/raif/task.rb', line 73 def files @files end |
#images ⇒ Object
Returns the value of attribute images.
73 74 75 |
# File 'app/models/raif/task.rb', line 73 def images @images end |
Class Method Details
.json_response_schema(&block) ⇒ Object
181 182 183 184 185 186 187 |
# File 'app/models/raif/task.rb', line 181 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.
168 169 170 |
# File 'app/models/raif/task.rb', line 168 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.
100 101 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 |
# File 'app/models/raif/task.rb', line 100 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. 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.
177 178 179 |
# File 'app/models/raif/task.rb', line 177 def self.system_prompt(creator: nil, **args) new(creator:, **args).build_system_prompt end |
Instance Method Details
#build_prompt ⇒ Object
189 190 191 |
# File 'app/models/raif/task.rb', line 189 def build_prompt raise NotImplementedError, "Raif::Task subclasses must implement #build_prompt" end |
#build_system_prompt ⇒ Object
193 194 195 196 197 198 |
# File 'app/models/raif/task.rb', line 193 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 |
#messages ⇒ Object
159 160 161 |
# File 'app/models/raif/task.rb', line 159 def [{ "role" => "user", "content" => }] end |
#re_run ⇒ Object
154 155 156 157 |
# File 'app/models/raif/task.rb', line 154 def re_run update_columns(started_at: Time.current) run(skip_prompt_population: true) end |
#run(skip_prompt_population: false) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'app/models/raif/task.rb', line 131 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: , 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 |
#status ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/raif/task.rb', line 78 def status if completed_at? :completed elsif failed_at? :failed elsif started_at? :in_progress else :pending end end |