-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarter_batch_node.cpp
More file actions
67 lines (58 loc) · 1.92 KB
/
Copy pathstarter_batch_node.cpp
File metadata and controls
67 lines (58 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <string>
#include "nodes/authoring.h"
namespace llm_edgeflow {
namespace custom_nodes {
namespace starter_batch {
struct Inputs {
const TextBatch* questions = nullptr;
const TextBatch* context = nullptr;
};
struct Options {
bool retry_once = false;
};
struct Models {
LlmCall generator;
};
// All request values stay local to this ordinary business function.
NodeResult<TextBatch> Run(const Inputs& inputs, const Options& options,
const Models& models) {
TextBatch prompts;
prompts.reserve(inputs.questions->size());
for (const auto& question : *inputs.questions) {
std::string prompt;
if (inputs.context) {
for (const auto& context : *inputs.context) {
if (context.req_id == question.req_id) prompt += context.data + "\n";
}
}
prompts.emplace_back(question.req_id, question.sub_id,
prompt + question.data);
}
auto result = models.generator.Generate(prompts);
if (!result.ok() && options.retry_once &&
result.failure().kind == NodeErrorKind::kModelCallError) {
return models.generator.Generate(prompts);
}
return result;
}
auto Spec() {
return MakeBatchSpec(
InputsOf<Inputs>({
Required("questions", &Inputs::questions),
Optional("context", &Inputs::context,
InputFlow::AggregateByRequest),
}),
PreservedOutput<TextBatch>("output", "questions"),
Parameters<Options>({
Field("retry_once", &Options::retry_once).Default(false),
}),
ModelsOf<Models>({
Llm("generator", "bind_model", &Models::generator),
}),
&Run)
.Description("Batch starter with optional request context and one retry");
}
REGISTER_FUNCTION_NODE(StarterBatchNode, Spec());
} // namespace starter_batch
} // namespace custom_nodes
} // namespace llm_edgeflow