-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaisac_compatible_amrex_agent_code.py
More file actions
144 lines (124 loc) · 4.2 KB
/
aisac_compatible_amrex_agent_code.py
File metadata and controls
144 lines (124 loc) · 4.2 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""AISAC-compatible AMReX logic for knowledge and demo actions."""
from __future__ import annotations
import json
from typing import Any
from src.interactive_service import invoke_tool
DEFAULT_SOLVER = "PeleLMeX"
DEFAULT_BASELINE_OVERRIDE = "PeleLMeX/Exec/Production/JetInCrossflow"
def _parse_context(context: str | dict[str, Any] | None) -> dict[str, Any]:
if context is None:
return {}
if isinstance(context, dict):
return dict(context)
raw = context.strip()
if not raw:
return {}
try:
parsed = json.loads(raw)
except json.JSONDecodeError:
return {}
return parsed if isinstance(parsed, dict) else {}
def process_task(task: str, context: str | dict[str, Any] | None = "{}") -> dict[str, Any]:
"""
Run a knowledge query and return AISAC-friendly response fields.
Inputs:
- task: user question
- context (optional JSON/dict): may include "code" to select solver
"""
context_dict = _parse_context(context)
payload: dict[str, Any] = {"question": task}
solver = (
context_dict.get("selected_solver")
or context_dict.get("code_name")
or context_dict.get("code")
or DEFAULT_SOLVER
)
payload["code"] = solver
result = invoke_tool(
"query_knowledge",
payload,
surface="aisac",
caller_action="amrex_knowledge_agent",
)
base_answer = str(result.get("answer", "No answer available"))
method = result.get("method", "unknown")
confidence = result.get("confidence", 0.0)
sources = result.get("sources", [])
sources_list = [str(src) for src in sources] if sources else []
source_lines = (
"\n".join(f"- `{src}`" for src in sources_list)
if sources_list
else "- None provided"
)
answer = (
"# AMReX Knowledge Response\n\n"
"## Answer\n\n"
f"{base_answer}\n\n"
"## Metadata\n\n"
f"- Solver: `{solver}`\n"
f"- Method: `{method}`\n"
f"- Confidence: `{confidence}`\n\n"
"## Sources\n\n"
f"{source_lines}"
)
rationale = "Answered via mcp_query_knowledge."
return {
"answer": answer,
"rationale": rationale,
}
def run_demo_workflow(
example: str,
context: str | dict[str, Any] | None = "{}",
) -> dict[str, Any]:
"""Run a constrained demo workflow on Perlmutter in dry-run mode."""
context_dict = _parse_context(context)
key = (context_dict.get("example") or "ad_hoc").strip()
prompt = context_dict.get("prompt")
if not prompt:
prompt = (example or "").strip()
if not prompt:
raise ValueError("Missing prompt. Provide text as first argument or context.prompt.")
baseline_override = context_dict.get("baseline_override") or DEFAULT_BASELINE_OVERRIDE
payload: dict[str, Any] = {
"prompt": prompt,
"baseline_override": baseline_override,
"steps": ["create_simulation_plan", "run_simulation"],
"submit": {
"system": "perlmutter",
"dry_run": True,
},
"config_overrides": {
"environment": "perlmutter",
"run_mode": "dry",
"dry_run": True,
},
}
result = invoke_tool(
"execute_workflow",
payload,
surface="aisac",
caller_action="amrex_demo_agent",
)
final = result.get("final", {}) if isinstance(result, dict) else {}
run_dir = final.get("run_directory")
job_status = final.get("job_status")
selected_case = final.get("selected_case")
answer = (
"# AMReX Demo Workflow Result\n\n"
"## Summary\n\n"
"Executed constrained demo workflow in dry-run mode on Perlmutter.\n\n"
"## Run Details\n\n"
f"- Example: `{key}`\n"
f"- Selected Case: `{selected_case}`\n"
f"- Job Status: `{job_status}`\n"
f"- Run Directory: `{run_dir}`\n"
f"- Baseline Override: `{baseline_override}`\n"
"- Steps: `create_simulation_plan`, `run_simulation`\n"
)
return {
"answer": answer,
"rationale": (
"Ran execute_workflow with steps=create_simulation_plan+run_simulation "
"using dry-run Perlmutter settings."
),
}