- This Notebook provides a comprehensive analysis using BERTopic, a state-of-the-art topic modeling technique, applied to the lmsys-chat-1m dataset on Hugging Face.
- The primary goal of this analysis is to delve into the dataset to uncover and understand the prevalent topics discussed in user interactions.
- This insight is crucial for training new models and finetuning older models on most sought after topics.
- The lmsys-chat-1m dataset comprises over one million user interaction entries, making it a rich source for understanding natural language processing in conversational AI.
- As the dataset is request access and not completely open, The notebook needs to have a basic authentication token, It can be done using Google Colab Secrets for safety reasons.
- Following are the columns of the dataset ->
Columns/Dataset Features:
conversation_id, model, conversation, turn, language, openai_moderation, redactedRows/Number of Datapoints:1,000,000
- Each entry in the "conversation" represents an array of dictionaries of both user prompts and assistant responses.
- General structure of the conversation looks like ->
- Although due to resource constraints of Google Colab, I have used only 10% subset of the original dataset.
Pandas DataFrames offer a more intuitive interface for data manipulation and are generally faster due to the optimized nature of the library.
The dataset in question was substantial in size, leading to excessive RAM consumption when attempting to load the entire dataset into memory for conversion at once. This issue was causing the computational notebook to crash.
To mitigate the memory overload issue, a batching system was implemented. The process involves:
- Extracting segments of the dataset of smaller size.
- Sequentially appending each batch to a Pandas DataFrame.
- Iterating over 1 & 2 until n% of the data is loaded.
The dataset contains multilingual data, which can lead to inconsistencies and inaccuracies in the analysis, as it complicates the linguistic processing and may skew the interpretation of topics.
A language based filtering step was implemented to retain only English language texts.
This approach ensures that the dataset is homogenized, reducing complexity and improving the predictability of the model outcomes.
The language based filtered out data is stored in df_english.
2. Handling Multiple Interaction Turns in Conversations & Extraction of Text from the Complex Structure
The dataset includes conversations that feature multiple interaction turns between a user and an assistant within a single conversation entry. For effective topic modeling, it is essential to capture the essence of all user prompts throughout the conversation, not just the initial ones.
The dataset exhibits a complex nested structure and for effective implementation of BERTopic, which requires a simplified array of strings format, it is crucial to perform selective content extraction.
The challenge lies in efficiently isolating and extracting only the user prompts from this intricate conversation structure, as these prompts contain the primary content necessary for our topic modeling analysis.
Each conversation row is structured as follows:
To address this challenge, the concatenate_user_messages() function was developed.
This function extracts all user prompts from a conversation by matching the "role" of each dictionary with "user" in the array and then concatenates these messages into a single continuous string.
This method ensures that every part of the user's input is considered, providing a comprehensive basis for subsequent topic modeling and analysis.
The function concatenate_user_messages is applied to each conversation within the df_english['conversation'] column iteratively and stored in a new column named combined_user_prompts.
The dataset contains extensive privacy-oriented redactions, where personal identifiers have been systematically replaced with standardized placeholders (e.g., NAME_1, NAME_2).
This redaction process affects more than 25% of the dataset entries, presenting a significant challenge for topic modeling accuracy.
The prevalence of these redacted placeholders poses a critical methodological challenge for topic modeling:
- The high frequency of standardized placeholders can lead to their misidentification as significant topics
- BERTopic's algorithm may incorrectly prioritize these non-contextual elements due to their repetitive nature
- The semantic value of the content is potentially compromised when these placeholders are treated as meaningful tokens
Consider the following example:
"NAME_1 went to the park on a run"
In this context, while "park" and "run" carry genuine topical significance related to outdoor activities and leisure, the placeholder "NAME_1" represents semantically irrelevant information that could skew topic modeling results.
To address this challenge, an efficient Regular Expression (RegEx) based approach was implemented. This solution:
- Provides O(n) time complexity for identifier removal
- Eliminates the need for complex NLP-based name recognition systems which would be much slower.
- Ensures consistent and accurate removal of standardized placeholders.
The dataset contains extensive numerical elements within user prompts, particularly in instructional contexts (e.g., "write 500 words about..." or "list 10 ways to...").
While these numbers are relevant for instruction purposes, they represent non-contextual noise for topic modeling analysis.
The presence of these numerical values poses several challenges for effective topic modeling:
- Repetitive numerical patterns can be mistakenly identified as significant topics.
- The semantic clarity of the content is potentially diluted by numerical noise as they add unnecessary dimensionality.
Consider the following example:
"Write 500 words about climate change and list 3 main impacts"
In this context, while "climate change" and "impacts" carry genuine topical significance, the numbers "500" and "3" represent semantically irrelevant information that could interfere with accurate topic identification.
To address this challenge, a streamlined Regular Expression (RegEx) based function was implemented. This solution:
- Maintains the integrity of the textual content while eliminating numerical noise.
- Implements a simple yet effective cleaning mechanism with O(n) computation.
The dataset exhibits significant length disparities in conversation turns, with some entries containing exceptionally long dialogue sequences (up to 214 turns) compared to the typical range of 1-5 turns. This imbalance presents a substantial challenge for effective topic modeling.
The presence of these lengthy conversations introduces several critical concerns:
- Disproportionate influence on topic distribution due to sheer content volume.
- Representation bias towards themes present in longer conversations
- Very high number of turns create a significantly skewed distribution compared to typical conversations.
To address this challenge, a strategic truncation approach was implemented:
- Limiting conversations to 150 words or less
- Affecting less than 0.2% of the dataset (approximately 20 conversations in 100,000)
- Maintaining dataset integrity while reducing computational overhead
Some other ways of doing it would have been
- Undersampling : Causes Loss of Data for 99.8% of Data.
- Oversampling: Causes very very high domination of 0.2% of data and is bad for topic modelling.
- Data Augmentation: High Computation and Unpredictable Behaviour as loss of context is possible.
- Choosing Data Normalization by length is practical and affects only the miniscule 0.2% of data.
The dataset contains abundant stop words (such as 'the', 'is', 'at', 'which') that add minimal semantic value to the text analysis. These common words appear with high frequency across all documents, potentially creating noise in our topic modeling process.
- Creates unnecessary computational overhead during processing
- Leads to less distinctive topic clusters due to the prevalence of common words
Consider the following example:
Original: "The cat is sitting on the mat in the garden and it is sleeping"
After Stop Word Removal: "cat sitting mat garden sleeping"
In this context, while the original sentence contains 13 words, the processed version retains only 5 key content words that carry the actual semantic meaning.
The removal of stop words maintains the core meaning while significantly reducing the text length.
A spaCy-based stop words removal approach was implemented. This solution:
- Leverages spaCy's built-in stop words list for efficient filtering
- Optimizes the dataset for faster topic modeling performance
The dataset contains multiple variations of words (such as 'running', 'runs', 'ran') that represent the same core concept.
These variations create unnecessary complexity and dimensionality in our topic modeling process, Thus mapping all of them to a base word is good idea.
The presence of varied word forms affects topic modeling efficiency in several ways:
- Creates redundant feature dimensions in the vector space.
- Leads to scattered semantic relationships for essentially same concepts.
- Reduces the effectiveness of term frequency calculations due to split word counts.
Consider the following example:
Original: "The children are running and playing in the gardens"
After Lemmatization: "child run and play in garden"
In this example, while maintaining the core meaning, lemmatization transforms 'children' to 'child', 'running' to 'run', 'playing' to 'play', and 'gardens' to 'garden', reducing word variations while preserving semantic content.
A spaCy-based lemmatization approach was implemented. This solution:
- Utilizes spaCy's sophisticated morphological analysis for accurate lemmatization
- Maintains semantic consistency while reducing vocabulary size
- Enhances topic modeling by consolidating related word forms
In this step, All the preprocessing functions were serially applied on the combined_user_prompts and then stored in a new column called processed_text.
df_english['processed_text'] = (df_english['combined_user_prompts']
.progress_apply(remove_redacted_names)
.progress_apply(truncate_to_first_n_words)
.progress_apply(remove_stop_words)
.progress_apply(lemmatize_text)
.progress_apply(remove_digits))# Original text: "NAME_1 spent 25 minutes running in the park yesterday"
# After sequential processing:
# 1. remove_redacted_names() -> "spent 25 minutes running in the park yesterday"
# 2. truncate_to_first_n_words() -> "spent 25 minutes running in the park yesterday"
# 3. remove_stop_words() -> "spent 25 minutes running park yesterday"
# 4. lemmatize_text() -> "spend 25 minute run park yesterday"
# 5. remove_digits() -> "spend minute run park yesterday"
# And Now, The text is ready for topic modelling.BERTopic is a cutting-edge topic modeling technique that uses transformer-based embeddings, such as BERT, combined with clustering and dimensionality reduction techniques to uncover hidden topics in textual data. Unlike traditional models like Latent Dirichlet Allocation (LDA), BERTopic leverages contextual embeddings, enabling it to produce more semantically meaningful topics.
- What it does: Utilizes transformer models (e.g.,
paraphrase-mpnet-base-v2) to generate contextual embeddings that represent text in a high-dimensional space. - Advantage: Captures semantic nuances of text, outperforming traditional bag-of-words models.
- Technique: UMAP (Uniform Manifold Approximation and Projection)
- Purpose: Reduces high-dimensional embeddings into a lower-dimensional space while preserving semantic relationships.
- Parameters:
n_neighbors: Controls local vs. global structure preservation.n_components: Number of dimensions in the reduced space.min_dist: Controls clustering density.metric: Defines distance calculations (e.g.,cosine).
- Technique: HDBSCAN (Hierarchical Density-Based Spatial Clustering of Applications with Noise)
- Purpose: Groups similar embeddings into clusters.
- Parameters:
min_cluster_size: Minimum documents per cluster.min_samples: Controls outlier sensitivity.metric: Defines the clustering distance measure (e.g.,euclidean).
- Technique: c-TF-IDF (Class-based Term Frequency-Inverse Document Frequency)
- Purpose: Identifies the most representative terms for each topic.
- Result: Enhances interpretability of discovered topics.
Below is a visual representation of BERTopic’s workflow:
Below is a detailed explanation of the configurations used in our BERTopic model:
| Component | Parameter | Value | Effect |
|---|---|---|---|
| Embedding Model | embedding_model |
paraphrase-mpnet-base-v2 |
Provides contextual embeddings with high semantic accuracy. |
| UMAP | n_neighbors |
20 | Balances local and global structure preservation. |
n_components |
8 | Sets the dimensionality of the reduced space. | |
min_dist |
0.1 | Lower values create denser clusters. | |
metric |
cosine |
Measures similarity using cosine distance. | |
| HDBSCAN | min_cluster_size |
50 | Ensures meaningful cluster sizes. |
min_samples |
10 | Balances sensitivity to outliers. | |
metric |
euclidean |
Defines clustering distance calculations. | |
| Topic Representation | top_n_words |
20 | Highlights the top 20 words for each topic. |
Below are some of the prominent topics discovered by our BERTopic model:
| Topic ID | Name | Count | Representative Terms |
|---|---|---|---|
| 0 | Roleplay |
3,987 | story, girl, character, game, roleplay |
| 1 | AI Assistant Queries |
2,809 | assistant, completion, repeat, system, instruction |
| 2 | Programming |
1,079 | import, int, self, const, return, class |
| 3 | Business and Industry |
962 | china, ltd, co, introduction, chemical |
| 4 | Coding Assistance |
721 | code, function, loop, debug, variable |
| 5 | Educational Resources |
643 | book, tutorial, explain, learn, teach |
| 6 | Physics & Mathematics |
489 | equation, solve, gravity, acceleration, theorem |
-
Intertopic Distance Map
- Description: Visualizes the relationships between topics in a 2D space using UMAP-reduced embeddings.
- Key Takeaways:
- Topics positioned closer together share higher semantic similarity.
- Dense clusters indicate closely related or overlapping topics.
-
Topic Word Scores Bar Chart
- Description: Displays the top representative words for selected topics along with their c-TF-IDF scores.
- Key Takeaways:
- Highlights the most significant terms contributing to each topic.
- Provides a quick comparison of word importance across topics.
-
Similarity Matrix
- Description: Shows the semantic similarity scores between topics.
- Key Takeaways:
- Darker cells indicate higher similarity between topic pairs.
- Useful for identifying groups of interrelated topics.
-
Topic Probability Distribution
- Description: Visualizes the distribution of topic probabilities for documents in the dataset.
- Key Takeaways:
- Shows the relative dominance of topics across the corpus.
- Highlights the prevalence of dominant and secondary topics in the dataset.
- Topic Evolution: The model enables dynamic topic modeling, making it possible to track changes in topics over time or across specific intervals in the dataset.
- Application Scope:
- Customer Feedback Analysis: Extract recurring themes from user reviews or customer service logs.
- Content Categorization: Automatically classify documents, articles, or blogs into relevant categories.
- Academic Research: Reveal trends, methodologies, and key topics across scientific literature.
- AI Training: Provides a foundation for training and fine-tuning conversational AI models based on prevalent topics.
This Notebook provides a comprehensive analysis using BERTopic, a state-of-the-art topic modeling technique, applied to the lmsys-chat-1m dataset on Hugging Face. The primary goal was to delve into the dataset to uncover and understand the prevalent topics discussed in user interactions. These insights are crucial for training new models and fine-tuning older models on the most sought-after topics.
The task involved uncovering dominant themes and their distributions in a dataset with multilingual content and varying conversational structures. The robust preprocessing pipeline solved subproblems such as:
- Handling multilingual data to ensure consistent insights.
- Resolving conversational imbalances due to varying turn lengths.
- Managing redacted or noisy information and addressing numerical anomalies in the data.
- Streamlining text using lemmatization and stop word elimination through spaCy.
The sequential processing workflow enabled a clean, consistent dataset that could be effectively modeled.
- Topic Insights: Dominant themes such as AI Assistant Queries, Programming Concepts, Educational Resources, and others were identified and visualized using inter-topic distance maps and probability distributions.
- Advanced Preprocessing: Successfully addressed multilingual challenges, redacted data issues, and conversational imbalances.
- Versatile Applications: The model's insights are applicable across domains like content categorization, conversational AI training, and customer feedback analysis.
- Visualization Mastery: Interactive visualizations provided deep insights into topic relationships, term significance, and dataset structure.
- Real-time Evolution Tracking: Enhance the pipeline to dynamically track topic changes across time.
- Multilingual Integration: Extend support for diverse datasets with varied linguistic and structural patterns.
- Customized Applications: Tailor the workflow to meet specific industry needs, such as domain-specific categorization or customer experience optimization.
- Robust Training for Robust Performance : Training the model more robustly covering 100% of the dataset and handle the outlier cases better for better insights.
The methodologies demonstrated here highlight the power of modern NLP techniques in managing and interpreting complex datasets, paving the way for innovative, actionable applications in both research and industry.
- lmsys-chat-1m - A dataset of user interactions with an AI chatbots.
- BERTopic - A state-of-the-art topic modeling technique.





