Scikit-LLM Names Groups from a Corpus of 300 Texts

An example combining Scikit-LLM, local vectorization, and the Groq API organizes 300 texts into groups and then assigns titles generated by a language model. Executable in Google Colab, this method separates semantic ranking from the drafting of labels. The representativeness of the excerpts used to produce these labels is assumed.
Three Groups with Distinct Themes
In the results presented by Iván Palomares Carrascosa, advisor, trainer, speaker, and author specializing in AI, group 0 contains 130 documents. Its title encompasses space narratives, myths, and conversations unrelated to the main theme. Group 1 includes 87 texts, with a label focusing on graphic tools, conversions, and design; group 2 contains 83 documents, associated with a title on car purchasing, maintenance, and unique anecdotes.
The advisor considers the correspondence with the initial categories to be reasonably accurate, particularly for group 0, which largely gathers messages from sci.space. The model responsible for the labels does not receive the groups in their entirety. The documents from each group are randomly mixed, and then up to 10 are retained to reduce the volume sent and comply with the service's token limits.
Each excerpt is limited to the first 300 characters, with line breaks replaced by spaces and ellipses added. A separator then distinguishes the excerpts; only texts exceeding 20 characters after removing peripheral spaces are kept at this stage. The ability of this sample to sufficiently represent the group is an assumption of the setup.
The Remote Model Intervenes After Classification
The division of tasks is explicit: semantic processing occurs locally, and Groq serves the model that writes the summaries. Using sentence-transformers, all-MiniLM-L6-v2 transforms the messages into dense vectors. K-means then distributes these representations into 3 groups, a number fixed in the configuration and not discovered by the language model.
The integration with scikit-learn relies on a custom class, LocalSemanticEmbedder, built with BaseEstimator and TransformerMixin. Its fit method loads the model; transform calls encode to obtain the vectors. A Pipeline object chains the embedder and clusterer components, while KMeans uses random_state=42 and n_init=10. The fit_predict call provides the group assignments.
The AI brief the pros read every evening
The 7 best AI stories, the essentials in 5 min. Free.
Included when you join: our pick of the best AI guides & comparisons.
Choose your rhythm
Free · No spam · Unsubscribe in 1 click
Scikit-LLM takes over with GPTSummarizer, configured on custom_url::openai/gpt-oss-20b; its fit_transform method converts the samples into titles. This step addresses the issue of interpreting unsupervised groupings, which may require a manual review of keywords to identify the theme. The pre-trained model is thus called upon to describe the already formed groups, not to decide their composition.
Iván Palomares Carrascosa advocates for the importance of semantic representations to obtain relevant summaries and believes that a TF-IDF representation would likely have been less satisfactory. He cites the term "engines," common to both automotive and space conversations, to explain potential confusions with a keyword-based approach.
A Corpus from Three Known Categories
The messages come from 20 Newsgroups, a public dataset accessible with the fetch_20newsgroups function from scikit-learn. The selection includes sci.space, rec.autos, and comp.graphics, representing space, automobiles, and computer graphics. Their labels are not used for grouping: these known categories provide a point of comparison to appreciate the identified themes.
The loading excludes headers, footers, and quoted passages. The code then removes spaces from the ends to measure the length of the texts and retains only those exceeding 50 characters. The working corpus corresponds to the first 300 messages meeting this condition, without random sampling at this stage. This cleaning aims, according to Iván Palomares Carrascosa, to eliminate fragments too short to carry an identifiable theme and likely to disrupt the grouping.
A Groq Key to Register in Colab
Execution begins with the installation of scikit-llm and sentence-transformers using pip. Iván Palomares Carrascosa presents the entire setup as free and fully usable in a Google Colab notebook. In this configuration, Scikit-LLM does not send its requests to the OpenAI service by default: it uses the Groq API, whose free models are offered to avoid token billing.
Access requires a Groq account and the generation of a key, displayed only once. This key is registered as GROQ_API_KEY in Colab's secrets, accessible via the key-shaped icon on the left side of the interface. The code then retrieves its value with userdata.get.
The configuration proceeds through the methods SKLLMConfig.set_gpt_url and SKLLMConfig.set_openai_key. The first sets https://api.groq.com/openai/v1 as the OpenAI-compatible access point; the second transmits the Groq key.
Brief AI — AI news in French
The essentials of artificial intelligence news, decoded and explained every day.