LLM Topic Labeling¤
llm_labeler provides utilities for assigning concise labels to MALLET topic clusters using OpenAI-compatible, Gemini, or Claude endpoints.
Configuration and client¤
TopicLabelerConfig
pydantic-model
¤
Bases: BaseModel
Configuration for the TopicLabelerClient.
Fields:
-
provider(str) -
model(str) -
api_key(Optional[str]) -
base_url(Optional[str]) -
n_terms(Optional[int]) -
temperature(Optional[float]) -
max_tokens(Optional[int]) -
documents_snippet(Optional[str]) -
prompt(Optional[str]) -
include_reasoning(Optional[bool]) -
timeout(Optional[int]) -
max_retries(Optional[int])
Validators:
Source code in lexos/topic_modeling/mallet/llm_labeler.py
api_key: Optional[str] = None
pydantic-field
¤
API key for the LLM provider, if required.
base_url: Optional[str] = None
pydantic-field
¤
Base URL for the LLM provider's API, if different from the default.
documents_snippet: Optional[str] = ''
pydantic-field
¤
Optional snippet of contextual context from the documents.
include_reasoning: Optional[bool] = False
pydantic-field
¤
Whether to request reasoning (thinking) from models that support it.
max_retries: Optional[int] = 5
pydantic-field
¤
Maximum number of retries for API requests in case of rate limiting.
max_tokens: Optional[int] = 50
pydantic-field
¤
Default to short, concise outputs.
model: str
pydantic-field
¤
The specific model to use from the provider.
n_terms: Optional[int] = 15
pydantic-field
¤
Number of top terms to consider from the topic model cluster.
prompt: Optional[str] = None
pydantic-field
¤
Optional custom prompt to override the default prompt.
temperature: Optional[float] = 0.1
pydantic-field
¤
Default to low creativity for clean labeling.
timeout: Optional[int] = 120
pydantic-field
¤
Timeout in seconds for API requests to the LLM provider.
__init__(**data)
¤
Initializes the TopicLabelerConfig with the specified provider in lower case.
check_api_auth() -> TopicLabelerConfig
pydantic-validator
¤
Ensures either an api_key or a base_url is provided.
Source code in lexos/topic_modeling/mallet/llm_labeler.py
TopicLabelerClient
pydantic-model
¤
Bases: BaseModel
Client for labeling topics using various LLM providers.
Fields:
Source code in lexos/topic_modeling/mallet/llm_labeler.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
config: TopicLabelerConfig
pydantic-field
¤
Configuration for the TopicLabelerClient.
generate_label(top_words: list[str]) -> str
¤
Sends a structured prompt to the selected model provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
top_words
|
list[str]
|
List of high-frequency words from a topic model cluster. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated label for the topic. |
Source code in lexos/topic_modeling/mallet/llm_labeler.py
generate_label(top_words: list[str]) -> str
¤
Sends a structured prompt to the selected model provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
top_words
|
list[str]
|
List of high-frequency words from a topic model cluster. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated label for the topic. |
Source code in lexos/topic_modeling/mallet/llm_labeler.py
_call_openai_compatible(prompt: str) -> str
¤
Calls an OpenAI-compatible API endpoint with the given prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to send to the model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated label from the model. |
Source code in lexos/topic_modeling/mallet/llm_labeler.py
_call_gemini(prompt: str) -> str
¤
Handles Google Gemini API endpoints with the given prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to send to the model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated label from the model. |
Source code in lexos/topic_modeling/mallet/llm_labeler.py
_call_claude(prompt: str) -> str
¤
Handles Anthropic Claude API endpoints with the given prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to send to the model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated label from the model. |
Source code in lexos/topic_modeling/mallet/llm_labeler.py
Batch labeling¤
label_mallet_topics(topic_keys_path: str, config: TopicLabelerConfig, topic_nums: Optional[int | list[int]] = None) -> dict[int, str]
¤
Parses Mallet's output topic keys file and assigns AI labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
topic_keys_path
|
str
|
Path to the Mallet output file (usually |
required |
config
|
TopicLabelerConfig
|
TopicLabelerConfig instance containing provider, model, api_key, and base_url |
required |
topic_nums
|
Optional[int | list[int]]
|
Optional list of topic numbers to label. If None, all topics will be labeled. |
None
|
Returns:
| Type | Description |
|---|---|
dict[int, str]
|
A dictionary mapping topic IDs to their generated labels. |