lcipolina commited on
Commit
d490fc2
·
verified ·
1 Parent(s): 8cef33b

Added Name cleaner for OpenRouter models

Browse files
Files changed (1) hide show
  1. ui/utils.py +189 -3
ui/utils.py CHANGED
@@ -55,7 +55,7 @@ def get_games_from_databases() -> Set[str]:
55
  return unique_games
56
 
57
 
58
- def clean_model_name(model_name: str) -> str:
59
  """
60
  Clean up long model names to display only the essential model name.
61
 
@@ -87,8 +87,16 @@ def clean_model_name(model_name: str) -> str:
87
  >>> clean_model_name("litellm_gpt-4-turbo")
88
  "GPT-4-turbo"
89
  """
90
- if not model_name or model_name == "Unknown":
91
- return model_name
 
 
 
 
 
 
 
 
92
 
93
  # Handle special cases first
94
  if model_name == "None" or model_name.lower() == "random":
@@ -103,6 +111,11 @@ def clean_model_name(model_name: str) -> str:
103
  # Extract GPT model variants
104
  if "gpt_3.5" in model_name.lower() or "gpt-3.5" in model_name.lower():
105
  return "GPT-3.5-turbo"
 
 
 
 
 
106
  elif "gpt_4" in model_name.lower() or "gpt-4" in model_name.lower():
107
  if "turbo" in model_name.lower():
108
  return "GPT-4-turbo"
@@ -130,6 +143,14 @@ def clean_model_name(model_name: str) -> str:
130
  cleaned = model_part.replace("_", "-")
131
  return cleaned
132
 
 
 
 
 
 
 
 
 
133
  # For vllm models, extract the model name part
134
  if model_name.startswith("vllm_"):
135
  # Remove vllm_ prefix
@@ -138,6 +159,91 @@ def clean_model_name(model_name: str) -> str:
138
  cleaned = model_part.replace("_", "-")
139
  return cleaned
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  # For litellm models without slashes (from database storage)
142
  # These correspond to the slash-separated patterns in the YAML
143
  if model_name.startswith("litellm_"):
@@ -207,5 +313,85 @@ def clean_model_name(model_name: str) -> str:
207
  if "/" in model_name:
208
  return model_name.split("/")[-1].replace("_", "-")
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  # Default: just replace underscores with dashes
211
  return model_name.replace("_", "-")
 
55
  return unique_games
56
 
57
 
58
+ def clean_model_name(model_name) -> str:
59
  """
60
  Clean up long model names to display only the essential model name.
61
 
 
87
  >>> clean_model_name("litellm_gpt-4-turbo")
88
  "GPT-4-turbo"
89
  """
90
+ # Handle NaN, None, and float values
91
+ import pandas as pd
92
+ if pd.isna(model_name) or model_name is None:
93
+ return "Unknown"
94
+
95
+ # Convert to string if it's not already
96
+ model_name = str(model_name)
97
+
98
+ if not model_name or model_name == "Unknown" or model_name == "nan":
99
+ return "Unknown"
100
 
101
  # Handle special cases first
102
  if model_name == "None" or model_name.lower() == "random":
 
111
  # Extract GPT model variants
112
  if "gpt_3.5" in model_name.lower() or "gpt-3.5" in model_name.lower():
113
  return "GPT-3.5-turbo"
114
+ elif "4o" in model_name.lower(): # Handle GPT-4o variants first
115
+ if "mini" in model_name.lower():
116
+ return "GPT-4o-mini"
117
+ else:
118
+ return "GPT-4o"
119
  elif "gpt_4" in model_name.lower() or "gpt-4" in model_name.lower():
120
  if "turbo" in model_name.lower():
121
  return "GPT-4-turbo"
 
143
  cleaned = model_part.replace("_", "-")
144
  return cleaned
145
 
146
+ # For openrouter models, extract everything after the last slash
147
+ if "openrouter_" in model_name and "/" in model_name:
148
+ # Split by "/" and take the last part
149
+ model_part = model_name.split("/")[-1]
150
+ # Clean up underscores and make it more readable
151
+ cleaned = model_part.replace("_", "-")
152
+ return cleaned
153
+
154
  # For vllm models, extract the model name part
155
  if model_name.startswith("vllm_"):
156
  # Remove vllm_ prefix
 
159
  cleaned = model_part.replace("_", "-")
160
  return cleaned
161
 
