language_model.rs

  1use collections::HashMap;
  2use schemars::JsonSchema;
  3use serde::{Deserialize, Serialize};
  4use settings_macros::{MergeFrom, with_fallible_options};
  5
  6use std::sync::Arc;
  7
  8#[with_fallible_options]
  9#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
 10pub struct AllLanguageModelSettingsContent {
 11    pub anthropic: Option<AnthropicSettingsContent>,
 12    pub bedrock: Option<AmazonBedrockSettingsContent>,
 13    pub deepseek: Option<DeepseekSettingsContent>,
 14    pub google: Option<GoogleSettingsContent>,
 15    pub lmstudio: Option<LmStudioSettingsContent>,
 16    pub mistral: Option<MistralSettingsContent>,
 17    pub ollama: Option<OllamaSettingsContent>,
 18    pub open_router: Option<OpenRouterSettingsContent>,
 19    pub openai: Option<OpenAiSettingsContent>,
 20    pub openai_compatible: Option<HashMap<Arc<str>, OpenAiCompatibleSettingsContent>>,
 21    pub vercel: Option<VercelSettingsContent>,
 22    pub x_ai: Option<XAiSettingsContent>,
 23    #[serde(rename = "zed.dev")]
 24    pub zed_dot_dev: Option<ZedDotDevSettingsContent>,
 25}
 26
 27#[with_fallible_options]
 28#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
 29pub struct AnthropicSettingsContent {
 30    pub api_url: Option<String>,
 31    pub available_models: Option<Vec<AnthropicAvailableModel>>,
 32}
 33
 34#[with_fallible_options]
 35#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
 36pub struct AnthropicAvailableModel {
 37    /// The model's name in the Anthropic API. e.g. claude-3-5-sonnet-latest, claude-3-opus-20240229, etc
 38    pub name: String,
 39    /// The model's name in Zed's UI, such as in the model selector dropdown menu in the assistant panel.
 40    pub display_name: Option<String>,
 41    /// The model's context window size.
 42    pub max_tokens: u64,
 43    /// A model `name` to substitute when calling tools, in case the primary model doesn't support tool calling.
 44    pub tool_override: Option<String>,
 45    /// Configuration of Anthropic's caching API.
 46    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
 47    pub max_output_tokens: Option<u64>,
 48    #[serde(serialize_with = "crate::serialize_optional_f32_with_two_decimal_places")]
 49    pub default_temperature: Option<f32>,
 50    #[serde(default)]
 51    pub extra_beta_headers: Vec<String>,
 52    /// The model's mode (e.g. thinking)
 53    pub mode: Option<ModelMode>,
 54}
 55
 56#[with_fallible_options]
 57#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
 58pub struct AmazonBedrockSettingsContent {
 59    pub available_models: Option<Vec<BedrockAvailableModel>>,
 60    pub endpoint_url: Option<String>,
 61    pub region: Option<String>,
 62    pub profile: Option<String>,
 63    pub authentication_method: Option<BedrockAuthMethodContent>,
 64    pub allow_global: Option<bool>,
 65}
 66
 67#[with_fallible_options]
 68#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
 69pub struct BedrockAvailableModel {
 70    pub name: String,
 71    pub display_name: Option<String>,
 72    pub max_tokens: u64,
 73    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
 74    pub max_output_tokens: Option<u64>,
 75    #[serde(serialize_with = "crate::serialize_optional_f32_with_two_decimal_places")]
 76    pub default_temperature: Option<f32>,
 77    pub mode: Option<ModelMode>,
 78}
 79
 80#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
 81pub enum BedrockAuthMethodContent {
 82    #[serde(rename = "named_profile")]
 83    NamedProfile,
 84    #[serde(rename = "sso")]
 85    SingleSignOn,
 86    #[serde(rename = "api_key")]
 87    ApiKey,
 88    /// IMDSv2, PodIdentity, env vars, etc.
 89    #[serde(rename = "default")]
 90    Automatic,
 91}
 92
 93#[with_fallible_options]
 94#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
 95pub struct OllamaSettingsContent {
 96    pub api_url: Option<String>,
 97    pub auto_discover: Option<bool>,
 98    pub available_models: Option<Vec<OllamaAvailableModel>>,
 99}
