Inference code
from transformers import AutoModelForCausalLM, AutoTokenizer
system_prompt = """
You are an expert in power system analysis and control. Now you are required to adjust a power flow from non-convergent to converged. Some rules are as follows.
1. You are only able to adjust P & V of PV generators and V of slack generators.
2. You can only adjust one variable at one generation.
3. Please generate the output in JSON format. Example is as: {"bus_id": 10, "adjustment_percentage": 15, "var_type": "Q"} where adjustment_percentage means the percentage of your adjustment, 15 means 1 + 0.15. You can choose another bus_id/adjustment_percentage/var_type based on your observation. You don't have to repeat 15 as percentage.
You should think of this problem based on your observation. Try to solve it using the knowledge of power systems. Your thinking should be clear and concise.
You should think about the problem and provide your working out. Remember, you can only adjust one parameter at one time. Do not think about multiple power flow iterations by your imagination.
Your thinking progress should be placed between <think> and </think>, and <think> must be front of </think>. Do not repeat the observation provided by the user. You only have the observations that user provided to you. So never give fake observations such as "tensor". Then, provide your solution in JSON format between <answer></answer>
"""
user_query = """
System ref generator is at [30.], PV generators are at [29. 31. 32. 33. 34. 35. 36. 37. 38.]. System status:
ref voltage: [0.8096701083840001]
PV voltage: [1.2442295706222575, 0.5118089395202785, 0.45347289095802085, 1.1519981930004022, 1.1088196329508058, 1.2773623280000002, 0.3933137714680207, 2.5447325675389645, 0.7415253385671602]
Active load: [97.6, 0.0, 322.0, 500.0, 0.0, 0.0, 233.8, 522.0, 6.5, 0.0, 0.0, 8.53, 0.0, 0.0, 320.0, 329.0, 0.0, 158.0, 0.0, 680.0, 274.0, 0.0, 247.5, 308.6, 224.0, 139.0, 281.0, 206.0, 283.5, 0.0, 9.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1104.0]
Reactive load: [44.2, 0.0, 2.4, 184.0, 0.0, 0.0, 84.0, 176.6, -66.6, 0.0, 0.0, 88.0, 0.0, 0.0, 153.0, 32.3, 0.0, 30.0, 0.0, 103.0, 115.0, 0.0, 84.6, -92.2, 47.2, 17.0, 75.5, 27.6, 26.9, 0.0, 4.6, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 250.0]
Generator active power: [852.0579722117993, 677.871, 1147.047809671296, 564.67834564, 1093.9101100599712, 1000.233, 733.8568204800001, 522.71206416, 230.27870743971766, 1045.090269655856]
In power flow sensitivity, ref voltage sensitivity is tensor([7.0194e+21], dtype=torch.float64), PV voltage sensitivity is tensor([ 7.2196e+19, 1.7284e+22, -6.5354e+21, -2.6708e+21, 6.7642e+21,
-9.8012e+20, -1.4130e+21, 9.1309e+19, 2.7271e+21],
dtype=torch.float64), PV active power sensitivity is tensor([-1.2828e+18, 8.3952e+17, -2.1504e+18, -2.2296e+18, -2.2199e+18,
-2.1236e+18, -1.0730e+18, -4.5789e+17, -1.0233e+18],
dtype=torch.float64).
Power flow is not converged, mismatch is 86149.68274090989。
"""
model_path = "Sheparddyan/PowerFlowLLM"
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, device_map="cuda:0")
tokenizer = AutoTokenizer.from_pretrained(model_path)
chat = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_query}
]
chat_input = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
input_ids = tokenizer(chat_input, return_tensors="pt").input_ids.to("cuda")
output_ids = model.generate(
input_ids=input_ids,
max_new_tokens=8192,
do_sample=True,
temperature=0.7,
top_p=0.99,
)
print(tokenizer.decode(output_ids[0]))