Class: Raif::Agent

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#on_conversation_history_entryObject

Returns the value of attribute on_conversation_history_entry.



61
62
63
# File 'app/models/raif/agent.rb', line 61

def on_conversation_history_entry
  @on_conversation_history_entry
end

Instance Method Details

#run!(&block) ⇒ Raif::Agent

Runs the agent and returns a Raif::Agent. If a block is given, it will be called each time a new entry is added to the agent’s conversation history. The block will receive the Raif::Agent and the new entry as arguments: agent = Raif::Agent.new(

task: task,
tools: [Raif::ModelTools::WikipediaSearch, Raif::ModelTools::FetchUrl],
creator: creator

)

agent.run! do |conversation_history_entry|

Turbo::StreamsChannel.broadcast_append_to(
  :my_agent_channel,
  target: "agent-progress",
  partial: "my_partial_displaying_agent_progress",
  locals: { agent: agent, conversation_history_entry: conversation_history_entry }
)

end

The conversation_history_entry will be a hash with “role” and “content” keys: { “role” => “assistant”, “content” => “a message here” }

Parameters:

  • block (Proc)

    Optional block to be called each time a new entry to the agent’s conversation history is generated

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
130
131
132
133
134
135
136
# File 'app/models/raif/agent.rb', line 86

def run!(&block)
  self.on_conversation_history_entry = block_given? ? block : nil
  self.started_at = Time.current
  save!

  logger.debug <<~DEBUG
    --------------------------------
    Starting Agent Run
    --------------------------------
    System Prompt:
    #{system_prompt}

    Task: #{task}
  DEBUG

  add_conversation_history_entry({ role: "user", content: task })

  while iteration_count < max_iterations
    update_columns(iteration_count: iteration_count + 1)

    model_completion = llm.chat(
      messages: conversation_history,
      source: self,
      system_prompt: system_prompt,
      available_model_tools: native_model_tools
    )

    logger.debug <<~DEBUG
      --------------------------------
      Agent iteration #{iteration_count}
      Messages:
      #{JSON.pretty_generate(conversation_history)}

      Response:
      #{model_completion.raw_response}
      --------------------------------
    DEBUG

    process_iteration_model_completion(model_completion)
    break if final_answer.present?
  end

  completed!
  final_answer
rescue StandardError => e
  self.failed_at = Time.current
  self.failure_reason = e.message
  save!

  raise
end