Skip to content

Commit 1c04afa

Browse files
Add code-integration guide for AI agents
Extend docs/AGENTS.md with code-integration patterns for the Experiment API (RAG/eval, SFT, DPO, GRPO), the AutoML config-group reference, and the experiment lifecycle, so AI coding agents can wire rapidfireai into a user's own project, not just install it. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 08bea39 commit 1c04afa

1 file changed

Lines changed: 123 additions & 8 deletions

File tree

docs/AGENTS.md

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# RapidFire AI — Agent Install & Setup Guide
1+
# RapidFire AI — Agent Integration Guide
22

3-
Operational instructions for **AI coding agents** (Claude Code, Cursor, Codex, GitHub Copilot, Windsurf, Aider, Junie, and similar) that are helping an end user install, configure, and run the `rapidfireai` Python package.
3+
Operational instructions for **AI coding agents** (Claude Code, Cursor, Codex, GitHub Copilot, Windsurf, Aider, Junie, and similar) that are helping an end user install, configure, and integrate the `rapidfireai` Python package into the user's own project.
44

55
This file is **not** for rapidfireai contributors. If you are working *on* rapidfireai itself, stop and read the repo's root [`AGENTS.md`](https://github.com/RapidFireAI/rapidfireai/blob/main/AGENTS.md) and [`CONTRIBUTING.md`](https://github.com/RapidFireAI/rapidfireai/blob/main/CONTRIBUTING.md) instead.
66

@@ -13,6 +13,7 @@ This guide does **not** restate version-specific install commands, package versi
1313
What this guide *does* provide that the README does not:
1414

1515
- A workflow decision tree (RAG vs fine-tuning vs post-training; OpenAI vs self-hosted; lite vs full).
16+
- Code integration patterns for the `Experiment` API and the AutoML config groups.
1617
- Trainer-type taxonomy for fine-tuning (`SFT` / `DPO` / `GRPO`).
1718
- Safety rules for handling user secrets, GPU assumptions, and gated model access.
1819

@@ -39,7 +40,7 @@ Pick a branch **before** running any install commands — the two workflows inst
3940
### Environment selectors
4041

4142
- **Remote / cloud machine** → an SSH port-forward is required to view the dashboard locally. The set of ports differs by workflow (smaller for fit, larger for evals because Jupyter and MLflow are also exposed). The current port set and the exact `ssh` command are in the README §Install — read them from there, do not memorize.
42-
- **GPU issues** (CUDA absent, OOM, driver mismatch) → run `rapidfireai doctor` and act on its output. For OOM, switch the user to a *lite* tutorial variant (see §6).
43+
- **GPU issues** (CUDA absent, OOM, driver mismatch) → run `rapidfireai doctor` and act on its output. For OOM, switch the user to a *lite* tutorial variant (see §9).
4344
- **Hugging Face permission issues** → confirm the user has run the README's HF auth step and has been granted access on the gated model's HF page. If access is blocked, suggest an open-license substitute (TinyLlama, Qwen-0.5B/3B) where licensing permits.
4445

4546
The exact CLI flag name for the evals init mode and the exact set of port numbers are intentionally not restated here — see the README, which is the authoritative install reference.
@@ -60,7 +61,7 @@ Run the steps in the order below. The **commands** are in the README; the **deci
6061
8. **Run `rapidfireai doctor`** and confirm no critical failures (Python, GPU/CUDA where applicable, ports, packages). Do not proceed past failures without diagnosing the root cause.
6162
9. **Start the dashboard stack** with `rapidfireai start` (frontend, MLflow, dispatcher). For the RAG/evals workflow, also start a Jupyter listener as documented in the README — the evals tutorials run inside Jupyter rather than an arbitrary IDE notebook.
6263
10. **(Remote/cloud machines only) Port-forward** the workflow-appropriate set with `ssh` *before* clicking the dashboard URL. The current set and SSH syntax are in README §Install. VS Code typically auto-forwards the dashboard port; other clients require the explicit `ssh -L` invocation. Skip this step on a local machine.
63-
11. **Open the tutorial notebook** under `./tutorial_notebooks/` (see §6 for the filename matching the workflow). Two non-obvious requirements:
64+
11. **Open the tutorial notebook** under `./tutorial_notebooks/` (see §9 for the filename matching the workflow). Two non-obvious requirements:
6465
- Notebooks **cannot** run via `python notebook.ipynb` — there is a multiprocessing restriction. Open the notebook in an IDE that supports Jupyter (VS Code, Cursor, JetBrains), or use `rapidfireai jupyter` (mandatory for the RAG/evals tutorials).
6566
- The notebook's kernel **must** be set to the `.venv` you created in step 2, not the system Python; otherwise `import rapidfireai` fails. In VS Code / Cursor, click the kernel selector in the upper-right of the notebook and pick the venv interpreter.
6667
- The dashboard URL printed by `rapidfireai start` (or by `rapidfireai jupyter` for evals) shows live run progress as cells execute.
@@ -81,7 +82,119 @@ Run the steps in the order below. The **commands** are in the README; the **deci
8182

