# yahyamomtaz/biolab-agent-planner architecture

> Architecture diagram of the GitHub repository yahyamomtaz/biolab-agent-planner, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yahyamomtaz/biolab-agent-planner
- Repository: https://github.com/yahyamomtaz/biolab-agent-planner

## Overview

1. **Overall shape**
   - This repo is a starter stack for an autonomous lab agent, not a generic app.
   - The core architectural idea is: natural-language request in, tool-using agent in the middle, structured lab protocol / analysis out.
   - Around that core are three delivery surfaces: a FastAPI service, a Gradio UI, and an evaluation harness.

2. **Primary runtime boundary**
   - The main application code lives under `src/biolab_agent/`.
   - `BaseAgent` defines the contract; `BaselineAgent` is the reference implementation.
   - The active agent is selected via `BIOLAB_AGENT_CLASS`, so the rest of the stack can stay fixed while swapping agent implementations.

3. **Request-to-result flow**
   - Requests enter through `/ask`, the CLI benchmark runner, or the UI.
   - The agent runs a multi-turn loop, calling tools as needed and then assembling an `AgentResult`.
   - That result contains the human-facing answer, structured payload, citations, and a tool trace, which is what downstream surfaces render or score.

4. **LLM orchestration layer**
   - The baseline agent uses Ollama-hosted MedGemma-4B for tool selection and response synthesis.
   - The agent forces a JSON-only interaction pattern: either a tool call or a final answer.
   - This makes the model the driver of branching and sequencing, while Python only dispatches tools and records results.

5. **Tool subsystem**
   - Tool implementations live under `src/biolab_agent/tools/`.
   - The four important tools map directly to the benchmark tasks:
     - segmentation of microscopy wells,
     - protocol retrieval,
     - reagent lookup,
     - structured protocol composition.
   - This is the main functional boundary in the repo: the agent decides what to do; the tools perform deterministic work.

6. **Segmentation pipeline**
   - `src/biolab_agent/segmentation/` handles image-based analysis using SAM-backed mask generation.
   - It consumes image IDs from `data/images/` and returns cell counts, masks, and confluency estimates.
   - These outputs are treated as trustworthy structured data and are aggregated back into the final response rather than being left for the LLM to restate from memory.

7. **Retrieval / RAG subsystem**
   - `src/biolab_agent/rag/` plus the `retrieve_protocol` tool provide retrieval over the protocol corpus.
   - The corpus is built from `data/protocols/opentrons.jsonl`, indexed into Qdrant, and queried with embeddings from a sentence-transformer model.
   - This is the main external-data integration path for protocol guidance, and it returns source identifiers for citation and scoring.

8. **Reagent catalog lookup**
   - `data/reagents/catalog.csv` is a lightweight local knowledge base extracted from protocols.
   - The reagent tool is intentionally simple: a local substring/catalog search rather than a model-mediated lookup.
   - This keeps reagent references deterministic and easy to score against benchmark expectations.

9. **Protocol composition and validation**
   - Structured protocol creation is handled as a typed/validated output boundary, not free-form text.
   - `compose_protocol(...)` and the schemas in `src/biolab_agent/schemas.py` enforce the shape of the final protocol object.
   - This boundary matters because the repository distinguishes “LLM-generated prose” from “validated structured lab plan.”

10. **Adapter polish path**
   - For protocol-design tasks, the agent can optionally unload the Ollama model and run one refinement pass using a LoRA adapter from `artifacts/lora-protocol-text/`.
   - That adapter is loaded through Hugging Face / transformers, separate from the main Ollama inference path.
   - This creates a distinct “base reasoning” vs. “final polishing” stage in the architecture.

11. **Serving and interfaces**
   - `src/biolab_agent/server.py` exposes the service API, while `ui/app.py` provides a Gradio front end.
   - Both are thin shells around the same agent contract and `AgentResult` schema.
   - The repo also includes readiness and liveness endpoints, which reflect dependency health for Ollama and Qdrant.

12. **Evaluation and benchmarking**
   - `eval/harness.py` and `eval/metrics.py` define the benchmark loop and scoring rules.
   - The benchmark uses the public task set in `data/queries_public.yaml` and compares against ground truth from the microscopy counts, protocol IDs, and reagent catalog.
   - This evaluation layer is a first-class subsystem, not an afterthought, and it shares the same agent entry point as the app.

