Table of Contents
Agents Overview
Raif also provides Raif::Agents::ReActAgent
, which implements a ReAct-style agent loop using tool calls:
# Create a new agent
agent = Raif::Agents::ReActAgent.new(
task: "Research the history of the Eiffel Tower",
available_model_tools: [Raif::ModelTools::WikipediaSearch, Raif::ModelTools::FetchUrl],
creator: current_user
)
# Run the agent and get the final answer
final_answer = agent.run!
# Or run the agent and monitor its progress
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
On each step of the agent loop, an entry will be added to the Raif::Agent#conversation_history
and, if you pass a block to the run!
method, the block will be called with the conversation_history_entry
as an argument. You can use this to monitor and display the agent’s progress in real-time.
The conversation_history_entry will be a hash with “role” and “content” keys:
{
"role" => "assistant",
"content" => "a message here"
}
Creating Custom Agents
You can create custom agents using the generator:
rails generate raif:agent WikipediaResearchAgent
This will create a new agent in app/models/raif/agents/wikipedia_research_agent.rb
:
module Raif
module Agents
class WikipediaResearchAgent < Raif::Agent
# If you want to always include a certain set of model tools with this agent type,
# uncomment this callback to populate the available_model_tools attribute with your desired model tools.
# before_create -> {
# self.available_model_tools ||= [
# Raif::ModelTools::WikipediaSearchTool,
# Raif::ModelTools::FetchUrlTool
# ]
# }
# Enter your agent's system prompt here. Alternatively, you can change your agent's superclass
# to an existing agent types (like Raif::Agents::ReActAgent) to utilize an existing system prompt.
def build_system_prompt
# TODO: Implement your system prompt here
end
# Each iteration of the agent loop will generate a new Raif::ModelCompletion record and
# then call this method with it as an argument.
def process_iteration_model_completion(model_completion)
# TODO: Implement your iteration processing here
end
end
end
end
Read next: Model Tools