17. Working with AI (MaiML, vocabularies, schemas, ELN entries)
MaiML mappings, vocabularies, metadata schemas and ELN entries all take effort to write from scratch. Letting an AI draft them is fast, but AI mixes in plausible mistakes (IRIs that do not exist, random uuids, wrong units). This chapter describes a workflow where the AI drafts and machines and people check. The BENTEN MaiML (XAFS, HAXPES) is used as the example.
Who does what
| Who | Does | In this toolkit |
|---|---|---|
| AI | Drafts (mappings, term candidates, the schema Excel, proposed edits) | Claude Code, Claude Desktop (chapter 16) |
| Machine | Checks structure and links | maiml check (chapter 10), benten_schema check, schema validation (chapter 15), the VS Code XML and YAML extensions |
| People | Decide meaning and what may be published | How terms map (exactMatch or broadMatch), which terms to add, the publication tags |
Treat AI output as a draft until it passes the machine checks. Even then, people decide whether the meaning is right.
Build vocabularies on existing ones
Every facility needs a "local dictionary" of techniques, detection modes, sample forms and materials. Building one from scratch means owning every definition, alias, translation and mapping yourself, which rarely lasts. Start from an existing vocabulary and add only what is missing (the "+α"). AI is good at finding what is missing and proposing candidates.
| Base | Covers | What BENTEN added (+α) |
|---|---|---|
| PaNET (photon and neutron techniques) | XAFS, EXAFS, XANES, HAXPES, … | Detection modes (transmission, fluorescence, TEY, CEY) mapped to PaNET; how XAFS relates to PaNET01197 |
| MatVoc (NIMS materials vocabulary) | Materials, elements, energy levels | A materials vocabulary with IDs and URIs for SPring-8 samples not in MatVoc; a dictionary of spelling variants (supplier names, middle dots in formulas, purity such as 3N); sample forms (powder, foil, lump, …) |
PaNET + α: map missing terms to a nearby term, with a strength
PaNET has no term for transmission mode or conversion electron yield (CEY). Instead of inventing new terms, keep the nearest PaNET term and how close it is.
| BENTEN | PaNET | Mapping |
|---|---|---|
| XAFS | PaNET01196 x-ray absorption spectroscopy | exactMatch |
| EXAFS | PaNET01198 | exactMatch |
| XANES | PaNET01199 | exactMatch |
| HAXPES | PaNET01103 | exactMatch |
| Total electron yield (TEY) | PaNET01220 | exactMatch |
| Transmission | PaNET01227 x-ray absorption | broadMatch (+α) |
| Fluorescence | PaNET01177 x-ray fluorescence-based techniques | related (+α) |
| Conversion electron yield (CEY) | PaNET01220 | broadMatch (+α) |
The AI may propose the mapping strength (SKOS exactMatch, closeMatch, broadMatch, related), but people decide it. In MaiML, write the technique as a PaNET IRI (b.uri("ex:technique", "http://purl.org/pan-science/PaNET/PaNET01196"), chapter 10).
MatVoc + α: use your own ID for missing materials, link later
Of the 751 materials among BENTEN's raw materials and HAXPES reference samples, 607 were in MatVoc and 144 were not. Do not wait for the missing ones: give them your own ID and URI, use them in MaiML, and list them as proposals for MatVoc. Once MatVoc registers one and it gets a QID, link your term to it with exactMatch. Keep your own ID, so MaiML files already written stay valid.
The MatVoc candidate search was split between machine and people:
- Machine: search MatVoc by name and formula and write the candidates with a confidence (high, medium, low) to Excel (nothing is written to the ELN)
- People: review the candidates in Excel and correct the "adopted QID" column
- Machine: write only the decided QIDs to the ELN, after checking them with a dry run
Make the spelling-variant dictionary part of the +α
Before matching, define rules that normalise spelling variants. For BENTEN's 693 raw materials, formula mismatches dropped from 122 to 16.
- Aliases of supplier names (Japanese and English company names → one id)
- Remove middle dots and trailing notes such as
(rutile)from formulas, and compare element counts - Compare purity as numbers (3N = 99.9)
Use the same rules for matching, AI input assistance and MaiML export. If each place keeps its own rules, the same material becomes different things in different places.
Notes on using vocabularies
- Check each vocabulary's terms of use and redistribution (PaNET is CC BY 4.0; BENTEN keeps its MatVoc copy internal and writes only IRIs into MaiML it publishes)
- Fix the shape of your own URIs before publishing them (BENTEN uses
https://support.jasri.jp/benten/vocab/material/<ID>). Changing it later leaves exported MaiML pointing at old URIs
Designing MaiML with AI
Ask the AI for a draft of the table below. In BENTEN, the AI produced this table from the ELN fields, people corrected it, and it then became toolkit code.
| To decide | Example (BENTEN) | How to check |
|---|---|---|
| ELN field → MaiML key, type, unit | Absorption edge → benten:absorption_edge (the ELN title) and matvoc:edge (the MatVoc URI) | maiml check (XSD) |
| Technique and material terms | XAFS → PaNET01196 in panet:technique; raw material → a MatVoc QID or your own ID | Look the IRI up (below) |
| What shares a uuid | One per raw-material item in the ELN; owner, vendor, instrument from role and id (stable_uuid) | Export twice and compare the uuids |
| Parent–child links | Raw-material hub → measured sample (instanceRef) → measurement (<parent> + SHA-256) | maiml check (parent-missing, parent-hash, material-differs) |
Where AI tends to go wrong
| Mistake | Example | How to prevent it |
|---|---|---|
| Inventing IRIs or QIDs | A plausible PaNET0xxxx or MatVoc Q… | Look them up, with labels, in the vocabulary copy (PaNET.csv, …) or its API |
| Calling a near term "the same" | Transmission as exactMatch to "x-ray absorption" | People decide the strength; when unsure use broadMatch or related |
| Random uuids | uuid4() gives a new uuid on every export (actually fixed in this toolkit) | Derive them from the ELN id with stable_uuid; export twice and compare |
| Wrong units or types | keV vs eV, numbers as strings | Check with the XSD and the schema's units and types |
| Treating spelling variants as different things | Two spellings of one supplier as two suppliers | Build the spelling-variant dictionary first and give it to the AI |
| Building from scratch | Defining every technique term yourself | Say "base it on the existing vocabulary and add only what is missing" up front |
How to check
| Check | How |
|---|---|
| The IRI or QID exists and the label matches | PaNET: PaNET.csv / panet.json (copies); MatVoc: https://matvoc.wikibase.cloud/w/api.php (wbgetentities, wbsearchentities) |
| MaiML structure and links | python -m elabftw_toolkit.maiml check <folder>; the VS Code XML extension with the MaiML XSD (chapter 10) |
| A diagram of links across places | python -m elabftw_toolkit.maiml graph <folders…> (Mermaid); the MCP tool maiml_graph (chapter 10) |
| Metadata matches the schema | python -m elabftw_toolkit.benten_schema check <folder>; the VS Code YAML extension with the JSON Schema from export-jsonschema |
| ELN writes do what you intend | The dry-run Plan (chapter 15) |
If you ask the AI to "run the check commands and fix until there are 0 errors and warnings", the machine checks can be left to the AI. People then look at the remaining warnings and at meaning.
Revising the metadata schema with AI
Start from the BENTEN schema Excel (BENTEN metadata and schema) and have the AI adapt it to your instrument.
- Give the AI the Excel and your instrument's fields (your current ELN template or your measurement software's output) and ask which columns to add, remove or rename
- Correct the proposal and apply it to the Excel, then run
python -m elabftw_toolkit.benten_schema lint my_schema.xlsxto catch typos in types and options and examples that do not fit - Convert it with
python -m elabftw_toolkit.benten_schema export-jsonschema my_schema.xlsx -o my_metadata.schema.jsonand check your metadata withcheck - Decide whether each mismatch is a schema error or a data error
Letting AI help with ELN entries
With the MCP server (chapter 16), Claude Desktop can edit the ELN.
- It only reads by default. Add
ELABFTW_ALLOW_WRITE=1only when you want it to write (put files to attach inELABFTW_UPLOAD_DIR) - When writing, have the AI preview with
dry_run=trueand show old value → new value before it writes - Have it pass
reason, so why the entry changed is kept as a comment (the person isELABFTW_MCP_USER) - People decide the publication tags (whether something may go outside). Do not let the AI set them
Example requests
Ready-to-use prompts. Replace < > with your own.
Mapping a vocabulary
Map the measurement techniques and detection modes used at <instrument> to PaNET terms.
- Do not invent new terms for what PaNET lacks; give the nearest PaNET term and the SKOS mapping (exactMatch / closeMatch / broadMatch / related).
- Use only PaNET IDs that appear in the attached PaNET.csv, and show their labels.
- For anything other than exactMatch, give a one-line reason.Finding MatVoc candidates
For the attached list of raw materials (name, formula, supplier), find MatVoc candidates.
- Actually search the MatVoc API (wbsearchentities) and list only QIDs and labels you found. Do not guess QIDs.
- Mark formula matches as "high" and name-only matches as "medium".
- Mark materials not found as "not in MatVoc" and collect what a proposal needs (English name, formula, CAS).Drafting the MaiML mapping
Turn the fields of the ELN template <template name> into a table of MaiML key, type and unit.
- Do not use random uuids; derive them from the ELN id (elabftw_toolkit.maiml.stable_uuid).
- After exporting, run python -m elabftw_toolkit.maiml check <folder> and fix until there are 0 errors and warnings.Editing the ELN
In items of category <category>, I want purity written as a number such as "99.9".
First show me, with dry_run=true, the entries that would change and old value → new value. After I agree, write them with reason="normalise purity notation".