8283
---
8384

84-
## 5. Troubleshooting
85+
## 5. Code integration patterns
86+
87+
The four patterns below cover every supported workflow. Each is the *minimum* a user needs.
88+
89+
### A. RAG / evaluation
90+
91+
```python
92+
from rapidfireai import Experiment
93+
from rapidfireai.automl import RFGridSearch, List, RFvLLMModelConfig
94+
95+
experiment = Experiment("my-rag-eval", mode="evals")
96+
97+
config_group = RFGridSearch({
98+
"vllm_config": List([vllm_config_a, vllm_config_b]),
99+
"preprocess_fn": user_preprocess,
100+
"postprocess_fn": user_postprocess,
101+
"compute_metrics_fn": user_compute_metrics,
102+
# Optional:
103+
# "accumulate_metrics_fn": user_accumulate_metrics,
104+
})
105+
106+
results = experiment.run_evals(config_group, dataset, num_shards=4)
107+
experiment.end()
108+
```
109+
110+
For an **OpenAI / Azure / Anthropic** generator, swap `RFvLLMModelConfig` for `RFAPIModelConfig`. The user-supplied callbacks are:
111+
112+
- `preprocess_fn(batch, rag, prompt_manager) -> dict` — assemble prompts (with retrieved context for RAG).
113+
- `postprocess_fn(batch) -> dict` — attach ground-truth / reference labels.
114+
- `compute_metrics_fn(batch) -> dict[str, dict]` — per-batch metrics (Precision, Recall, NDCG@k, MRR, …).
115+
- `accumulate_metrics_fn(aggregated) -> dict[str, dict]` — optional, aggregates per-batch metrics across shards.
116+
117+
`run_evals` returns `dict[run_id, (aggregated_results, cumulative_metrics)]`.
118+
119+
### B. SFT (supervised fine-tuning)
120+
121+
```python
122+
from rapidfireai import Experiment
123+
from rapidfireai.automl import RFGridSearch
124+
125+
experiment = Experiment("my-sft", mode="fit")
126+
127+
config_group = RFGridSearch(configs=config_set, trainer_type="SFT")
128+
129+
experiment.run_fit(
130+
param_config=config_group,
131+
create_model_fn=user_create_model,
132+
train_dataset=train_ds,
133+
eval_dataset=eval_ds,
134+
num_chunks=4,
135+
)
136+
137+
results_df = experiment.get_results()
138+
experiment.end()
139+
```
140+
141+
User-supplied callbacks:
142+
143+
- `create_model_fn(model_config) -> (model, tokenizer)` — required. Loads model and tokenizer from the per-run config dict.
144+
- `formatting_func(row) -> dict` — optional. Returned in `RFModelConfig`. Maps a dataset row to `{"prompt": ..., "completion": ...}`.
145+
- `compute_metrics(preds, labels) -> dict[str, float]` — optional. Returned in `RFModelConfig`.
146+
147+
### C. DPO (preference alignment)
148+
149+
Same shape as SFT but `trainer_type="DPO"`. The training dataset must have `chosen` and `rejected` columns (or whatever the chosen DPO config maps to). A reference model is handled internally; the user does not have to construct one explicitly.
150+
151+
### D. GRPO (RL-based post-training)
152+
153+
Same shape as SFT but `trainer_type="GRPO"`. Reward functions are passed via the trainer config, not as a separate argument. Use a small model and a small dataset for first runs — GRPO samples multiple generations per prompt and is significantly slower than SFT/DPO.
154+
155+
---
156+
157+
## 6. Config groups and search
158+
159+
Imports:
160+
161+
```python
162+
from rapidfireai.automl import RFGridSearch, RFRandomSearch, List, Range
163+
```
164+
165+
- **Fit mode**: `RFGridSearch(configs=config_set, trainer_type="SFT" | "DPO" | "GRPO")` — exhaustive sweep. `trainer_type` selects the underlying TRL trainer.
166+
- **Evals mode**: `RFGridSearch({"vllm_config": List([...]), "preprocess_fn": ..., ...})` — dict form. **No `trainer_type`** (evals does inference, not training).
167+
- `RFRandomSearch(configs, num_runs, trainer_type=...)` — sampled variant. `trainer_type` is again fit-only.
168+
- `List([a, b, c])` and `Range(min=..., max=...)` define sweep dimensions and work in both modes.
169+
170+
Fit-mode config classes (from `rapidfireai.automl`): `RFModelConfig`, `RFLoraConfig`, `RFSFTConfig`, `RFDPOConfig`, `RFGRPOConfig`. Evals-mode config classes: `RFvLLMModelConfig`, `RFAPIModelConfig`, plus helpers `RFLangChainRagSpec` and `RFPromptManager`.
171+
172+
---
173+
174+
## 7. Experiment lifecycle
175+
176+
`rapidfireai start` (the CLI command) and `Experiment(...)` (the Python constructor) are distinct concerns:
177+
178+
- `rapidfireai start` brings up the **visual stack** (frontend dashboard, MLflow tracking, dispatcher API) so the user can watch runs live.
179+
- `Experiment(...)` separately starts an in-process dispatcher thread for the API calls it needs internally.
180+
181+
Users typically run `rapidfireai start` first (to get the dashboard), then run their notebook or script which constructs `Experiment(...)`. The CLI `start` is **not strictly required** for `Experiment(...)` to function — but without it the dashboard UI is unavailable.
182+
183+
API surface (from `rapidfireai.experiment`):
184+
185+
- `Experiment(name, mode="fit" | "evals")``mode` defaults to `"fit"`. `name` is required.
186+
- `run_fit(...)` — fit mode only. Blocking on non-Colab; async on Colab.
187+
- `run_evals(...)` — evals mode only. Returns `dict[run_id, (results, metrics)]`.
188+
- `get_results()` — fit mode only. Returns a pandas `DataFrame` of metrics.
189+
- `get_runs_info()` — fit mode only. Returns a pandas `DataFrame` of run metadata.
190+
- `cancel_current()` — both modes. Cancels an in-flight task.
191+
- `end()` — both modes. **Required** for clean shutdown of worker processes / Ray.
192+
193+
Only `Experiment` is exported at the top level. AutoML primitives are imported from `rapidfireai.automl`.
194+
195+
---
196+
197+
## 8. Troubleshooting
85198

