Agents 0.0.2
Edge AI Agents SDK
Loading...
Searching...
No Matches
parallelization_workflow.h
1
10#pragma once
11
12#include <agents-cpp/workflow.h>
13#include <functional>
14#include <vector>
15
16namespace agents {
17namespace workflows {
18
28public:
33 enum class Strategy {
42 };
43
48 struct Task {
64 std::function<String(const String&)> prompt_fn;
68 std::function<JsonObject(const String&)> result_parser;
69
79 const String& name,
81 const JsonObject& context = JsonObject(),
82 std::function<String(const String&)> prompt_fn = nullptr,
83 std::function<JsonObject(const String&)> result_parser = nullptr
85 };
86
93 std::shared_ptr<AgentContext> context,
95 );
96
101 void addTask(const Task& task);
102
110 const String& name,
111 const String& prompt_template,
112 const JsonObject& context = JsonObject()
113 );
114
124 const String& name,
125 const String& prompt_template,
126 std::function<String(const String&)> prompt_fn,
127 std::function<JsonObject(const String&)> result_parser,
128 const JsonObject& context = JsonObject()
129 );
130
135 void setAggregator(std::function<JsonObject(const std::vector<JsonObject>&)> aggregator);
136
141 void setStrategy(Strategy strategy);
142
146 void init();
147
152 void setVotingThreshold(double threshold);
153
159 JsonObject run(const String& input) override;
160
166
167private:
171 std::vector<Task> tasks_;
172
176 Strategy strategy_;
177
181 std::function<JsonObject(const std::vector<JsonObject>&)> aggregator_;
182
186 double voting_threshold_ = 0.5;
187
191 std::shared_ptr<LLMInterface> llm_;
192
198 std::vector<JsonObject> runTasksInParallel(const String& input);
199
205 static JsonObject defaultSectioningAggregator(const std::vector<JsonObject>& results);
206
212 static JsonObject defaultVotingAggregator(const std::vector<JsonObject>& results);
213};
214
215} // namespace workflows
216} // namespace agents
ParallelizationWorkflow(std::shared_ptr< AgentContext > context, Strategy strategy=Strategy::SECTIONING)
Constructor with context.
void addTask(const Task &task)
Add a task to the workflow.
void setStrategy(Strategy strategy)
Set the strategy.
void setAggregator(std::function< JsonObject(const std::vector< JsonObject > &)> aggregator)
Set the aggregation function for task results.
void addTask(const String &name, const String &prompt_template, std::function< String(const String &)> prompt_fn, std::function< JsonObject(const String &)> result_parser, const JsonObject &context=JsonObject())
Add a task to the workflow with generators and parser.
JsonObject run(const String &input) override
Execute the workflow with input.
Strategy
Enum for parallelization strategy.
Definition parallelization_workflow.h:33
@ VOTING
Run same task multiple times for consensus.
Definition parallelization_workflow.h:41
@ SECTIONING
Break task into independent subtasks.
Definition parallelization_workflow.h:37
void addTask(const String &name, const String &prompt_template, const JsonObject &context=JsonObject())
Add a task to the workflow with basic params.
void setVotingThreshold(double threshold)
Set the voting threshold (for VOTING mode)
void init()
Initialize the workflow.
JsonObject run()
Execute using the latest USER message from context memory.
Workflow(std::shared_ptr< AgentContext > context)
Constructor.
Worflows Namespace.
Definition workflow.h:22
Framework Namespace.
Definition agent.h:18
nlohmann::json JsonObject
JSON object type.
Definition types.h:39
std::string String
String type.
Definition types.h:27
Task definition for parallel execution.
Definition parallelization_workflow.h:48
std::function< String(const String &)> prompt_fn
The prompt function of the task.
Definition parallelization_workflow.h:64
std::function< JsonObject(const String &)> result_parser
The result parser of the task.
Definition parallelization_workflow.h:68
JsonObject context
The context of the task.
Definition parallelization_workflow.h:60
String name
The name of the task.
Definition parallelization_workflow.h:52
Task(const String &name, const String &prompt_template, const JsonObject &context=JsonObject(), std::function< String(const String &)> prompt_fn=nullptr, std::function< JsonObject(const String &)> result_parser=nullptr)
Constructor.
Definition parallelization_workflow.h:78
String prompt_template
The prompt template of the task.
Definition parallelization_workflow.h:56