What is GritLM?
GritLM is the open-source code and model family for Generative Representational Instruction Tuning (GRIT), a training method that lets a single large language model do two jobs that usually need two separate models: producing text embeddings and generating text. It sits in the Embedding Models & Inference category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python.
Overview
GritLM is the open-source code and model family for Generative Representational Instruction Tuning (GRIT), a training method that lets a single large language model do two jobs that usually need two separate models: producing text embeddings and generating text. You tell the model which job to do through instructions, so the same weights answer a chat prompt or return a vector for retrieval.
It ships as a small Python package (gritlm) that wraps Hugging Face models. The released GritLM-7B and GritLM-8x7B weights are reported to score well on the Massive Text Embedding Benchmark (MTEB) while still handling generative tasks. It is aimed at developers building retrieval and Retrieval-Augmented Generation (RAG) systems who would otherwise run a dedicated embedder alongside a generator.
Within the embedding-models category, GritLM stands out because it is not embedding-only. The same model can encode documents into vectors and then generate an answer from them, which the paper notes can speed up RAG on long documents by reusing one model instead of two.
What it does
- Single model for both embedding and generation, selected at call time through instructions
- Task is steered with a plain-text instruction (e.g. "Given a scientific paper title, retrieve the paper's abstract")
- Released GritLM-7B and GritLM-8x7B checkpoints, downloadable from Hugging Face
- model.encode() returns vectors; model.generate() with a chat template produces text
- Optional mode="embedding" load to drop the LM head and save memory when you only need vectors
- Wraps Hugging Face from_pretrained, so kwargs like device_map="auto" work for multi-GPU loading
Getting started
Install the package, load a GritLM checkpoint, then call encode for embeddings or generate for text.
Install
Install the package from PyPI.
pip install gritlmLoad the model
Load GritLM-7B for both capabilities. Pass mode="embedding" if you only need vectors.
from gritlm import GritLM
model = GritLM("GritLM/GritLM-7B", torch_dtype="auto")Create embeddings
Wrap your instruction in the model's embed template, then encode queries and documents. Retrieval documents need no instruction.
def gritlm_instruction(instruction):
return "<|user|>\n" + instruction + "\n<|embed|>\n" if instruction else "<|embed|>\n"
instruction = "Given a scientific paper title, retrieve the paper's abstract"
q_rep = model.encode(queries, instruction=gritlm_instruction(instruction))
d_rep = model.encode(documents, instruction=gritlm_instruction(""))Generate text
Use the tokenizer's chat template and call generate, the same way you would with a standard chat model.
messages = [{"role": "user", "content": "Write me a short poem about Mt. Fuji."}]
encoded = model.tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
encoded = encoded.to(model.device)
gen = model.generate(encoded, max_new_tokens=256, do_sample=False)
print(model.tokenizer.batch_decode(gen)[0])Commands and code are distilled from the project's own documentation — always check the official repo for the latest.
When to use it
- Building a RAG pipeline where one model both embeds the documents and writes the answer, instead of running a separate retriever and generator
- Semantic search and document retrieval using instruction-conditioned embeddings
- Reproducing or extending the GRIT paper's results on MTEB and generative benchmarks
- Prototyping retrieval and chat features without standing up two separate model services
How GritLM compares
GritLM alongside other open-source embedding models & inference tools AI/TLDR tracks, ranked by GitHub stars.
| Tool | Stars | What it does |
|---|---|---|
| Sentence Transformers | ★ 19.3k | The standard Python framework for loading, training, and computing embeddings with sentence and reranking models. |
| OpenCLIP | ★ 14.3k | An open implementation of CLIP for training and running image-text embedding models, with a large catalogue of pretrained checkpoints for zero-shot classification and retrieval. |
| PyTorch Metric Learning | ★ 6.3k | PyTorch library of losses, miners, distances, samplers, trainers and accuracy testers for training embedding models with deep metric and self-supervised learning. |
| EmbeddingGemma (Gemma) | ★ 5.9k | Google DeepMind's Gemma repo, home to EmbeddingGemma, a 308M multilingual embedding model small enough to run on-device for RAG and semantic search. |
| Text Embeddings Inference (TEI) | ★ 5.1k | Hugging Face's Rust-based server for deploying embedding, reranking, and sequence-classification models with high throughput on GPU or CPU. |
| text2vec | ★ 5k | Python library for sentence embeddings and text similarity, implementing Word2Vec, BM25, Sentence-BERT, CoSENT and BGE with Chinese and multilingual models. |
| Infinity (Embeddings) | ★ 3k | A high-throughput serving engine for text embeddings, rerankers, CLIP, and ColPali models, exposing an OpenAI-compatible API. |
| GritLM | ★ 702 | One model for both text embedding and text generation, switched by instruction |