162
+ # For openrouter models without slashes (from database storage)
163
+ # These correspond to the slash-separated patterns in the YAML
164
+ # Models are stored as llm_openrouter_provider_model_name
165
+ if model_name.startswith("llm_openrouter_"):
166
+ parts = model_name.split("_")
167
+
168
+ # Handle OpenAI models via OpenRouter: llm_openrouter_openai_*
169
+ if len(parts) >= 4 and parts[2] == "openai":
170
+ # Everything after "llm_openrouter_openai_"
171
+ model_parts = parts[3:]
172
+ cleaned = "-".join(model_parts)
173
+ # Clean up common GPT model patterns
174
+ if "gpt" in cleaned.lower():
175
+ # Handle GPT-4o variants first (more specific)
176
+ if "4o" in cleaned:
177
+ # Replace gpt-4o or gpt_4o with GPT-4o
178
+ cleaned = cleaned.replace("gpt-4o", "GPT-4o")
179
+ cleaned = cleaned.replace("gpt_4o", "GPT-4o")
180
+ elif "3.5" in cleaned:
181
+ cleaned = cleaned.replace("gpt-3.5", "GPT-3.5")
182
+ cleaned = cleaned.replace("gpt_3.5", "GPT-3.5")
183
+ elif "gpt-4" in cleaned:
184
+ # Only replace if it's not part of "gpt-4o"
185
+ cleaned = cleaned.replace("gpt-4", "GPT-4")
186
+ cleaned = cleaned.replace("gpt_4", "GPT-4")
187
+ return cleaned
188
+
189
+ # Handle Anthropic models via OpenRouter: llm_openrouter_anthropic_*
190
+ if len(parts) >= 4 and parts[2] == "anthropic":
191
+ # Everything after "llm_openrouter_anthropic_"
192
+ model_parts = parts[3:]
193
+ cleaned = "-".join(model_parts)
194
+ # Capitalize Claude
195
+ if "claude" in cleaned.lower():
196
+ cleaned = cleaned.replace("claude", "Claude")
197
+ return cleaned
198
+
199
+ # Handle Meta Llama models via OpenRouter: llm_openrouter_meta_llama_*
200
+ if len(parts) >= 5 and parts[2] == "meta" and parts[3] == "llama":
201
+ # Everything after "llm_openrouter_meta_llama_"
202
+ model_parts = parts[4:]
203
+ cleaned = "-".join(model_parts)
204
+ # Capitalize Llama
205
+ if "llama" in cleaned.lower():
206
+ cleaned = cleaned.replace("llama", "Llama")
207
+ return cleaned
208
+
209
+ # Handle Google models via OpenRouter: llm_openrouter_google_*
210
+ if len(parts) >= 4 and parts[2] == "google":
211
+ # Everything after "llm_openrouter_google_"
212
+ model_parts = parts[3:]
213
+ cleaned = "-".join(model_parts)
214
+ # Capitalize common Google model names
215
+ if "gemini" in cleaned.lower():
216
+ cleaned = cleaned.replace("gemini", "Gemini")
217
+ elif "gemma" in cleaned.lower():
218
+ cleaned = cleaned.replace("gemma", "Gemma")
219
+ return cleaned
220
+
221
+ # Handle Qwen models via OpenRouter: llm_openrouter_qwen_*
222
+ if len(parts) >= 4 and parts[2] == "qwen":
223
+ # Everything after "llm_openrouter_qwen_"
224
+ model_parts = parts[3:]
225
+ cleaned = "-".join(model_parts)
226
+ # Capitalize Qwen
227
+ cleaned = cleaned.replace("qwen", "Qwen")
228
+ return cleaned
229
+
230
+ # Handle MistralAI models via OpenRouter: llm_openrouter_mistralai_*
231
+ if len(parts) >= 4 and parts[2] == "mistralai":
232
+ # Everything after "llm_openrouter_mistralai_"
233
+ model_parts = parts[3:]
234
+ cleaned = "-".join(model_parts)
235
+ # Capitalize Mistral
236
+ if "mistral" in cleaned.lower():
237
+ cleaned = cleaned.replace("mistral", "Mistral")
238
+ return cleaned
239
+
240
+ # For other llm_openrouter patterns, skip first three parts
241
+ # (llm_openrouter_provider_)
242
+ if len(parts) >= 4:
243
+ model_parts = parts[3:] # Everything after provider
244
+ cleaned = "-".join(model_parts)
245
+ return cleaned
246
+
247
  # For litellm models without slashes (from database storage)
248
  # These correspond to the slash-separated patterns in the YAML
