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}
221
222#[with_fallible_options]
223#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
224pub struct OpenAiCompatibleSettingsContent {
225    pub api_url: String,
226    pub available_models: Vec<OpenAiCompatibleAvailableModel>,
227}
228
229#[with_fallible_options]
230#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
231pub struct OpenAiCompatibleAvailableModel {
232    pub name: String,
233    pub display_name: Option<String>,
234    pub max_tokens: u64,
235    pub max_output_tokens: Option<u64>,
236    pub max_completion_tokens: Option<u64>,
237    #[serde(default)]
238    pub capabilities: OpenAiCompatibleModelCapabilities,
239}
240
241#[with_fallible_options]
242#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
243pub struct OpenAiCompatibleModelCapabilities {
244    pub tools: bool,
245    pub images: bool,
246    pub parallel_tool_calls: bool,
247    pub prompt_cache_key: bool,
248}
249
250impl Default for OpenAiCompatibleModelCapabilities {
251    fn default() -> Self {
252        Self {
253            tools: true,
254            images: false,
255            parallel_tool_calls: false,
256            prompt_cache_key: false,
257        }
258    }
259}
260
261#[with_fallible_options]
262#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
263pub struct VercelSettingsContent {
264    pub api_url: Option<String>,
265    pub available_models: Option<Vec<VercelAvailableModel>>,
266}
267
268#[with_fallible_options]
269#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
270pub struct VercelAvailableModel {
271    pub name: String,
272    pub display_name: Option<String>,
273    pub max_tokens: u64,
274    pub max_output_tokens: Option<u64>,
275    pub max_completion_tokens: Option<u64>,
276}
277
278#[with_fallible_options]
279#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
280pub struct GoogleSettingsContent {
281    pub api_url: Option<String>,
282    pub available_models: Option<Vec<GoogleAvailableModel>>,
283}
284
285#[with_fallible_options]
286#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
287pub struct GoogleAvailableModel {
288    pub name: String,
289    pub display_name: Option<String>,
290    pub max_tokens: u64,
291    pub mode: Option<ModelMode>,
292}
293
294#[with_fallible_options]
295#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
296pub struct XAiSettingsContent {
297    pub api_url: Option<String>,
298    pub available_models: Option<Vec<XaiAvailableModel>>,
299}
300
301#[with_fallible_options]
302#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
303pub struct XaiAvailableModel {
304    pub name: String,
305    pub display_name: Option<String>,
306    pub max_tokens: u64,
307    pub max_output_tokens: Option<u64>,
308    pub max_completion_tokens: Option<u64>,
309    pub supports_images: Option<bool>,
310    pub supports_tools: Option<bool>,
311    pub parallel_tool_calls: Option<bool>,
312}
313
314#[with_fallible_options]
315#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
316pub struct ZedDotDevSettingsContent {
317    pub available_models: Option<Vec<ZedDotDevAvailableModel>>,
318}
319
320#[with_fallible_options]
321#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
322pub struct ZedDotDevAvailableModel {
323    /// The provider of the language model.
324    pub provider: ZedDotDevAvailableProvider,
325    /// The model's name in the provider's API. e.g. claude-3-5-sonnet-20240620
326    pub name: String,
327    /// The name displayed in the UI, such as in the assistant panel model dropdown menu.
328    pub display_name: Option<String>,
329    /// The size of the context window, indicating the maximum number of tokens the model can process.
330    pub max_tokens: usize,
331    /// The maximum number of output tokens allowed by the model.
332    pub max_output_tokens: Option<u64>,
333    /// The maximum number of completion tokens allowed by the model (o1-* only)
334    pub max_completion_tokens: Option<u64>,
335    /// Override this model with a different Anthropic model for tool calls.
336    pub tool_override: Option<String>,
337    /// Indicates whether this custom model supports caching.
338    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
339    /// The default temperature to use for this model.
340    #[serde(serialize_with = "crate::serialize_optional_f32_with_two_decimal_places")]
341    pub default_temperature: Option<f32>,
342    /// Any extra beta headers to provide when using the model.
343    #[serde(default)]
344    pub extra_beta_headers: Vec<String>,
345    /// The model's mode (e.g. thinking)
346    pub mode: Option<ModelMode>,
347}
348
349#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
350#[serde(rename_all = "lowercase")]
351pub enum ZedDotDevAvailableProvider {
352    Anthropic,
353    OpenAi,
354    Google,
355}
356
357#[with_fallible_options]
358#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema, MergeFrom)]
359pub struct OpenRouterSettingsContent {
360    pub api_url: Option<String>,
361    pub available_models: Option<Vec<OpenRouterAvailableModel>>,
362}
363
364#[with_fallible_options]
365#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
366pub struct OpenRouterAvailableModel {
367    pub name: String,
368    pub display_name: Option<String>,
369    pub max_tokens: u64,
370    pub max_output_tokens: Option<u64>,
371    pub max_completion_tokens: Option<u64>,
372    pub supports_tools: Option<bool>,
373    pub supports_images: Option<bool>,
374    pub mode: Option<ModelMode>,
375    pub provider: Option<OpenRouterProvider>,
376}
377
378#[with_fallible_options]
379#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
380pub struct OpenRouterProvider {
381    order: Option<Vec<String>>,
382    #[serde(default = "default_true")]
383    allow_fallbacks: bool,
384    #[serde(default)]
385    require_parameters: bool,
386    #[serde(default)]
387    data_collection: DataCollection,
388    only: Option<Vec<String>>,
389    ignore: Option<Vec<String>>,
390    quantizations: Option<Vec<String>>,
391    sort: Option<String>,
392}
393
394#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
395#[serde(rename_all = "lowercase")]
396pub enum DataCollection {
397    #[default]
398    Allow,
399    Disallow,
400}
401
402fn default_true() -> bool {
403    true
404}
405
406/// Configuration for caching language model messages.
407#[with_fallible_options]
408#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, MergeFrom)]
409pub struct LanguageModelCacheConfiguration {
410    pub max_cache_anchors: usize,
411    pub should_speculate: bool,
412    pub min_total_token: u64,
413}
414
415#[derive(
416    Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema, MergeFrom,
417)]
418#[serde(tag = "type", rename_all = "lowercase")]
419pub enum ModelMode {
420    #[default]
421    Default,
422    Thinking {
423        /// The maximum number of tokens to use for reasoning. Must be lower than the model's `max_output_tokens`.
424        budget_tokens: Option<u32>,
425    },
426}