This project implements a multi-agent LLM framework for explaining AC Optimal Power Flow (AC-OPF) results in a form that is useful and accessible to the system operator. It is designed to translate complex OPF outputs into clear, structured, and operationally relevant explanations that support decision-making in power system analysis.
The framework uses a multi-agent architecture in which different LLM roles collaborate to generate, critique, refine, and evaluate explanations of AC-OPF snapshots.
This design enables iterative improvement of explanation quality and helps produce more consistent, informative, and operator-oriented outputs.
The pipeline is compatible with OpenAI GPT-5.4 through the API wrapper defined in llm_utils.py.
All LLM-based explanation, critique, distillation, and refinement steps are executed using GPT-5.4 as the underlying language model.
This repository is intended for building an LLM-ready explanation pipeline for AC-OPF results, with a focus on interpretability, human-in-the-loop refinement, and practical support for power system operators.
This project generates OPF snapshot datasets, runs an LLM-based training pipeline to improve explanation quality, tests the resulting prompt, and evaluates the outputs.
- Replace your organization API key in
llm_utils.py - Run
run_poa.pyto generate warm-start, training, and test data - Run
mainppeline.pyto run the training/refinement pipeline - Run
testing.pyto generate test explanations - Run
evaluator.pyto evaluate and score the results
Before running anything, open:
llm_utils.pyReplace the existing placeholder or API key value with your own organization API key.
Run:
python run_poa.pyThis script generates warm-start, training, and test snapshots for the selected case.
warm_snapshots_case39_cf.jsontrain_snapshots_case39_cf.jsontest_snapshots_case39_cf.jsontest_snapshots_case39_cf2.json
Run:
python mainppeline.pyThis script runs the Actor / Critic / Refiner pipeline.
When prompted, choose one of the following:
1= No human intervention2= Human in the loop3= Human feedback visible to AI Critic
actor_prompt_base.txtcritic1_prompt.txtrefiner_prompt.txtdistiller_prompt.txt
warmstart_examples.jsontrain_snapshots_case39_cf2.json
actor_prompt_1.txtactor_prompt_2.txtactor_prompt_3.txtall_distilled_rules_1.jsonall_distilled_rules_2.jsonall_distilled_rules_3.jsonactor_explanations_1.jsonactor_explanations_2.jsonactor_explanations_3.json
Run:
python testing.pyCurrent test execution uses:
- input prompt:
actor_prompt_base.txt - output file:
test_actor_explanations_base.json
Run:
python evaluator.pyThis script performs evaluation and scoring of the generated test explanations.
python run_poa.py
python mainppeline.py
python testing.py
python evaluator.py- Make sure the API key in
llm_utils.pyis valid before running any script. mainppeline.pyexpects:warmstart_examples.jsontrain_snapshots_case39_cf2.json
- If your generated filenames are different, rename them or update the paths inside the script.
- The pipeline expects input entries with an
opf_summaryfield.
llm_utils.pyrun_poa.pymainppeline.pytesting.pyevaluator.py
actor_prompt_base.txtcritic1_prompt.txtrefiner_prompt.txtdistiller_prompt.txt
warm_snapshots_case39_cf.jsontrain_snapshots_case39_cf.jsontest_snapshots_case39_cf.jsontest_snapshots_case39_cf2.json
actor_prompt_1.txtactor_prompt_2.txtactor_prompt_3.txtall_distilled_rules_1.jsonall_distilled_rules_2.jsonall_distilled_rules_3.jsonactor_explanations_1.jsonactor_explanations_2.jsonactor_explanations_3.json
test_actor_explanations_base.json
pip install -r requirements.txt
## Citation
This repository contains the code accompanying the accepted paper:
F. Moaidi and R. J. Bessa, "Understanding OPF Redispatch Actions through LLM-Assisted
Counterfactual Analysis," 26th International Conference on Environment
and Electrical Engineering & 10th Industrial and Commercial Power Systems Europe,
Lisbon, Portugal, 29 June – 02 July, 2026.
## License
This code is released under the Mozilla Public License 2.0 (MPL-2.0).
You may use, modify, and redistribute this software under the terms of
the MPL 2.0. See the LICENSE file for details.