86199
Match the symptom to the diagnostic direction. Specific commands (kill-by-port, SSH-forward, hf-xet uninstall, etc.) are in README §Troubleshooting and §Install — do not paste them from memory.
87200

@@ -100,7 +213,7 @@ Match the symptom to the diagnostic direction. Specific commands (kill-by-port,
100213

101214
---
102215

103-
## 6. Tutorials
216+
## 9. Tutorials
104217

105218
After `rapidfireai init`, tutorial notebooks are copied to `./tutorial_notebooks/`. Always prefer the on-disk copy — it matches the installed version.
106219

@@ -117,7 +230,7 @@ Online directory: <https://github.com/RapidFireAI/rapidfireai/tree/main/tutorial
117230

118231
---
119232

120-
## 7. Validation checklist (run before reporting "done")
233+
## 10. Validation checklist (run before reporting "done")
121234

122235
Before telling the user setup is complete, confirm each:
123236

@@ -128,12 +241,14 @@ Before telling the user setup is complete, confirm each:
128241
5. `rapidfireai start` brought up the stack without errors. The user can reach the dashboard URL printed by `start`.
129242
6. (Remote/cloud) The `ssh -L` forward is active in a separate terminal.
130243
7. (Tutorial path) The notebook's kernel is the project `.venv`, not the system Python. The first import cell (`from rapidfireai import Experiment`) runs without `ImportError`.
244+
8. (Code-integration done) `Experiment(...)` constructs without raising; the user's `create_model_fn` (fit) or `preprocess_fn` (evals) runs on a single example without error.
245+
9. `experiment.end()` is called at the end of the user's script/notebook.
131246

132247
If any check fails, stop and diagnose — do not paper over with retries.
133248

134249
---
135250

136-
## 8. References
251+
## 11. References
137252

138253
- **Canonical install instructions and troubleshooting**: <https://github.com/RapidFireAI/rapidfireai/blob/main/README.md>
139254
- **Full documentation**: <https://oss-docs.rapidfire.ai>

0 commit comments

Comments
 (0)