26  Fine-tune an encoder

Run a LoRA fine-tune on top of an existing text encoder, poll the job to completion, and use the resulting checkpoint to encode a query.

When to use this pattern. Your domain (legal contracts, medical abstracts, patent claims, internal product docs) doesn’t match the distribution the base encoder was trained on, and you have a few hundred to a few thousand labelled or contrastive pairs. LoRA gets you ~80% of the lift of a full fine-tune at a fraction of the cost; the resulting adapter is small enough to ship as an attachment to the base model rather than a re-distributed full checkpoint.

26.1 API surface exercised

  • Session.fine_tune(*, source, base_model, columns, method, task=..., ...)
  • Job.wait()
  • Job.job_id, Job.output_model_id
  • Session.encode_query(*, model, query, modality="text")

The full keyword list on fine_tune covers LoRA rank/alpha/dropout, learning rate, epochs, batch size, max sequence length, validation fraction, early-stopping patience/metric, warmup, gradient accumulation, backbone dtype, weight decay, and gradient clipping — the recipe uses the defaults for everything except rank and epochs.

The program is cookbook/recipes/fine_tune/example.py; its steps run here in order.

import tempfile

import jammi
from jammi_cookbook import fixtures

BASE_MODEL = fixtures.model("tiny_bert")

db = jammi.connect(f"file://{tempfile.mkdtemp()}")

26.2 Register the training pairs

Thirty contrastive pairs, (text_a, text_b, score): how similar the two texts should come out.

db.add_source("training", url=str(fixtures.path("tiny_pairs.csv")), format="csv")

26.3 Submit the fine-tune

fine_tune submits a job and returns its handle at once; wait blocks until the job finishes. The defaults suit production workloads; this keeps the LoRA rank small and runs one epoch so it finishes in seconds. The trained model is registered in the catalog under a jammi:fine-tuned: id.

job = db.fine_tune(
    source="training",
    base_model=BASE_MODEL,
    columns=["text_a", "text_b", "score"],
    method="lora",
    task="text_embedding",
    lora_rank=4,
    epochs=1,
)
assert job.job_id, "fine_tune returned a job without an id"
print(f"job_id:   {job.job_id}")

job.wait()
model_id = job.output_model_id
assert model_id.startswith("jammi:fine-tuned:"), f"unexpected model_id: {model_id}"
print(f"model_id: {model_id}")
job_id:   b2650262-ee6d-40d9-a3e4-9de4d720dbbd
model_id: jammi:fine-tuned:b2650262-ee6d-40d9-a3e4-9de4d720dbbd
2026-10-03T07:43:24.475851Z  WARN jammi_kernels::admission: fused-kernel domain check failed; falling back to the eager composition op="attention_block_fused" predicate="head_dim_is_attention_block_fixed_head_dim"

26.4 Use the fine-tuned model

The new id names a model like any other: here it encodes a query, loaded from the catalog, in the base model’s 32 dimensions.

query_vec = db.encode_query(model=model_id, query="quantum computing applications")
assert len(query_vec) == 32, f"tiny_bert is 32-dim; got {len(query_vec)}-dim from fine-tuned"

26.5 Fine-tune from a graph instead

With no labelled pairs, a graph supplies them: fine_tune_graph pulls together papers that cite each other, sampling positives by random walks over the citation edges and negatives from the graph’s non-neighbours. The nodes and the edges are two ordinary sources.

citations = fixtures.path("tiny_citation_graph")
db.add_source("papers", url=str(citations / "nodes.jsonl"), format="jsonl")
db.add_source("cites", url=str(citations / "edges.jsonl"), format="jsonl")

graph_job = db.fine_tune_graph(
    node_source="papers",
    id_column="id",
    text_column="text",
    edge_source="cites",
    base_model=BASE_MODEL,
    lora_rank=4,
    epochs=1,
    sample_seed=0,
)
graph_job.wait()
assert graph_job.output_model_id.startswith("jammi:fine-tuned:")
print(f"graph-tuned model_id: {graph_job.output_model_id}")
graph-tuned model_id: jammi:fine-tuned:488606af-e33d-4711-a342-ead351113716
db.close()