ofa_main¶
OnField Assistant (ofa) — locally hosted, RAG-augmented LLM for HPC and scientific-computing workflows.
Functions
|
Non-streaming chat call — returns full response string (for planning). |
|
Stream a chat response from Ollama using per-model sampling options. |
|
Parse the assistant's response for tool-use fences and dispatch each tool's handler. |
Start Ollama server if not already running. |
|
|
Persist a `=== LESSON === . |
|
Persist a `=== PREFS === . |
|
|
|
Extract URLs from query, fetch their content, and return as context. |
|
Generate a single OpenFOAM file, using already-generated files as context. |
Return the sampling options for the currently-selected MODEL. |
|
Native chat-template thinking tags for the currently-selected MODEL, used by make_thought_filter() to also hide upstream reasoning streams on top of our own <thought>...</thought> convention. |
|
|
|
|
|
|
Run interactive chat loop. |
|
|
|
|
|
Build a streaming filter that hides 'thinking' regions from display. |
|
Compress session history to stay within |
|
True if the given model id (default: active MODEL) can accept images. |
|
Ask the LLM to plan which files the case needs, in generation order. |
|
AMReX-only retrieval. |
|
Retrieve relevant OpenFOAM file chunks from the vector database using Hybrid Search. |
|
|
|
MARBLES-focused retrieval. |
|
Retrieve from the user's own indexed data, if any. |
|
Quantum-computing focused retrieval. |
|
VASP-focused retrieval. |
|
Parse response and save files to a case directory. |
|
|
|
- ofa_main.model_supports_vision(model=None)[source]¶
True if the given model id (default: active MODEL) can accept images.
- ofa_main.get_model_options()[source]¶
Return the sampling options for the currently-selected MODEL.
Per-env-var overrides ($OFA_TEMPERATURE etc.) always win over the registry, so a one-off tweak doesn’t require editing the registry.
- ofa_main.get_model_thought_tags()[source]¶
Native chat-template thinking tags for the currently-selected MODEL, used by make_thought_filter() to also hide upstream reasoning streams on top of our own <thought>…</thought> convention.
- ofa_main.manage_session_context(messages, max_chars=100000)[source]¶
Compress session history to stay within
max_charsbudget.Strategy (oldest → newest, skipping system prompt and the last 2 messages so the model retains the immediate user→assistant→tool context):
Drop
<thought>...</thought>deliberation from old assistant turns.Replace old
"Output from executed commands:"user messages with a short placeholder.Strip the contents of fenced code blocks (
` ... `) from any other old user message aboveLARGE_USER_MSG_CHARS— this catches large@filepastes and command outputs the model produced from sources other than the tool-execution helper.
We only emit the “Compressing…” banner when we actually freed bytes, and we now report before→after sizes so the user can see the effect. If we still cannot reach the target after exhausting eligible messages, we hint at
/clearinstead of repeatedly alarming the user on every subsequent turn.
- ofa_main.make_thought_filter(extra_tag_pairs=None)[source]¶
Build a streaming filter that hides ‘thinking’ regions from display.
Always hides our own <thought>…</thought> convention. Additionally hides any (open_tag, close_tag) pairs in extra_tag_pairs, which the caller typically gets from get_model_thought_tags() — used for models whose chat template emits a native reasoning channel that should not be shown to the end user.
The captured response string fed back to the caller stays intact, so the model retains continuity with its own scratchpad on the next turn.
- ofa_main.extract_and_save_prefs(response_text)[source]¶
Persist a === PREFS === … === END PREFS === block from the model.
- Parameters:
response_text (str)
- ofa_main.extract_and_save_lessons(response_text)[source]¶
Persist a === LESSON === … === END LESSON === block from the model.
Triggered autonomously when the model recognizes a failure mode, an environment quirk, or a user correction. The ‘Lessons Channel’ system prompt encourages this without requiring an explicit user directive. Capped tighter than prefs (2/turn vs 4) to prevent the model from spamming the lessons file with every observation.
- Parameters:
response_text (str)
- ofa_main.ensure_ollama_running()[source]¶
Start Ollama server if not already running.
The chosen TCP port is persisted to <OFA_SCRATCH>/.ofa_ollama.port and the daemon’s PID to <OFA_SCRATCH>/.ofa_ollama.pid. Concurrent ofa invocations by the same user reuse the same daemon when possible.
- ofa_main.fetch_url_context(query, max_chars=64000)[source]¶
Extract URLs from query, fetch their content, and return as context.
- ofa_main.retrieve_context(query, top_k=10)[source]¶
Retrieve relevant OpenFOAM file chunks from the vector database using Hybrid Search.
- ofa_main.chat_stream(messages, **option_overrides)[source]¶
Stream a chat response from Ollama using per-model sampling options.
- Parameters:
messages (list)
- ofa_main.chat_complete(messages)[source]¶
Non-streaming chat call — returns full response string (for planning).
- ofa_main.plan_file_list(query, rag_context, system_prompt)[source]¶
Ask the LLM to plan which files the case needs, in generation order.
Returns a list of file paths (e.g. [“system/controlDict”, “0/U”, …]) or None if parsing fails (caller should fall back to single-shot).
- ofa_main.generate_file(filepath, query, rag_context, system_prompt, generated)[source]¶
Generate a single OpenFOAM file, using already-generated files as context.
Streams output to stdout and returns the full file content.
- ofa_main.save_case(response_text, output_dir)[source]¶
Parse response and save files to a case directory.
- ofa_main.interactive_mode(save_dir=None, resume=False, hpc_mode=False, code_mode=False, amrex_mode=False, marbles_mode=False, reframe_mode=False, quantum_computing_mode=False, vasp_mode=False)[source]¶
Run interactive chat loop.
- ofa_main.retrieve_amrex_context(query, top_k=5)[source]¶
AMReX-only retrieval. Grabs top-k results from
amrex_srcplus a small slice ofhpc_docs(module paths / SLURM) so the model has Kestrel context for a real build/run. Does NOT touchmarbles_src— useretrieve_marbles_context()for that mode.
- ofa_main.retrieve_marbles_context(query, top_k=5)[source]¶
MARBLES-focused retrieval.
Queries
marbles_srcprimarily (top-k), with a smalleramrex_srcslice (top-2) because MARBLES is a lattice-Boltzmann solver built on top of AMReX — its users legitimately need to trace intoMultiFab/ParallelFor/BoxArraydetails for anything non-trivial. Also grabs light HPC docs for module paths / SLURM.Split introduced when the old combined –amrex mode was factored into pure –amrex and –marbles modes; keeping the AMReX slice in the marbles path preserves the useful behaviour of the old mode for LBM developers.
- ofa_main.retrieve_quantum_computing_context(query, top_k=7)[source]¶
Quantum-computing focused retrieval.
Queries the single
quantum_computingcollection, which mixes code (Qiskit / Cirq / PennyLane / cuQuantum snippets) with PDFs (papers, thesis excerpts, textbook chapters). Metadatasource_typedistinguishes them; retrieval headers surface that plus (for PDFs) the page number so the model can cite properly.Also grabs a light HPC docs slice so module load / Slurm advice is grounded when users ask about running quantum simulators on Kestrel.
- ofa_main.retrieve_private_context(query, top_k=5)[source]¶
Retrieve from the user’s own indexed data, if any.
Returns “” when the user has no private store, which is the common case — callers can append the result unconditionally. Searches every private collection and interleaves the results, since a user’s corpora are usually small and topically distinct enough that ranking across them is not worth the complexity.
- ofa_main.retrieve_vasp_context(query, top_k=5)[source]¶
VASP-focused retrieval.
Queries the
vasp_srccollection (tutorials + general notes + Wisconsin docs snapshot), pinsApplications/vasp.mdfrom the HPC docs as the authoritative build / module / Slurm / licensing reference, and adds a smallhpc_docsslice so tooling advice stays grounded when users ask about running VASP on Kestrel.
- ofa_main.check_and_execute_bash(response_text)[source]¶
Parse the assistant’s response for tool-use fences and dispatch each tool’s handler. Returns the concatenated tool output (for feeding back into the LLM as a new user message), or None if nothing was executed.