Initially revealed at https://weblog.developer.bazaarvoice.com on October 28, 2024.
Giant language fashions are incredible instruments for unstructured textual content, however what in case your textual content doesn’t match within the context window? Bazaarvoice confronted precisely this problem when constructing our AI Assessment Summaries function: tens of millions of person opinions merely gained’t match into the context window of even newer LLMs and, even when they did, it could be prohibitively costly.
On this put up, I share how Bazaarvoice tackled this downside by compressing the enter textual content with out lack of semantics. Particularly, we use a multi-pass hierarchical clustering method that lets us explicitly regulate the extent of element we need to lose in trade for compression, whatever the embedding mannequin chosen. The ultimate method made our Assessment Summaries function financially possible and set us as much as proceed to scale our enterprise sooner or later.
Bazaarvoice has been amassing user-generated product opinions for almost 20 years so we’ve got rather a lot of information. These product opinions are utterly unstructured, various in size and content material. Giant language fashions are wonderful instruments for unstructured textual content: they’ll deal with unstructured information and establish related items of data amongst distractors.
LLMs have their limitations, nevertheless, and one such limitation is the context window: what number of tokens (roughly the variety of phrases) could be put into the community without delay. State-of-the-art giant language fashions, resembling Athropic’s Claude model 3, have extraordinarily giant context home windows of as much as 200,000 tokens. This implies you may match small novels into them, however the web continues to be an enormous, every-growing assortment of information, and our user-generated product opinions are not any completely different.
We hit the context window restrict whereas constructing our Assessment Summaries function that summarizes the entire opinions of a selected product on our purchasers web site. Over the previous 20 years, nevertheless, many merchandise have garnered 1000’s of opinions that rapidly overloaded the LLM context window. Actually, we even have merchandise with tens of millions of opinions that will require immense re-engineering of LLMs to have the ability to course of in a single immediate.
Even when it was technically possible, the prices can be fairly prohibitive. All LLM suppliers cost primarily based on the variety of enter and output tokens. As you method the context window limits for every product, of which we’ve got tens of millions, we are able to rapidly run up cloud internet hosting payments in extra of six figures.
To ship Assessment Summaries regardless of these technical, and monetary, limitations, we centered on a relatively easy perception into our information: Many opinions say the identical factor. Actually, the entire concept of a abstract depends on this: assessment summaries seize the recurring insights, themes, and sentiments of the reviewers. We realized that we are able to capitalize on this information duplication to scale back the quantity of textual content we have to ship to the LLM, saving us from hitting the context window restrict and lowering the working value of our system.
To attain this, we would have liked to establish segments of textual content that say the identical factor. Such a job is less complicated mentioned than accomplished: usually folks use completely different phrases or phrases to precise the identical factor.
Thankfully, the duty of figuring out if textual content is semantically related has been an energetic space of analysis within the pure language processing subject. The work by Agirre et. al. 2013 (SEM 2013 shared job: Semantic Textual Similarity. In Second Joint Convention on Lexical and Computational Semantics) even revealed a human-labeled information of semantically related sentences generally known as the STS Benchmark. In it, they ask people to point if textual sentences are semantically related or dissimilar on a scale of 1–5, as illustrated within the desk under (from Cer et. al., SemEval-2017 Job 1: Semantic Textual Similarity Multilingual and Crosslingual Targeted Analysis):
The STSBenchmark dataset is usually used to guage how nicely a textual content embedding mannequin can affiliate semantically related sentences in its high-dimensional house. Particularly, Pearson’s correlation is used to measure how nicely the embedding mannequin represents the human judgements.
Thus, we are able to use such an embedding mannequin to establish semantically related phrases from product opinions, after which take away repeated phrases earlier than sending them to the LLM.
Our method is as follows:
- First, product opinions are segmented the into sentences.
- An embedding vector is computed for every sentence utilizing a community that performs nicely on the STS benchmark
- Agglomerative clustering is used on all embedding vectors for every product.
- An instance sentence — the one closest to the cluster centroid — is retained from every cluster to ship to the LLM, and different sentences inside every cluster are dropped.
- Any small clusters are thought of outliers, and people are randomly sampled for inclusion within the LLM.
- The variety of sentences every cluster represents is included within the LLM immediate to make sure the burden of every sentiment is taken into account.
This may occasionally appear simple when written in a bulleted record, however there have been some devils within the particulars we needed to kind out earlier than we might belief this method.
First, we had to make sure the mannequin we used successfully embedded textual content in an area the place semantically related sentences are shut, and semantically dissimilar ones are distant. To do that, we merely used the STS benchmark dataset and computed the Pearson correlation for the fashions we desired to think about. We use AWS as a cloud supplier, so naturally we wished to guage their Titan Textual content Embedding fashions.
Under is a desk displaying the Pearson’s correlation on the STS Benchmark for various Titan Embedding fashions:
(Cutting-edge is seen right here)
So AWS’s embedding fashions are fairly good at embedding semantically related sentences. This was nice information for us — we are able to use these fashions off the shelf and their value is extraordinarily low.
The following problem we confronted was: how can we implement semantic similarity throughout clustering? Ideally, no cluster would have two sentences whose semantic similarity is lower than people can settle for — a rating of 4 within the desk above. These scores, nevertheless, don’t straight translate to the embedding distances, which is what is required for agglomerative clustering thresholds.
To cope with this challenge, we once more turned to the STS benchmark dataset. We computed the distances for all pairs within the coaching dataset, and match a polynomial from the scores to the gap thresholds.
This polynomial lets us compute the gap threshold wanted to satisfy any semantic similarity goal. For Assessment Summaries, we chosen a rating of three.5, so almost all clusters comprise sentences which might be “roughly” to “principally” equal or extra.
It’s value noting that this may be accomplished on any embedding community. This lets us experiment with completely different embedding networks as they develop into accessible, and rapidly swap them out ought to we need with out worrying that the clusters may have semantically dissimilar sentences.
Up so far, we knew we might belief our semantic compression, however it wasn’t clear how a lot compression we might get from our information. As anticipated, the quantity of compression various throughout completely different merchandise, purchasers, and industries.
With out lack of semantic info, i.e., a tough threshold of 4, we solely achieved a compression ratio of 1.18 (i.e., an area financial savings of 15%).
Clearly lossless compression wasn’t going to be sufficient to make this function financially viable.
Our distance choice method mentioned above, nevertheless, offered an fascinating chance right here: we are able to slowly enhance the quantity of data loss by repeatedly working the clustering at decrease thresholds for remaining information.
The method is as follows:
- Run the clustering with a threshold chosen from rating = 4. That is thought of lossless.
- Choose any outlying clusters, i.e., these with just a few vectors. These are thought of “not compressed” and used for the subsequent part. We selected to re-run clustering on any clusters with dimension lower than 10.
- Run clustering once more with a threshold chosen from rating = 3. This isn’t lossless, however not so dangerous.
- Choose any clusters with dimension lower than 10.
- Repeat as desired, repeatedly lowering the rating threshold.
So, at every move of the clustering, we’re sacrificing extra info loss, however getting extra compression and never muddying the lossless consultant phrases we chosen in the course of the first move.
As well as, such an method is extraordinarily helpful not just for Assessment Summaries, the place we would like a excessive degree of semantic similarity at the price of much less compression, however for different use instances the place we could care much less about semantic info loss however need to spend much less on immediate inputs.
In follow, there are nonetheless a considerably giant variety of clusters with solely a single vector in them even after dropping the rating threshold a variety of instances. These are thought of outliers, and are randomly sampled for inclusion within the last immediate. We choose the pattern dimension to make sure the ultimate immediate has 25,000 tokens, however no extra.
The multi-pass clustering and random outlier sampling permits semantic info loss in trade for a smaller context window to ship to the LLM. This raises the query: how good are our summaries?
At Bazaarvoice, we all know authenticity is a requirement for client belief, and our Assessment Summaries should keep genuine to actually characterize all voices captured within the opinions. Any lossy compression method runs the danger of mis-representing or excluding the customers who took time to creator a assessment.
To make sure our compression method was legitimate, we measured this straight. Particularly, for every product, we sampled a variety of opinions, after which used LLM Evals to establish if the abstract was consultant of and related to every assessment. This provides us a tough metric to guage and stability our compression in opposition to.
Over the previous 20 years, we’ve got collected almost a billion user-generated opinions and wanted to generate summaries for tens of tens of millions of merchandise. Many of those merchandise have 1000’s of opinions, and a few as much as tens of millions, that will exhaust the context home windows of LLMs and run the worth up significantly.
Utilizing our method above, nevertheless, we diminished the enter textual content dimension by 97.7% (a compression ratio of 42), letting us scale this resolution for all merchandise and any quantity of assessment quantity sooner or later.
As well as, the price of producing summaries for all of our billion-scale dataset diminished 82.4%. This consists of the price of embedding the sentence information and storing them in a database.