Class: Raif::Agent
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Raif::Agent
- Includes:
- Concerns::AgentInferenceStats, Concerns::HasAvailableModelTools, Concerns::HasLlm, Concerns::HasRequestedLanguage, Concerns::InvokesModelTools, Concerns::RunWith
- Defined in:
- app/models/raif/agent.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#on_conversation_history_entry ⇒ Object
Returns the value of attribute on_conversation_history_entry.
Instance Method Summary collapse
- #final_iteration? ⇒ Boolean
-
#run!(&block) ⇒ Raif::Agent
Runs the agent and returns a Raif::Agent.
Methods included from Concerns::AgentInferenceStats
#total_completion_tokens, #total_cost, #total_output_token_cost, #total_prompt_token_cost, #total_prompt_tokens, #total_tokens_sum
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
table_name_prefix, where_json_not_blank
Instance Attribute Details
#on_conversation_history_entry ⇒ Object
Returns the value of attribute on_conversation_history_entry.
69 70 71 |
# File 'app/models/raif/agent.rb', line 69 def on_conversation_history_entry @on_conversation_history_entry end |
Instance Method Details
#final_iteration? ⇒ Boolean
153 154 155 |
# File 'app/models/raif/agent.rb', line 153 def final_iteration? iteration_count == max_iterations end |
#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” }
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/models/raif/agent.rb', line 94 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(Raif::Messages::UserMessage.new(content: task).to_h) while iteration_count < max_iterations update_columns(iteration_count: iteration_count + 1) # Update the system prompt on each iteration in case it has changed since the last iteration self.system_prompt = build_system_prompt # Hook for subclasses to perform actions before the LLM chat (e.g., add warnings) before_iteration_llm_chat model_completion = llm.chat( messages: conversation_history, source: self, system_prompt: system_prompt, available_model_tools: native_model_tools, tool_choice: tool_choice_for_iteration ) 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. save! raise end |