LlamaRec: Two-Stage Recommendation using Large Language Models for Ranking

Zhenrui Yue, Sara Rabhi, Gabriel de Souza Pereira Moreira, Dong Wang, Even Oldridge · DOI

Introduction

Large Language Models (LLMs) have demonstrated a remarkable ability to understand complex user preferences through natural language. However, integrating them into real-time recommendation systems (RecSys) poses significant challenges. Standard LLMs generate text autoregressively—one token at a time—which is far too slow for production environments where users expect millisecond response times. Furthermore, many LLM-based recommenders are "training-free," limiting their ability to adapt to specific dataset patterns.

LlamaRec addresses these hurdles by introducing a two-stage framework that combines the efficiency of traditional sequential models with the deep semantic reasoning of Llama 2. By utilizing a "verbalizer" technique, LlamaRec achieves state-of-the-art accuracy with a 50x speedup in inference time compared to traditional generative approaches.

Methods

LlamaRec utilizes a classic two-stage architecture designed to optimize both retrieval speed and ranking precision:

  1. Retrieval Stage: A lightweight model called LRURec (Linear Recurrent Unit for Recommendation) efficiently scans the item catalog to retrieve the top 20 most relevant candidates based on the user's interaction history.
  2. Ranking Stage: These candidates are then processed by a Llama 2 (7B) model for fine-grained re-ranking.
The LlamaRec pipeline: from user interactions and retrieval to prompt construction and LLM-based ranking.

The Verbalizer Technique

The core innovation of LlamaRec is the Verbalizer. Instead of requiring the LLM to generate a full movie title (which is slow and prone to formatting errors), the system formats the prompt to list candidates with index letters (e.g., (A) Toy Story, (B) Star Wars).

To rank these items, the model does not actually generate text. Instead, it performs a single forward pass and extracts the probability distribution (logits) for the very next token. By comparing the scores for tokens "A", "B", "C", etc., the system can rank all 20 candidates simultaneously. This eliminates the bottleneck of autoregressive generation.

Fine-Tuning

The model is optimized using QLoRA, a parameter-efficient fine-tuning (PEFT) method. This allows the 7B parameter model to be tuned on consumer-grade hardware by updating less than 1% of the total parameters, ensuring the model learns the specific nuances of the recommendation dataset.

Datasets and Evaluation

The framework was evaluated on three benchmark datasets:

  • ML-100k: A standard movie recommendation dataset.
  • Amazon Beauty: Product reviews and metadata for beauty items.
  • Amazon Games: Video game reviews and ratings.

Items were represented by their text titles, allowing the LLM to leverage its pre-existing world knowledge regarding semantic relationships between products.

Key Results

LlamaRec consistently outperformed state-of-the-art baselines such as SASRec and BERT4Rec. On the ML-100k dataset, it achieved an improvement of over 12% in MRR@5 (Mean Reciprocal Rank).

Beyond accuracy, the efficiency gains were transformative. In inference testing, LlamaRec maintained a latency of under one second regardless of the length of the item titles. In contrast, a standard generative LLM approach (CausalLM) saw latency scale linearly with title length, taking over 50 seconds for the same task.

Inference time comparison: LlamaRec maintains near-constant latency, while standard CausalLM generation time increases with title length.

Discussion and Limitations

LlamaRec demonstrates that "small" retrieval models and "large" ranking models are highly complementary. The small model solves the "needle in a haystack" problem, while the LLM handles fine-grained preference modeling.

However, some limitations remain. The system currently caps user history at the 20 most recent items to keep the prompt manageable, which may ignore long-term interests. Additionally, the system's performance is tied to the quality of text metadata; if item titles are non-descriptive (e.g., serial numbers), the LLM's semantic advantage is diminished. Future work may involve incorporating multi-modal data, such as item images, to further enhance recommendation quality.

Summary updated

BibTeX
@misc{yue2023llamarec,
  author = {Zhenrui Yue and Sara Rabhi and Gabriel de Souza Pereira Moreira and Dong Wang and Even Oldridge},
  title = {{LlamaRec: Two-Stage Recommendation using Large Language Models for Ranking}},
  year = {2023},
  eprint = {2311.02089},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2311.02089},
}
Figure