13. **Data and artifact boundaries**
   - The repo bundles three main data domains: microscopy images, protocol texts, and reagent catalog entries.
   - The `data/` tree is the source of truth for retrieval, segmentation evaluation, and reagent scoring.
   - `artifacts/` holds model artifacts such as the LoRA adapter, separating immutable training outputs from source code.

14. **Deployment topology**
   - The stack is containerized with Docker Compose.
   - Typical deployment includes the app container plus Ollama and Qdrant sidecars; there is also a CPU-only override and a host-Ollama override.
   - This means the architecture intentionally externalizes model serving and vector storage rather than embedding them in-process.

15. **Fine-tuning pipeline**
   - `src/biolab_agent/finetune/` and `data/finetune/` support LoRA training from protocol-derived instruction pairs.
   - The training path is separate from the runtime agent path but feeds the adapter-polarization step used at inference time.
   - In other words: corpus → training data → LoRA adapter → optional protocol-design refinement.

16. **Config and operational control**
   - Environment variables in `.env.example`, plus `configs/`, control model names, adapter paths, and service wiring.
   - Shell scripts under `scripts/` wrap setup, data fetching, model pulling, and benchmark execution.
   - These are operational glue, but they matter architecturally because they define how the repo’s external dependencies are provisioned and connected.

