๐How Does AdalFlow Work
The key idea of AdalFlow is text gradients. Here's the workflow in each iteration:

1. Forward Pass
It uses specified LLM and current prompt to generated the expected output.
To achieve this, users need to specify the task instruction, the prompt template and the expected output data schema as the example below:

2. Evaluation
By comparing the correct answer provided in the training data and LLM generated output, this step generates a numerical evaluation score. Users can specify the evaluation metrics, but has to be AdalFlow built-in metrics.
In this example, it's using G-Eval and LLM-as-Judge to calculate the evaluation score:

3. Loss Computation
This is the key step of text gradients. AdalFlow converts the numerical evaluation score to a textual feedback (text-based "gradient hints") to allow the text optimizer (another LLM) understand how to rewrite the prompt to improve the performance.

For example, you have context, question and the correct answer like below:

But the answer you got is "Pierre Curie". So you got a low evaluation score, such as 0.4 G-Eval score. Then AdalFlow converts this evaluation score to textual loss:
"The predicted answer incorrectly identified Pierre Curie instead of Marie Curie. Revise the prompt to encourage entity grounding in the given context and ensure the model extracts the most relevant factual entity."
This textual loss serves like a "gradient hint".
4. Backward Pass
AdalFlowโs backward engine reads that textual feedback and generates a new prompt, then the text optimizer rewrites the prompt. This is equivalent to a "gradient descent" step in traditional optimization, except the "gradient" is a textual instruction rather than a numeric vector.
In AdalFlow, you put the task instruction, the prompt template, the backward engine instruction and the optimizer instruction together like the example below:

The updated prompt will be used in the next iteration, and these 4 steps will be repeatedly executed until reaching to the convergence.
๐ป Click to see full code >>
As shown in the code, AdalFlow saves all the output in a JSON file, and you can get the optimized prompt from the record with the max val_scores.
Comparing the prompt before and after the optimization here, did you notice anything abnormal?

The overfitting! Now, would you like to explore more in detailed experiments? ๐
Last updated