Class: RCrewAI::Crew
- Inherits:
-
Object
- Object
- RCrewAI::Crew
- Includes:
- AsyncExtensions
- Defined in:
- lib/rcrewai/crew.rb
Instance Attribute Summary collapse
-
#agents ⇒ Object
readonly
Returns the value of attribute agents.
-
#consensus_agents ⇒ Object
readonly
Returns the value of attribute consensus_agents.
-
#knowledge ⇒ Object
readonly
Returns the value of attribute knowledge.
-
#last_inputs ⇒ Object
readonly
Returns the value of attribute last_inputs.
-
#max_iterations ⇒ Object
Returns the value of attribute max_iterations.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#process_type ⇒ Object
readonly
Returns the value of attribute process_type.
-
#stream_sink ⇒ Object
readonly
Returns the value of attribute stream_sink.
-
#tasks ⇒ Object
readonly
Returns the value of attribute tasks.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
- #add_agent(agent) ⇒ Object
- #add_task(task) ⇒ Object
-
#after_kickoff(&block) ⇒ Object
Register a callback run after execution.
-
#before_kickoff(&block) ⇒ Object
Register a callback run before execution.
- #execute(async: false, stream: nil, inputs: {}, **async_options, &block) ⇒ Object
- #execute_async(**options) ⇒ Object
- #execute_sync ⇒ Object
-
#initialize(name, **options) ⇒ Crew
constructor
A new instance of Crew.
-
#kickoff_for_each(inputs:) ⇒ Object
Runs the crew once per input set, returning one result per input in order.
- #planning? ⇒ Boolean
- #process=(new_process) ⇒ Object
- #save ⇒ Object
-
#test(n_iterations:, scorer: nil, model: nil) ⇒ Object
Runs the crew repeatedly and scores each run.
-
#train(n_iterations:, filename:, feedback: nil) ⇒ Object
Runs the crew repeatedly, collecting feedback after each iteration and persisting it to
filenameas JSON.
Methods included from AsyncExtensions
#async_execution?, included, #thread_id
Constructor Details
#initialize(name, **options) ⇒ Crew
Returns a new instance of Crew.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rcrewai/crew.rb', line 15 def initialize(name, **) @name = name @agents = [] @tasks = [] @process_type = .fetch(:process, :sequential) @verbose = .fetch(:verbose, false) @max_iterations = .fetch(:max_iterations, 10) @planning = .fetch(:planning, false) @planning_llm = [:planning_llm] @planned = false @consensus_agents = .fetch(:consensus_agents, 3) @knowledge = build_knowledge([:knowledge], [:knowledge_sources]) @before_kickoff_hooks = [] @after_kickoff_hooks = [] @last_inputs = {} @process_instance = nil validate_process_type! end |
Instance Attribute Details
#agents ⇒ Object (readonly)
Returns the value of attribute agents.
12 13 14 |
# File 'lib/rcrewai/crew.rb', line 12 def agents @agents end |
#consensus_agents ⇒ Object (readonly)
Returns the value of attribute consensus_agents.
34 35 36 |
# File 'lib/rcrewai/crew.rb', line 34 def consensus_agents @consensus_agents end |
#knowledge ⇒ Object (readonly)
Returns the value of attribute knowledge.
34 35 36 |
# File 'lib/rcrewai/crew.rb', line 34 def knowledge @knowledge end |
#last_inputs ⇒ Object (readonly)
Returns the value of attribute last_inputs.
34 35 36 |
# File 'lib/rcrewai/crew.rb', line 34 def last_inputs @last_inputs end |
#max_iterations ⇒ Object
Returns the value of attribute max_iterations.
13 14 15 |
# File 'lib/rcrewai/crew.rb', line 13 def max_iterations @max_iterations end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/rcrewai/crew.rb', line 12 def name @name end |
#process_type ⇒ Object (readonly)
Returns the value of attribute process_type.
12 13 14 |
# File 'lib/rcrewai/crew.rb', line 12 def process_type @process_type end |
#stream_sink ⇒ Object (readonly)
Returns the value of attribute stream_sink.
34 35 36 |
# File 'lib/rcrewai/crew.rb', line 34 def stream_sink @stream_sink end |
#tasks ⇒ Object (readonly)
Returns the value of attribute tasks.
12 13 14 |
# File 'lib/rcrewai/crew.rb', line 12 def tasks @tasks end |
#verbose ⇒ Object
Returns the value of attribute verbose.
13 14 15 |
# File 'lib/rcrewai/crew.rb', line 13 def verbose @verbose end |
Class Method Details
.create(name) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/rcrewai/crew.rb', line 146 def self.create(name) crew = new(name) crew.save puts "Crew '#{name}' created successfully!" crew end |
.list ⇒ Object
158 159 160 161 |
# File 'lib/rcrewai/crew.rb', line 158 def self.list # List all available crews %w[example_crew research_crew development_crew] end |
.load(name) ⇒ Object
153 154 155 156 |
# File 'lib/rcrewai/crew.rb', line 153 def self.load(name) # Load crew configuration from file new(name) end |
Instance Method Details
#add_agent(agent) ⇒ Object
54 55 56 |
# File 'lib/rcrewai/crew.rb', line 54 def add_agent(agent) @agents << agent end |
#add_task(task) ⇒ Object
58 59 60 |
# File 'lib/rcrewai/crew.rb', line 58 def add_task(task) @tasks << task end |
#after_kickoff(&block) ⇒ Object
Register a callback run after execution. Receives the result and may return a transformed result. Multiple hooks run in registration order.
49 50 51 52 |
# File 'lib/rcrewai/crew.rb', line 49 def after_kickoff(&block) @after_kickoff_hooks << block self end |
#before_kickoff(&block) ⇒ Object
Register a callback run before execution. Receives the inputs hash and may return a transformed hash. Multiple hooks run in registration order.
42 43 44 45 |
# File 'lib/rcrewai/crew.rb', line 42 def before_kickoff(&block) @before_kickoff_hooks << block self end |
#execute(async: false, stream: nil, inputs: {}, **async_options, &block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rcrewai/crew.rb', line 62 def execute(async: false, stream: nil, inputs: {}, **, &block) sinks = [] sinks << block if block_given? Array(stream).each { |s| sinks << s } if stream @stream_sink = sinks.empty? ? nil : RCrewAI::Events.fan_out(sinks) run_before_hooks(inputs) distribute_knowledge if @knowledge run_planning_pass if planning? result = async ? execute_async(**) : execute_sync run_after_hooks(result) end |
#execute_async(**options) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rcrewai/crew.rb', line 112 def execute_async(**) puts "Executing crew: #{name} (async #{process_type} process)" case process_type when :sequential execute_sequential_async(**) when :hierarchical execute_hierarchical_async(**) when :consensual execute_consensual_async(**) else raise ConfigurationError, "Async execution not implemented for #{process_type} process" end end |
#execute_sync ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rcrewai/crew.rb', line 127 def execute_sync puts "Executing crew: #{name} (#{process_type} process)" # Create appropriate process instance @process_instance = create_process_instance # Execute using the process results = @process_instance.execute # Return formatted results format_execution_results(results) end |
#kickoff_for_each(inputs:) ⇒ Object
Runs the crew once per input set, returning one result per input in order. Runs are isolated: each execution starts from only its own inputs.
79 80 81 |
# File 'lib/rcrewai/crew.rb', line 79 def kickoff_for_each(inputs:) Array(inputs).map { |input| execute(inputs: input) } end |
#planning? ⇒ Boolean
36 37 38 |
# File 'lib/rcrewai/crew.rb', line 36 def planning? @planning end |
#process=(new_process) ⇒ Object
140 141 142 143 144 |
# File 'lib/rcrewai/crew.rb', line 140 def process=(new_process) @process_type = new_process.to_sym validate_process_type! @process_instance = nil # Reset process instance end |
#save ⇒ Object
163 164 165 166 |
# File 'lib/rcrewai/crew.rb', line 163 def save # Save crew configuration to file true end |
#test(n_iterations:, scorer: nil, model: nil) ⇒ Object
Runs the crew repeatedly and scores each run. scorer is a callable
->(result) { Float }; it defaults to the run's success_rate.
Mirrors CrewAI's crew.test.
104 105 106 107 108 109 110 |
# File 'lib/rcrewai/crew.rb', line 104 def test(n_iterations:, scorer: nil, model: nil) # rubocop:disable Lint/UnusedMethodArgument scorer ||= ->(result) { result[:success_rate].to_f } scores = (1..n_iterations).map { scorer.call(execute) } average = scores.empty? ? 0.0 : (scores.sum / scores.length).round(2) { iterations: n_iterations, scores: scores, average_score: average } end |
#train(n_iterations:, filename:, feedback: nil) ⇒ Object
Runs the crew repeatedly, collecting feedback after each iteration and
persisting it to filename as JSON. feedback is a callable
->(iteration, result) { "..." }; it defaults to prompting a human.
Mirrors CrewAI's crew.train.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rcrewai/crew.rb', line 87 def train(n_iterations:, filename:, feedback: nil) feedback ||= method(:default_training_feedback) entries = [] (1..n_iterations).each do |iteration| result = execute note = feedback.call(iteration, result) entries << { iteration: iteration, feedback: note } end write_training_file(filename, entries) { iterations: n_iterations, filename: filename, entries: entries } end |