LLM-based Embeddings: Attention Values Encode Sentence Semantics Better Than Hidden States
Abstract
Lay Summary
Many artificial intelligence systems need to compare pieces of text by meaning, such as finding relevant documents, grouping similar articles, or matching a question with a useful answer. A common way to do this with large language models is to use the model’s final internal representation from the last layer as a sentence “fingerprint.” However, this representation is mainly trained to help the model guess the next word, so it may focus too much on what comes immediately after the sentence rather than on the full meaning of the sentence. In this paper, we study which internal signals are better for creating sentence fingerprints. We find that attention values, another type of information already computed inside the model, provide a clearer signal of sentence meaning than the commonly used final representation. Based on this finding, we introduce a simple method that combines attention values from different words and model layers, without extra training. This method performs better than many existing no-training methods for text search, similarity, grouping, and related tasks. We also show that a prompted version can further improve performance while avoiding the high cost of methods that require many repeated prompts. These findings suggest a simple way to make existing large language models more useful for search and other text understanding tasks.