Views
No views yet
LLM2Vec-Gen is a recipe to train interpretable, generative embeddings that encode the potential answer of an LLM to a query rather than the query itself.
pip install llm2vec-gen1import torch
2from llm2vec_gen import LLM2VecGenModel
3
4model = LLM2VecGenModel.from_pretrained("McGill-NLP/LLM2Vec-Gen-Qwen3-8B")1q_instruction = "Generate a passage that best answers this question: "
2d_instruction = "Summarize the following passage: "
3
4queries = [
5 "where do polar bears live and what's their habitat",
6 "what does disk cleanup mean on a computer"
7]
8q_reps = model.encode([q_instruction + q for q in queries])
9
10documents = [
11 "Polar bears live throughout the circumpolar North in the Arctic, spanning across Canada, Alaska (USA), Russia, Greenland, and Norway. Their primary habitat is sea ice over the continental shelf, which they use for hunting, mating, and traveling. They are marine mammals that rely on this environment to hunt seals.",
12 "Disk Cleanup is a built-in Windows tool that frees up hard drive space by scanning for and deleting unnecessary files like temporary files, cached data, Windows updates, and items in the Recycle Bin. It improves computer performance by removing \"junk\" files, which can prevent the system from running slowly due to low storage.",
13]
14d_reps = model.encode([d_instruction + d for d in documents])
15
16# Compute cosine similarity
17q_reps_norm = torch.nn.functional.normalize(q_reps, p=2, dim=1)
18d_reps_norm = torch.nn.functional.normalize(d_reps, p=2, dim=1)
19cos_sim = torch.mm(q_reps_norm, d_reps_norm.transpose(0, 1))
20
21print(cos_sim)
22"""
23tensor([[0.8789, 0.0938],
24 [0.1143, 0.9297]])
25"""1_, recon_hidden_states = model.encode("what does disk cleanup mean on a computer", get_recon_hidden_states=True)
2# recon_hidden_states: torch.Tensor with shape (1, compression token size, hidden_dim)
3
4answer = model.generate(recon_hidden_states=recon_hidden_states, max_new_tokens=55)
5
6print(answer)
7"""
8**Disk Cleanup**" is a built-in utility in Windows that helps you **free up disk space** by **removing unnecessary files** and **temporary data** that are no longer needed. [...]"""recon_hidden_states).parishad.behnamghader@mila.quebec) and Vaibhav (vaibhav.adlakha@mila.quebec).@article{behnamghader2026llm2vecgen,
title={LLM2Vec-Gen: Generative Embeddings from Large Language Models},
author={BehnamGhader, Parishad and Adlakha, Vaibhav and Schmidt, Fabian David and Chapados, Nicolas and Mosbach, Marius and Reddy, Siva},
journal={arXiv preprint: arXiv:2603.10913},
year={2026}
url={https://arxiv.org/abs/2603.10913}
}