Skip to content

Commit f6659a7

Browse files
committed
agent: Add experience database and manual page
Experience database provides a tool to search when finding solutions.
1 parent 5e99143 commit f6659a7

4 files changed

Lines changed: 891 additions & 138 deletions

File tree

doc/agent.rst

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
HypnotoadAgent: LLM-Assisted Mesh Generation
2+
============================================
3+
4+
The ``HypnotoadAgent`` provides an LLM-driven interface to the Hypnotoad
5+
mesh generator. It combines:
6+
7+
- Structured tool calls (validate, run, inspect)
8+
- A searchable options reference
9+
- A growing database of past experience
10+
- Interactive or notebook-based workflows
11+
12+
The agent is designed to help users explore the settings space efficiently,
13+
diagnose mesh quality issues, and improve results over time.
14+
15+
Overview
16+
--------
17+
18+
The agent wraps Hypnotoad’s normal workflow:
19+
20+
1. Inspect equilibrium geometry
21+
2. Choose mesh settings
22+
3. Validate settings
23+
4. Run mesh generation
24+
5. Inspect mesh quality
25+
6. Iterate until acceptable
26+
27+
The LLM can call tools to perform these steps programmatically, rather than
28+
guessing option names or values.
29+
30+
Basic Usage
31+
-----------
32+
33+
Create an agent by providing a GEQDSK (or equivalent) equilibrium file:
34+
35+
.. code-block:: python
36+
37+
from hypnotoad.agent import HypnotoadAgent
38+
39+
agent = HypnotoadAgent(
40+
gridfile="example.geqdsk",
41+
model="gpt-4o-mini",
42+
embedding_model="text-embedding-3-large",
43+
experience_db="experience_store"
44+
)
45+
46+
Then interact with it:
47+
48+
.. code-block:: python
49+
50+
response = agent.chat(
51+
"Generate a mesh for this equilibrium with high resolution near the X-point."
52+
)
53+
print(response)
54+
55+
In a Jupyter notebook, use:
56+
57+
.. code-block:: python
58+
59+
agent.chat_nb("Create a double-null mesh with refined SOL resolution")
60+
61+
This displays tool calls and outputs in collapsible sections.
62+
63+
Main Tools
64+
----------
65+
66+
The agent exposes the following tools to the LLM:
67+
68+
get_equilibrium_info
69+
^^^^^^^^^^^^^^^^^^^^
70+
71+
Describes magnetic topology, X-point locations, and geometric scale.
72+
Call this at the start of a session to inform resolution choices.
73+
74+
validate_settings
75+
^^^^^^^^^^^^^^^^^
76+
77+
Checks a settings dictionary against Hypnotoad’s options schema:
78+
79+
- Unknown option names
80+
- Type mismatches
81+
- Violations of allowed ranges
82+
- Constraint failures
83+
84+
Always validate before running Hypnotoad.
85+
86+
run_hypnotoad
87+
^^^^^^^^^^^^^
88+
89+
Runs the mesh generator with a given settings dictionary.
90+
91+
Returns:
92+
93+
- ``status`` (success or error)
94+
- ``mesh_index`` (for later inspection)
95+
- ``diagnostics`` (summary metrics and warnings)
96+
97+
inspect_mesh
98+
^^^^^^^^^^^^
99+
100+
Examines mesh quality at different detail levels:
101+
102+
- ``summary`` — global pass/fail and warnings
103+
- ``standard`` — per-region statistics and connectivity
104+
- ``full`` — cell-level diagnostics
105+
106+
Typical workflow:
107+
108+
.. code-block:: python
109+
110+
validate_settings(...)
111+
run_hypnotoad(...)
112+
inspect_mesh(detail="summary")
113+
114+
search_hypnotoad_options
115+
^^^^^^^^^^^^^^^^^^^^^^^^^
116+
117+
Searches the settings reference using BM25 keyword search.
118+
119+
Use this when:
120+
121+
- You do not know the exact option name
122+
- Validation reports an unknown key
123+
- You want to understand default values or allowed ranges
124+
125+
Do not guess option names.
126+
127+
search_experience
128+
^^^^^^^^^^^^^^^^^
129+
130+
Searches previously stored mesh-generation experience.
131+
132+
Use this when:
133+
134+
- You encounter a warning or error
135+
- You are working with a similar topology
136+
- You want to see what worked before
137+
138+
This enables the agent to improve over time.
139+
140+
Experience Database
141+
-------------------
142+
143+
The agent can store summaries of successful (and failed) runs in a
144+
persistent FAISS-based vector database.
145+
146+
After generating a mesh, call:
147+
148+
.. code-block:: python
149+
150+
agent.add_experience_report()
151+
agent.save_experience()
152+
153+
Each experience entry includes:
154+
155+
- Topology and goal
156+
- Overrides (differences from defaults)
157+
- Key lessons (symptom → change → outcome)
158+
- Diagnostics summary
159+
160+
On future runs, the agent can retrieve similar experiences and reuse
161+
successful parameter combinations.
162+
163+
Session History
164+
---------------
165+
166+
All meshes generated in a session are stored in memory:
167+
168+
.. code-block:: python
169+
170+
agent.list_meshes()
171+
172+
You can inspect or plot the most recent mesh:
173+
174+
.. code-block:: python
175+
176+
agent.inspect_mesh(mesh_index=-1, detail="standard")
177+
agent.plot_last_mesh()
178+
179+
Best Practices
180+
--------------
181+
182+
1. Always call ``get_equilibrium_info`` at the start.
183+
2. Use ``search_hypnotoad_options`` rather than guessing option names.
184+
3. Validate settings before running.
185+
4. Inspect at ``summary`` level before requesting ``full`` detail.
186+
5. Change only a few options per iteration.
187+
6. Save experience after successful runs.
188+
189+
Limitations
190+
-----------
191+
192+
- The agent does not automatically delete meshes from history.
193+
- The experience database is append-only.
194+
- Large conversations may exceed model context limits.
195+
- Mesh quality metrics depend on Hypnotoad’s diagnostics.
196+
197+
Future Extensions
198+
-----------------
199+
200+
Possible enhancements include:
201+
202+
- Hybrid search (BM25 + vector) over options and experience
203+
- Automatic option-diff computation for experience entries
204+
- Objective-driven optimisation loops
205+
- Integration with CI pipelines
206+

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Welcome to hypnotoad's documentation!
2121
tips-and-tricks
2222
nonorthogonal-tips
2323
utilities
24+
agent
2425
provenance-tracking
2526
_apidoc/modules
2627
other-configurations

0 commit comments

Comments
 (0)