Class: Raif::Task

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::LlmResponseParsing

#clean_html_fragment, #parsed_response

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

Instance Attribute Details

#filesObject

Returns the value of attribute files.



32
33
34
# File 'app/models/raif/task.rb', line 32

def files
  @files
end

#imagesObject

Returns the value of attribute images.



32
33
34
# File 'app/models/raif/task.rb', line 32

def images
  @images
end

Class Method Details

.json_response_schema(&block) ⇒ Object



128
129
130
131
132
133
134
# File 'app/models/raif/task.rb', line 128

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:, **args) ⇒ String

Returns the LLM prompt for the task.

Parameters:

  • creator (Object)

    The creator of the task (polymorphic association)

  • args (Hash)

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

Returns:

  • (String)

    The LLM prompt for the task.



115
116
117
# File 'app/models/raif/task.rb', line 115

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

.run(creator:, 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)

    The creator of the task (polymorphic association)

  • 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.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/raif/task.rb', line 58

def self.run(creator:, available_model_tools: [], llm_model_key: nil, images: [], files: [], **args)
  task = new(creator:, llm_model_key:, 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:, **args) ⇒ String

Returns the LLM system prompt for the task.

Parameters:

  • creator (Object)

    The creator of the task (polymorphic association)

  • args (Hash)

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

Returns:

  • (String)

    The LLM system prompt for the task.



124
125
126
# File 'app/models/raif/task.rb', line 124

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

Instance Method Details

#build_promptObject

Raises:

  • (NotImplementedError)


136
137
138
# File 'app/models/raif/task.rb', line 136

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

#build_system_promptObject



140
141
142
143
144
145
# File 'app/models/raif/task.rb', line 140

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

#re_runObject



105
106
107
108
# File 'app/models/raif/task.rb', line 105

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

#run(skip_prompt_population: false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/raif/task.rb', line 81

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

  populate_prompts unless skip_prompt_population
  messages = [{ "role" => "user", "content" => message_content }]

  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



36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/raif/task.rb', line 36

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