Architecture¶
High-level layout, sourced from the repo root’s ARCHITECTURE.md. For a
deeper walk through the runtime (RAG pipeline, memory system, tool-call
protocol, --serve internals), see the technical overview.
OnField Assistant 🌵 (ofa) Architecture & Hybrid Search¶
Overview¶
The OnField Assistant (ofa) is an AI-powered CLI wrapper designed to help HPC users navigate complex CFD case setups, explore OpenFOAM C++ source code, and retrieve HPC hardware documentation directly from the console. It leverages locally hosted Large Language Models (via Ollama) augmented with highly specialized domain knowledge through Retrieval-Augmented Generation (RAG).
Core Components¶
LLM Backend: Ollama running locally on the user’s GPU allocation (default model
gemma4:31b-it-q8_0at its full 262144-token context; seeMODEL_REGISTRYinsrc/ofa_main.py). Optional alternatives: an OpenAI-compatible LiteLLM gateway (--litellm), a larger model thatofa --modelstarts automatically on its own GPU node(s) with vLLM or llama.cpp (src/ofa_vllm.py), or a hand-started vLLM server (--vllm). Retrieval and prompts stay local in every case.Vector Database: ChromaDB (Persistent).
Embeddings:
sentence-transformersfor dense vector semantic representations.Lexical Search:
rank-bm25(Okapi BM25) for exact keyword and token matching.
Retrieval-Augmented Generation (RAG) Collections¶
There are eight ChromaDB collections (see the technical overview for the full list); each mode queries the ones relevant to it. The OpenFOAM and HPC paths use:
openfoam: OpenFOAM tutorials and case dictionaries to accurately generate and format case files (system/,constant/,0/).hpc_docs: Hardware, SLURM, and exact HPC software guides (e.g., Kestrel vs Gila configurations).of13_src: OpenFOAM C++ source code (accessible via keywords likecpp,source code,pEqn.H, etc.).
Hybrid Search & Reciprocal Rank Fusion (RRF)¶
Initially, the RAG pipeline relied purely on dense vector similarity (Semantic Search). This posed a problem for highly specific, domain-unique keywords (e.g., “M-Star”, “STAR-CCM+”, “pEqn.H”). Semantic search models tend to dilute specialized terms into generic concepts, returning irrelevant tutorials and promoting LLM hallucinations.
To resolve this, the architecture employs a True Hybrid Search mechanism:
Vector Semantic Search: Encodes the user’s query and retrieves the top
Kmost conceptually similar documents. This is excellent for conceptual intent like “tips for optimizing simulations.”BM25 Lexical Search: Tokenizes the query—filtering out common English stopwords (
how,to,what,is,here)—and performs an exact-match term frequency-inverse document frequency search. This is crucial for retrieving specific software acronyms or exact C++ file structures when the semantic model fails to prioritize them.Reciprocal Rank Fusion: RRF mathematically merges the ranked result lists from both the Vector and BM25 search queries into a single unified context block:
# Simplification of the fusion logic rrf_score = 1.0 / (rank + 60)
The merged list ensures that documents appearing high in either exact-keyword matches or semantic representation take absolute priority in the LLM’s context window.
Query construction: the retrieval query is the user’s latest request followed by up to two earlier requests (build_rag_query()), so follow-ups like “now run it on a GPU” keep the earlier topic. Context that VS Code adds around the request is stripped first (request_text()). In HPC retrieval, chunks from the RHEL9 upgrade docs are placed first and labelled CURRENT so they outrank older guidance.
Primary Execution Paths¶
General coding (
ofa, the default): an agent loop that reads and edits files, runs commands, and submits SLURM jobs, with Kestrel HPC docs retrieved as context. Other modes (--amrex,--marbles,--vasp,--quantum-computing,--rhel9_reframe) swap in their own prompt and collections.Case Generation (
ofa --openfoam <query>): Processes requested configurations, queries theopenfoamDB, and predictably maps output to required file hierarchies. Can be run in step-by-step memory tracking mode, or single-shot (--fast) for rapid setup. Saved directly to target directories (--save).HPC Documentation (
ofa --hpc <query>): Bypasses the CFD configuration pipeline to strictly queryhpc_docs. Used for generating accuratesallocandsbatchsetups tailored for Kestrel/Gila software modules (like M-Star, PyTorch, Conda).C++ Exploration: RAG automatically intercepts C++ intent (checking for
.C,.H,cpp, orimplementation) and executes a hybrid query against theof13_srcDB collection to explain and retrieve actual OpenFOAM class architectures.