Main entry points: [`src/biolab_agent/agent/baseline.py`](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/agent/baseline.py) (Baseline agent), [`src/biolab_agent/agent/loader.py`](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/agent/loader.py) (Agent loader), [`eval/harness.py`](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/eval/harness.py) (Eval harness), [`src/biolab_agent/llm/base.py`](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/llm/base.py) (LLM interface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_cli["CLI<br/>entrypoint<br/>[cli.py]"]
  node_server["API server<br/>fastapi service<br/>[server.py]"]
  node_ui["Gradio UI<br/>frontend<br/>[app.py]"]
  node_loader["Agent loader<br/>runtime selection<br/>[loader.py]"]
  node_base_agent["Agent contract<br/>interface<br/>[base.py]"]
  node_baseline_agent["Baseline agent<br/>tool-using agent<br/>[baseline.py]"]
  node_llm_base["LLM interface<br/>client abstraction<br/>[base.py]"]
  node_ollama_client["Ollama client<br/>llm backend<br/>[ollama_client.py]"]
  node_hf_client["HF client<br/>adapter backend<br/>[hf_client.py]"]
  node_schemas["Schemas<br/>typed outputs<br/>[schemas.py]"]
end

subgraph group_tools["Tools"]
  node_segment_tool["Segment tool<br/>[segment.py]"]
  node_rag_tool["Retrieve tool<br/>[rag.py]"]
  node_reagent_tool["Reagent tool<br/>[reagents.py]"]
  node_compose_tool["Compose tool<br/>[compose.py]"]
  node_segmentation["Segmentation<br/>image analysis<br/>[sam_backend.py]"]
  node_rag_ingest["RAG ingest<br/>indexing pipeline<br/>[ingest.py]"]
end

subgraph group_data["Data"]
  node_protocols_data["Protocols<br/>corpus<br/>[opentrons.jsonl]"]
  node_images_data[("Images<br/>image set<br/>[ground_truth.json]")]
  node_reagents_data[("Reagents<br/>catalog<br/>[catalog.csv]")]
  node_finetune_data["Finetune data<br/>instruction set<br/>[train.jsonl]"]
end

subgraph group_eval["Evaluation"]
  node_eval_harness["Eval harness<br/>benchmark runner<br/>[harness.py]"]
  node_eval_metrics["Metrics<br/>scoring<br/>[metrics.py]"]
end

subgraph group_infra["Infrastructure"]
  node_train["Finetune<br/>lora training<br/>[train.py]"]
  node_config["Config<br/>[config.py]"]
  node_adapter["LoRA adapter<br/>model artifact"]
end

node_cli -->|"start"| node_loader
node_server -->|"start"| node_loader
node_ui -->|"start"| node_loader
node_loader -->|"instantiate"| node_base_agent
node_base_agent -->|"default"| node_baseline_agent
node_baseline_agent -->|"uses"| node_llm_base
node_llm_base -->|"runtime"| node_ollama_client
node_llm_base -->|"adapter"| node_hf_client
node_baseline_agent -->|"calls"| node_segment_tool
node_baseline_agent -->|"calls"| node_rag_tool
node_baseline_agent -->|"calls"| node_reagent_tool
node_baseline_agent -->|"calls"| node_compose_tool
node_segment_tool -->|"delegates"| node_segmentation
node_rag_tool -->|"queries"| node_rag_ingest
node_rag_ingest -->|"builds"| node_protocols_data
node_segmentation -->|"reads"| node_images_data
node_reagent_tool -->|"reads"| node_reagents_data
node_compose_tool -->|"validates"| node_schemas
node_baseline_agent -->|"returns"| node_schemas
node_eval_harness -->|"runs"| node_baseline_agent
node_eval_harness -->|"scores"| node_eval_metrics
node_eval_harness -->|"loads"| node_finetune_data
node_train -->|"trains on"| node_finetune_data
node_train -->|"produces"| node_adapter
node_hf_client -->|"loads"| node_adapter
node_config -->|"configures"| node_baseline_agent

click node_cli "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/cli.py"
click node_server "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/server.py"
click node_ui "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/ui/app.py"
click node_loader "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/agent/loader.py"
click node_base_agent "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/agent/base.py"
click node_baseline_agent "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/agent/baseline.py"
click node_llm_base "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/llm/base.py"
click node_ollama_client "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/llm/ollama_client.py"
click node_hf_client "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/llm/hf_client.py"
click node_schemas "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/schemas.py"
click node_segment_tool "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/tools/segment.py"
click node_rag_tool "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/tools/rag.py"
click node_reagent_tool "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/tools/reagents.py"
click node_compose_tool "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/tools/compose.py"
click node_segmentation "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/segmentation/sam_backend.py"
click node_rag_ingest "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/rag/ingest.py"
click node_eval_harness "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/eval/harness.py"
click node_eval_metrics "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/eval/metrics.py"
click node_train "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/finetune/train.py"
click node_config "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/config.py"
click node_protocols_data "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/data/protocols/opentrons.jsonl"
click node_images_data "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/data/images/ground_truth.json"
click node_reagents_data "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/data/reagents/catalog.csv"
click node_finetune_data "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/data/finetune/train.jsonl"
click node_adapter "https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/artifacts/lora-protocol-text/adapter_model.safetensors"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_server,node_ui,node_loader,node_base_agent,node_baseline_agent,node_llm_base,node_ollama_client,node_hf_client,node_schemas toneBlue
class node_segment_tool,node_rag_tool,node_reagent_tool,node_compose_tool,node_segmentation,node_rag_ingest toneAmber
class node_protocols_data,node_images_data,node_reagents_data,node_finetune_data toneMint
class node_eval_harness,node_eval_metrics toneRose
class node_train,node_config,node_adapter toneIndigo
```

## Components

### Runtime

Agent execution, LLM orchestration, schemas, and service entrypoints.

- [CLI](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/cli.py): entrypoint · Benchmark and local command-line entrypoint. · `src/biolab_agent/cli.py`
- [API server](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/server.py): fastapi service · HTTP service exposing the agent. · `src/biolab_agent/server.py`
- [Gradio UI](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/ui/app.py): frontend · Human-facing interface wrapping the same agent contract. · `ui/app.py`
- [Agent loader](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/agent/loader.py): runtime selection · Resolves the active agent implementation from configuration. · `src/biolab_agent/agent/loader.py`
- [Agent contract](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/agent/base.py): interface · Shared behavior for all agent implementations. · `src/biolab_agent/agent/base.py`
- [Baseline agent](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/agent/baseline.py): tool-using agent · Reference multi-turn agent that orchestrates tools and final responses. · `src/biolab_agent/agent/baseline.py`
- [LLM interface](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/llm/base.py): client abstraction · Shared model-client boundary for Ollama and Hugging Face backends. · `src/biolab_agent/llm/base.py`
- [Ollama client](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/llm/ollama_client.py): llm backend · Primary inference path for the baseline agent. · `src/biolab_agent/llm/ollama_client.py`
- [HF client](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/llm/hf_client.py): adapter backend · Loads the LoRA adapter for optional protocol refinement. · `src/biolab_agent/llm/hf_client.py`
- [Schemas](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/schemas.py): typed outputs · Validated request and result objects, including structured protocols. · `src/biolab_agent/schemas.py`

### Tools

Deterministic capabilities used by the agent.

- [Segment tool](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/tools/segment.py): tool · Invokes image segmentation and returns counts and confluency. · `src/biolab_agent/tools/segment.py`
- [Retrieve tool](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/tools/rag.py): tool · Queries protocol retrieval for citations and guidance. · `src/biolab_agent/tools/rag.py`
- [Reagent tool](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/tools/reagents.py): tool · Performs local reagent catalog lookup. · `src/biolab_agent/tools/reagents.py`
- [Compose tool](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/tools/compose.py): tool · Builds structured protocol outputs. · `src/biolab_agent/tools/compose.py`
- [Segmentation](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/segmentation/sam_backend.py): image analysis · SAM-backed mask generation and measurement pipeline. · `src/biolab_agent/segmentation/sam_backend.py`
- [RAG ingest](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/rag/ingest.py): indexing pipeline · Builds and queries the protocol retrieval corpus. · `src/biolab_agent/rag/ingest.py`

### Data

Protocol, image, reagent, and training corpora.

- [Protocols](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/data/protocols/opentrons.jsonl): corpus · Source protocol corpus used for retrieval and training. · `data/protocols/opentrons.jsonl`
- [Images](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/data/images/ground_truth.json): image set · Microscopy inputs and ground truth for segmentation. · `data/images/ground_truth.json`
- [Reagents](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/data/reagents/catalog.csv): catalog · Local reagent reference table. · `data/reagents/catalog.csv`
- [Finetune data](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/data/finetune/train.jsonl): instruction set · Training and evaluation pairs for adapter learning. · `data/finetune/train.jsonl`

### Evaluation

Benchmarking and scoring against public tasks.

- [Eval harness](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/eval/harness.py): benchmark runner · Drives public tasks through the same agent entrypoint. · `eval/harness.py`
- [Metrics](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/eval/metrics.py): scoring · Compares outputs against task-specific ground truth. · `eval/metrics.py`

### Infrastructure

Fine-tuning, adapters, and deployment wiring.

- [Finetune](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/finetune/train.py): lora training · Creates protocol-oriented LoRA adapters. · `src/biolab_agent/finetune/train.py`
- [Config](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/src/biolab_agent/config.py): runtime config · Environment and deployment wiring for models and services. · `src/biolab_agent/config.py`
- [LoRA adapter](https://github.com/yahyamomtaz/biolab-agent-planner/blob/main/artifacts/lora-protocol-text/adapter_model.safetensors): model artifact · Protocol-polishing adapter used during optional refinement. · `artifacts/lora-protocol-text/adapter_model.safetensors`

## Connections

- CLI → Agent loader: start · CLI resolves the active agent before running benchmarks or local requests.
- API server → Agent loader: start · API server loads the configured agent implementation.
- Gradio UI → Agent loader: start · Gradio UI uses the same agent loader as the service.
- Agent loader → Agent contract: instantiate · Loader binds the chosen implementation to the common contract.
- Agent contract → Baseline agent: default · Baseline agent is the reference implementation of the contract.
- Baseline agent → LLM interface: uses · Agent depends on the shared model-client abstraction.
- LLM interface → Ollama client: runtime · Primary runtime model backend.
- LLM interface → HF client: adapter · Optional Hugging Face path for adapter-based refinement.
- Baseline agent → Segment tool: calls · Agent invokes segmentation when image analysis is needed.
- Baseline agent → Retrieve tool: calls · Agent retrieves protocol guidance and citations.
- Baseline agent → Reagent tool: calls · Agent looks up reagents through a deterministic catalog search.
- Baseline agent → Compose tool: calls · Agent assembles structured protocol outputs through the compose boundary.
- Segment tool → Segmentation: delegates · Tool delegates image processing to the SAM-backed pipeline.
- Retrieve tool → RAG ingest: queries · Retrieval tool queries the indexed protocol corpus.
- RAG ingest → Protocols: builds · Protocol corpus is the source for indexing and retrieval.
- Segmentation → Images: reads · Segmentation consumes microscopy inputs and ground truth assets.
- Reagent tool → Reagents: reads · Reagent lookup is backed by the local catalog.
- Compose tool → Schemas: validates · Structured protocol composition is enforced by schemas.
- Baseline agent → Schemas: returns · Agent emits validated AgentResult objects and structured payloads.
- Eval harness → Baseline agent: runs · Benchmark harness drives the same agent entrypoint as the app.
- Eval harness → Metrics: scores · Harness uses scoring rules to evaluate outputs.
- Eval harness → Finetune data: loads · Public benchmark tasks provide the evaluation set and training sources.
- Finetune → Finetune data: trains on · Fine-tuning consumes instruction pairs derived from protocol data.
- Finetune → LoRA adapter: produces · Training outputs the optional LoRA adapter artifact.
- HF client → LoRA adapter: loads · Adapter backend loads the saved LoRA weights for refinement.
- Config → Baseline agent: configures · Environment config controls agent class selection and model wiring.
