Embeddings
Fine-tune an embedding model on your own docs and codebase for better search.
General-purpose embedding models often miss internal acronyms, product names, and custom schemas. Fine-tuning a small encoder with EmbeddingRecipe on your own query/document pairs teaches it those terms, and the model stays small enough to embed queries in milliseconds. The eval report shows the held-out top-10 hit rate (recall_at_10) before and after, so you can see the gain on your own data.
What data you need#
Each training example is a triplet with three parts:
query: what a user (or agent) searched for.positive: the doc or code snippet that actually answers the query.negatives: one or more wrong docs that look similar to the right one, so the model learns to rank the right doc above them.
Aim for 1,000–5,000 triplets in a Dataset. Push them as plain records (a .jsonl path with the same keys works too, and grad.Triplet(...) is available if you want type checking):
from grad import Grad
client = Grad()
dataset = client.datasets.push("docs-retrieval-triplets", [
{
"query": "how do I rotate my api key?",
"positive": "To rotate an existing API key, open Settings > API Keys and click Regenerate.",
"negatives": [
"To create your first API key, open Settings > API Keys and click New Key.",
"API keys are scoped to a single workspace and can be revoked at any time.",
],
},
])
Why wrong docs should be close misses: if the query is "rotate my api key" and the negative is "Pricing FAQ", any base model already knows those are unrelated and learns almost nothing. Pulling close-but-wrong results from your current search or RAG pipeline (like "create an API key" when the user asked to rotate one) teaches the model the exact distinctions it currently gets wrong. If you only have (query, positive) pairs to start, you can omit negatives and EmbeddingRecipe will use other docs in the same batch as negatives, though adding real close-miss negatives improves ranking quality further.
Train with EmbeddingRecipe#
from grad import Grad
from grad.recipes import EmbeddingRecipe
client = Grad()
dataset = client.datasets.get("docs-retrieval-triplets")
run = client.runs.create(
dataset=dataset,
name="docs-embedder",
recipe=EmbeddingRecipe(), # manages its own encoder, baai/bge-small-en-v1.5
).wait()
print(run.artifact.id, run.eval_report)
When to use embeddings vs. decision models vs. Cartridges#
- Use
EmbeddingRecipewhen you search or retrieve across thousands or millions of documents (RAG, code search, semantic deduplication) and general-purpose embeddings miss your domain terms. - Use a Decision model when you want a direct answer (a category, yes/no probability, or score) rather than nearest-neighbor search.
- Use Context Cartridges when your docs fit in the model's context window and are mostly static, so you can skip retrieval and bake them straight into a small KV prefix.
Parameters you might change#
loss=losses.InfoNCE(temp=...): pulls matching(query, positive)pairs together and pushesnegativesapart.tempis0.05by default; lower it (to0.03) to sharpen rankings when wrong docs are very similar to the right one.pool:pooling.Mean()(the default) averages the token vectors;pooling.CLS()uses the first token's vector.model: leave it out to use the recipe-managed encoder,baai/bge-small-en-v1.5.grad models list --recipe embeddingshows any other encoders your server offers.
The recipe trains every weight of the encoder (they're small enough that this beats LoRA), and run.artifact is the checkpoint with the best held-out top-10 hit rate. Every other setting has a default; see the training parameters reference.
Download and embed#
The artifact is a standard transformers model directory with the sentence-transformers pooling and normalization files, so you can download it and load it with SentenceTransformer. Vectors are L2-normalized and use the trained pooling:
from grad import Grad
client = Grad()
client.artifacts.download("docs-embedder@latest", "./docs-embedder", format="files")
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("./docs-embedder")
vectors = model.encode(["rotate api keys"])
Without sentence-transformers, read grad_embedding.json for the pooling and sequence length and embed with transformers directly; the artifact formats spec has the reference loader.
On Gradiated Cloud, you'll publish it as a model version from the console or the Models API and embed through the OpenAI SDK's embeddings.create(model="your-org/docs-embedder", ...).
Want more control?#
Custom loss functions, embeddings you can truncate to smaller sizes, and rerankers that score query/doc pairs directly are coming soon with Advanced objectives and Custom loops.