100
101#[with_fallible_options]
102#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
103pub struct OllamaAvailableModel {
104    /// The model name in the Ollama API (e.g. "llama3.2:latest")
105    pub name: String,
106    /// The model's name in Zed's UI, such as in the model selector dropdown menu in the assistant panel.
107    pub display_name: Option<String>,
108    /// The Context Length parameter to the model (aka num_ctx or n_ctx)
109    pub max_tokens: u64,
110    /// The number of seconds to keep the connection open after the last request
111    pub keep_alive: Option<KeepAlive>,
112    /// Whether the model supports tools
113    pub supports_tools: Option<bool>,
114    /// Whether the model supports vision
115    pub supports_images: Option<bool>,
116    /// Whether to enable think mode
117    pub supports_thinking: Option<bool>,
118}
119
120#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq, JsonSchema, MergeFrom)]
121#[serde(untagged)]
122pub enum KeepAlive {
123    /// Keep model alive for N seconds
124    Seconds(isize),
125    /// Keep model alive for a fixed duration. Accepts durations like "5m", "10m", "1h", "1d", etc.
126    Duration(String),
127}
128
129impl KeepAlive {
130    /// Keep model alive until a new model is loaded or until Ollama shuts down
131    pub fn indefinite() -> Self {
132        Self::Seconds(-1)
133    }
134}
135
136impl Default for KeepAlive {
137    fn default() -> Self {
138        Self::indefinite()
139    }
140}
141
142#[with_fallible_options]
143#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
144pub struct LmStudioSettingsContent {
145    pub api_url: Option<String>,
146    pub available_models: Option<Vec<LmStudioAvailableModel>>,
147}
148
149#[with_fallible_options]
150#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
151pub struct LmStudioAvailableModel {
152    pub name: String,
153    pub display_name: Option<String>,
154    pub max_tokens: u64,
155    pub supports_tool_calls: bool,
156    pub supports_images: bool,
157}
158
159#[with_fallible_options]
160#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
161pub struct DeepseekSettingsContent {
162    pub api_url: Option<String>,
163    pub available_models: Option<Vec<DeepseekAvailableModel>>,
164}
165
166#[with_fallible_options]
167#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
168pub struct DeepseekAvailableModel {
169    pub name: String,
170    pub display_name: Option<String>,
171    pub max_tokens: u64,
172    pub max_output_tokens: Option<u64>,
173}
174
175#[with_fallible_options]
176#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
177pub struct MistralSettingsContent {
178    pub api_url: Option<String>,
179    pub available_models: Option<Vec<MistralAvailableModel>>,
180}
181
182#[with_fallible_options]
183#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
184pub struct MistralAvailableModel {
185    pub name: String,
186    pub display_name: Option<String>,
187    pub max_tokens: u64,
188    pub max_output_tokens: Option<u64>,
189    pub max_completion_tokens: Option<u64>,
190    pub supports_tools: Option<bool>,
191    pub supports_images: Option<bool>,
192    pub supports_thinking: Option<bool>,
193}
194
195#[with_fallible_options]
196#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
197pub struct OpenAiSettingsContent {
198    pub api_url: Option<String>,
199    pub available_models: Option<Vec<OpenAiAvailableModel>>,
200}
201
202#[with_fallible_options]
203#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
204pub struct OpenAiAvailableModel {
205    pub name: String,
206    pub display_name: Option<String>,
207    pub max_tokens: u64,
208    pub max_output_tokens: Option<u64>,
209    pub max_completion_tokens: Option<u64>,
210    pub reasoning_effort: Option<OpenAiReasoningEffort>,
211}
212
213#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, JsonSchema, MergeFrom)]
214#[serde(rename_all = "lowercase")]
215pub enum OpenAiReasoningEffort {
216    Minimal,
217    Low,
218    Medium,
219    High,
220    Xhigh,
221}
222
223#[with_fallible_options]
224#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
225pub struct OpenAiCompatibleSettingsContent {
226    pub api_url: String,
227    pub available_models: Vec<OpenAiCompatibleAvailableModel>,
228}
229
230#[with_fallible_options]
231#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
232pub struct OpenAiCompatibleAvailableModel {
233    pub name: String,
234    pub display_name: Option<String>,
235    pub max_tokens: u64,
236    pub max_output_tokens: Option<u64>,
237    pub max_completion_tokens: Option<u64>,
238    #[serde(default)]
239    pub capabilities: OpenAiCompatibleModelCapabilities,
240}
241
242#[with_fallible_options]
243#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
244pub struct OpenAiCompatibleModelCapabilities {
245    pub tools: bool,
246    pub images: bool,
247    pub parallel_tool_calls: bool,
248    pub prompt_cache_key: bool,
249}
250
251impl Default for OpenAiCompatibleModelCapabilities {
252    fn default() -> Self {
253        Self {
254            tools: true,
255            images: false,
256            parallel_tool_calls: false,
257            prompt_cache_key: false,
258        }
259    }
260}
261
262#[with_fallible_options]
263#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
264pub struct VercelSettingsContent {
265    pub api_url: Option<String>,
266    pub available_models: Option<Vec<VercelAvailableModel>>,
267}
268
269#[with_fallible_options]
270#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
271pub struct VercelAvailableModel {
272    pub name: String,
273    pub display_name: Option<String>,
274    pub max_tokens: u64,
275    pub max_output_tokens: Option<u64>,
276    pub max_completion_tokens: Option<u64>,
277}
278
279#[with_fallible_options]
280#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
281pub struct GoogleSettingsContent {
282    pub api_url: Option<String>,
283    pub available_models: Option<Vec<GoogleAvailableModel>>,
284}
285
286#[with_fallible_options]
287#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
288pub struct GoogleAvailableModel {
289    pub name: String,
290    pub display_name: Option<String>,
291    pub max_tokens: u64,
292    pub mode: Option<ModelMode>,
293}
294
295#[with_fallible_options]
296#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
297pub struct XAiSettingsContent {
298    pub api_url: Option<String>,
299    pub available_models: Option<Vec<XaiAvailableModel>>,
300}
301
302#[with_fallible_options]
303#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
304pub struct XaiAvailableModel {
305    pub name: String,
306    pub display_name: Option<String>,
307    pub max_tokens: u64,
308    pub max_output_tokens: Option<u64>,
309    pub max_completion_tokens: Option<u64>,
310    pub supports_images: Option<bool>,
311    pub supports_tools: Option<bool>,
312    pub parallel_tool_calls: Option<bool>,
313}
314
315#[with_fallible_options]
316#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
317pub struct ZedDotDevSettingsContent {
318    pub available_models: Option<Vec<ZedDotDevAvailableModel>>,
319}
320
321#[with_fallible_options]
322#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
323pub struct ZedDotDevAvailableModel {
324    /// The provider of the language model.
325    pub provider: ZedDotDevAvailableProvider,
326    /// The model's name in the provider's API. e.g. claude-3-5-sonnet-20240620
327    pub name: String,
328    /// The name displayed in the UI, such as in the assistant panel model dropdown menu.
329    pub display_name: Option<String>,
330    /// The size of the context window, indicating the maximum number of tokens the model can process.
331    pub max_tokens: usize,
332    /// The maximum number of output tokens allowed by the model.
333    pub max_output_tokens: Option<u64>,
334    /// The maximum number of completion tokens allowed by the model (o1-* only)
335    pub max_completion_tokens: Option<u64>,
336    /// Override this model with a different Anthropic model for tool calls.
337    pub tool_override: Option<String>,
338    /// Indicates whether this custom model supports caching.
339    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
340    /// The default temperature to use for this model.
341    #[serde(serialize_with = "crate::serialize_optional_f32_with_two_decimal_places")]
342    pub default_temperature: Option<f32>,
343    /// Any extra beta headers to provide when using the model.
344    #[serde(default)]
345    pub extra_beta_headers: Vec<String>,
346    /// The model's mode (e.g. thinking)
347    pub mode: Option<ModelMode>,
348}
349
350#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
351#[serde(rename_all = "lowercase")]
352pub enum ZedDotDevAvailableProvider {
353    Anthropic,
354    OpenAi,
355    Google,
356}
357
358#[with_fallible_options]
359#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
360pub struct OpenRouterSettingsContent {
361    pub api_url: Option<String>,
362    pub available_models: Option<Vec<OpenRouterAvailableModel>>,
363}
364
365#[with_fallible_options]
366#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
367pub struct OpenRouterAvailableModel {
368    pub name: String,
369    pub display_name: Option<String>,
370    pub max_tokens: u64,
371    pub max_output_tokens: Option<u64>,
372    pub max_completion_tokens: Option<u64>,
373    pub supports_tools: Option<bool>,
374    pub supports_images: Option<bool>,
375    pub mode: Option<ModelMode>,
376    pub provider: Option<OpenRouterProvider>,
377}
378
379#[with_fallible_options]
380#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
381pub struct OpenRouterProvider {
382    order: Option<Vec<String>>,
383    #[serde(default = "default_true")]
384    allow_fallbacks: bool,
385    #[serde(default)]
386    require_parameters: bool,
387    #[serde(default)]
388    data_collection: DataCollection,
389    only: Option<Vec<String>>,
390    ignore: Option<Vec<String>>,
391    quantizations: Option<Vec<String>>,
392    sort: Option<String>,
393}
394
395#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
396#[serde(rename_all = "lowercase")]
397pub enum DataCollection {
398    #[default]
399    Allow,
400    Disallow,
401}
402
403fn default_true() -> bool {
404    true
405}
406
407/// Configuration for caching language model messages.
408#[with_fallible_options]
409#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
410pub struct LanguageModelCacheConfiguration {
411    pub max_cache_anchors: usize,
412    pub should_speculate: bool,
413    pub min_total_token: u64,
414}
415
416#[derive(
417    Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema, MergeFrom,
418)]
419#[serde(tag = "type", rename_all = "lowercase")]
420pub enum ModelMode {
421    #[default]
422    Default,
423    Thinking {
424        /// The maximum number of tokens to use for reasoning. Must be lower than the model's `max_output_tokens`.
425        budget_tokens: Option<u32>,
426    },
427}