249
  if model_name.startswith("litellm_"):
 
313
  if "/" in model_name:
314
  return model_name.split("/")[-1].replace("_", "-")
315
 
316
+ # Handle intermediate OpenRouter format: openrouter-provider-model-name
317
+ # Also handle underscore version: openrouter_provider_model_name
318
+ if (model_name.startswith("openrouter-") or
319
+ model_name.startswith("openrouter_")):
320
+ # Normalize to use dashes for consistent processing
321
+ normalized_name = model_name.replace("_", "-")
322
+ parts = normalized_name.split("-")
323
+ if len(parts) >= 3: # At least openrouter-provider-model
324
+ # Remove "openrouter" prefix
325
+ remaining_parts = parts[1:]
326
+
327
+ # Handle specific provider patterns
328
+ if remaining_parts[0] == "openai":
329
+ # openrouter-openai-gpt-4o-mini -> GPT-4o-mini
330
+ model_parts = remaining_parts[1:]
331
+ cleaned = "-".join(model_parts)
332
+ if "gpt" in cleaned.lower():
333
+ if "4o" in cleaned:
334
+ cleaned = cleaned.replace("gpt", "GPT")
335
+ return cleaned.replace("GPT-4o", "GPT-4o")
336
+ elif "3.5" in cleaned:
337
+ cleaned = cleaned.replace("gpt", "GPT")
338
+ return cleaned.replace("GPT-3.5", "GPT-3.5")
339
+ elif "4" in cleaned:
340
+ return cleaned.replace("gpt", "GPT")
341
+ return cleaned
342
+
343
+ elif remaining_parts[0] == "anthropic":
344
+ # openrouter-anthropic-claude-3.5-sonnet -> Claude-3.5-Sonnet
345
+ model_parts = remaining_parts[1:]
346
+ cleaned = "-".join(model_parts)
347
+ if "claude" in cleaned.lower():
348
+ cleaned = cleaned.replace("claude", "Claude")
349
+ return cleaned.replace("sonnet", "Sonnet")
350
+ return cleaned
351
+
352
+ elif (remaining_parts[0] == "meta" and len(remaining_parts) >= 2
353
+ and remaining_parts[1] == "llama"):
354
+ # openrouter-meta-llama-llama-3.1-8b-instruct
355
+ # -> Llama-3.1-8B-Instruct
356
+ model_parts = remaining_parts[2:] # Skip meta-llama
357
+ cleaned = "-".join(model_parts)
358
+ cleaned = cleaned.replace("llama", "Llama")
359
+ cleaned = cleaned.replace("8b", "8B")
360
+ cleaned = cleaned.replace("70b", "70B")
361
+ cleaned = cleaned.replace("instruct", "Instruct")
362
+ return cleaned
363
+
364
+ elif remaining_parts[0] == "google":
365
+ # openrouter-google-gemma-2-9b-it -> Gemma-2-9B-IT
366
+ model_parts = remaining_parts[1:]
367
+ cleaned = "-".join(model_parts)
368
+ cleaned = cleaned.replace("gemma", "Gemma")
369
+ cleaned = cleaned.replace("9b", "9B")
370
+ cleaned = cleaned.replace("it", "IT")
371
+ return cleaned
372
+
373
+ elif remaining_parts[0] == "qwen":
374
+ # openrouter-qwen-qwen-2.5-72b-instruct
375
+ # -> Qwen-2.5-72B-Instruct
376
+ model_parts = remaining_parts[1:]
377
+ cleaned = "-".join(model_parts)
378
+ cleaned = cleaned.replace("qwen", "Qwen")
379
+ cleaned = cleaned.replace("72b", "72B")
380
+ cleaned = cleaned.replace("instruct", "Instruct")
381
+ return cleaned
382
+
383
+ elif remaining_parts[0] == "mistralai":
384
+ # openrouter-mistralai-mistral-7b-instruct
385
+ # -> Mistral-7B-Instruct
386
+ model_parts = remaining_parts[1:]
387
+ cleaned = "-".join(model_parts)
388
+ cleaned = cleaned.replace("mistral", "Mistral")
389
+ cleaned = cleaned.replace("7b", "7B")
390
+ cleaned = cleaned.replace("instruct", "Instruct")
391
+ return cleaned
392
+
393
+ # For other providers, just remove openrouter prefix
394
+ return "-".join(remaining_parts)
395
+
396
  # Default: just replace underscores with dashes
397
  return model_name.replace("_", "-")