From c0397727e0b114f62d038b821e2a7e6e10743944 Mon Sep 17 00:00:00 2001 From: little-dude Date: Tue, 3 Jun 2025 17:37:08 +0200 Subject: [PATCH] language_models: Sort Ollama models by name (#31620) Hello, This is my first contribution so apologies if I'm not following the proper process (I haven't seen anything special in https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md). Also, I have tested my changes manually, but I could not figure out an easy we to instantiate a `LanguageModelSelector` in the unit tests, so I didn't write a test. If you can provide some guidance I'd be happy to write a test. --- If the user configured the models with custom names via `display_name`, we want the ollama models to be sorted based on the name that is actually displayed. ~~The original issue is only about ollama but this change will also affect the other providers.~~ Closes #30854 Release Notes: - Ollama: Changed models to be sorted by name. --- crates/language_models/src/provider/ollama.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/language_models/src/provider/ollama.rs b/crates/language_models/src/provider/ollama.rs index ed5f10da23..2f39680acd 100644 --- a/crates/language_models/src/provider/ollama.rs +++ b/crates/language_models/src/provider/ollama.rs @@ -19,7 +19,7 @@ use serde::{Deserialize, Serialize}; use settings::{Settings, SettingsStore}; use std::pin::Pin; use std::sync::atomic::{AtomicU64, Ordering}; -use std::{collections::BTreeMap, sync::Arc}; +use std::{collections::HashMap, sync::Arc}; use ui::{ButtonLike, Indicator, List, prelude::*}; use util::ResultExt; @@ -201,7 +201,7 @@ impl LanguageModelProvider for OllamaLanguageModelProvider { } fn provided_models(&self, cx: &App) -> Vec> { - let mut models: BTreeMap = BTreeMap::default(); + let mut models: HashMap = HashMap::new(); // Add models from the Ollama API for model in self.state.read(cx).available_models.iter() { @@ -228,7 +228,7 @@ impl LanguageModelProvider for OllamaLanguageModelProvider { ); } - models + let mut models = models .into_values() .map(|model| { Arc::new(OllamaLanguageModel { @@ -238,7 +238,9 @@ impl LanguageModelProvider for OllamaLanguageModelProvider { request_limiter: RateLimiter::new(4), }) as Arc }) - .collect() + .collect::>(); + models.sort_by_key(|model| model.name()); + models } fn load_model(&self, model: Arc, cx: &App) {