-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreflection.tsx
64 lines (58 loc) · 1.91 KB
/
reflection.tsx
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
import * as gensx from "@gensx/core";
export interface ReflectionOutput {
feedback: string;
continueProcessing: boolean;
}
interface ReflectionProps<TInput> {
// The initial input to process
input: TInput;
// Component to process the input and generate new output
ImproveFn: gensx.GsxComponent<{ input: TInput; feedback: string }, TInput>;
// Component to evaluate if we should continue processing and provide feedback
EvaluateFn: gensx.GsxComponent<{ input: TInput }, ReflectionOutput>;
// Current iteration count
iterations?: number;
// Maximum number of iterations allowed
maxIterations?: number;
}
/**
* A generic component for implementing reflection-based processing.
* It will continue processing the input until either:
* 1. the evaluate function returns false
* 2. maxIterations is reached
*/
export function createReflectionLoop<TInput>(name: string) {
return gensx.Component<ReflectionProps<TInput>, TInput>(
name,
async ({
input,
ImproveFn,
EvaluateFn,
iterations = 0,
maxIterations = 3,
}) => {
// Check if we should continue processing
const { feedback, continueProcessing } =
await gensx.execute<ReflectionOutput>(<EvaluateFn input={input} />);
if (continueProcessing && iterations < maxIterations) {
// Process the input
const newInput: TInput = await gensx.execute<TInput>(
<ImproveFn input={input} feedback={feedback} />,
);
// Recursive call with updated input and iteration count
const Reflection = createReflectionLoop<TInput>(name);
return (
<Reflection
input={newInput}
ImproveFn={ImproveFn}
EvaluateFn={EvaluateFn}
iterations={iterations + 1}
maxIterations={maxIterations}
/>
);
}
// Return the final input when we're done processing
return input;
},
);
}