# GritLM

> One model for both text embedding and text generation, switched by instruction

- Category: Retrieval & Knowledge (RAG) / Embedding Models & Inference
- Language: Python
- Repository: https://github.com/ContextualAI/gritlm
- Canonical page: https://ai-tldr.dev/tools/gritlm/

## In short

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.

1. Install

   Install the package from PyPI.

```bash
pip install gritlm
```

2. Load the model

   Load GritLM-7B for both capabilities. Pass mode="embedding" if you only need vectors.

```python
from gritlm import GritLM

model = GritLM("GritLM/GritLM-7B", torch_dtype="auto")
```

3. Create embeddings

   Wrap your instruction in the model's embed template, then encode queries and documents. Retrieval documents need no instruction.

```python
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(""))
```

4. Generate text

   Use the tokenizer's chat template and call generate, the same way you would with a standard chat model.

```python
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])
```

## 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
