models.rs

  1use anyhow::anyhow;
  2use serde::{Deserialize, Serialize};
  3use strum::EnumIter;
  4
  5#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
  6#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
  7pub enum BedrockModelMode {
  8    #[default]
  9    Default,
 10    Thinking {
 11        budget_tokens: Option<u64>,
 12    },
 13}
 14
 15#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
 16#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, EnumIter)]
 17pub enum Model {
 18    // Anthropic models (already included)
 19    #[default]
 20    #[serde(rename = "claude-3-5-sonnet-v2", alias = "claude-3-5-sonnet-latest")]
 21    Claude3_5SonnetV2,
 22    #[serde(rename = "claude-3-7-sonnet", alias = "claude-3-7-sonnet-latest")]
 23    Claude3_7Sonnet,
 24    #[serde(
 25        rename = "claude-3-7-sonnet-thinking",
 26        alias = "claude-3-7-sonnet-thinking-latest"
 27    )]
 28    Claude3_7SonnetThinking,
 29    #[serde(rename = "claude-3-opus", alias = "claude-3-opus-latest")]
 30    Claude3Opus,
 31    #[serde(rename = "claude-3-sonnet", alias = "claude-3-sonnet-latest")]
 32    Claude3Sonnet,
 33    #[serde(rename = "claude-3-5-haiku", alias = "claude-3-5-haiku-latest")]
 34    Claude3_5Haiku,
 35    Claude3_5Sonnet,
 36    Claude3Haiku,
 37    // Amazon Nova Models
 38    AmazonNovaLite,
 39    AmazonNovaMicro,
 40    AmazonNovaPro,
 41    AmazonNovaPremier,
 42    // AI21 models
 43    AI21J2GrandeInstruct,
 44    AI21J2JumboInstruct,
 45    AI21J2Mid,
 46    AI21J2MidV1,
 47    AI21J2Ultra,
 48    AI21J2UltraV1_8k,
 49    AI21J2UltraV1,
 50    AI21JambaInstructV1,
 51    AI21Jamba15LargeV1,
 52    AI21Jamba15MiniV1,
 53    // Cohere models
 54    CohereCommandTextV14_4k,
 55    CohereCommandRV1,
 56    CohereCommandRPlusV1,
 57    CohereCommandLightTextV14_4k,
 58    // DeepSeek
 59    DeepSeekR1,
 60    // Meta models
 61    MetaLlama38BInstructV1,
 62    MetaLlama370BInstructV1,
 63    MetaLlama318BInstructV1_128k,
 64    MetaLlama318BInstructV1,
 65    MetaLlama3170BInstructV1_128k,
 66    MetaLlama3170BInstructV1,
 67    MetaLlama3211BInstructV1,
 68    MetaLlama3290BInstructV1,
 69    MetaLlama321BInstructV1,
 70    MetaLlama323BInstructV1,
 71    // Mistral models
 72    MistralMistral7BInstructV0,
 73    MistralMixtral8x7BInstructV0,
 74    MistralMistralLarge2402V1,
 75    MistralMistralSmall2402V1,
 76    // Writer models
 77    PalmyraWriterX5,
 78    PalmyraWriterX4,
 79    #[serde(rename = "custom")]
 80    Custom {
 81        name: String,
 82        max_tokens: usize,
 83        /// The name displayed in the UI, such as in the assistant panel model dropdown menu.
 84        display_name: Option<String>,
 85        max_output_tokens: Option<u32>,
 86        default_temperature: Option<f32>,
 87    },
 88}
 89
 90impl Model {
 91    pub fn default_fast() -> Self {
 92        Self::Claude3_5Haiku
 93    }
 94
 95    pub fn from_id(id: &str) -> anyhow::Result<Self> {
 96        if id.starts_with("claude-3-5-sonnet-v2") {
 97            Ok(Self::Claude3_5SonnetV2)
 98        } else if id.starts_with("claude-3-opus") {
 99            Ok(Self::Claude3Opus)
100        } else if id.starts_with("claude-3-sonnet") {
101            Ok(Self::Claude3Sonnet)
102        } else if id.starts_with("claude-3-5-haiku") {
103            Ok(Self::Claude3_5Haiku)
104        } else if id.starts_with("claude-3-7-sonnet") {
105            Ok(Self::Claude3_7Sonnet)
106        } else if id.starts_with("claude-3-7-sonnet-thinking") {
107            Ok(Self::Claude3_7SonnetThinking)
108        } else {
109            Err(anyhow!("invalid model id"))
110        }
111    }
112
113    pub fn id(&self) -> &str {
114        match self {
115            Model::Claude3_5SonnetV2 => "anthropic.claude-3-5-sonnet-20241022-v2:0",
116            Model::Claude3_5Sonnet => "anthropic.claude-3-5-sonnet-20240620-v1:0",
117            Model::Claude3Opus => "anthropic.claude-3-opus-20240229-v1:0",
118            Model::Claude3Sonnet => "anthropic.claude-3-sonnet-20240229-v1:0",
119            Model::Claude3Haiku => "anthropic.claude-3-haiku-20240307-v1:0",
120            Model::Claude3_5Haiku => "anthropic.claude-3-5-haiku-20241022-v1:0",
121            Model::Claude3_7Sonnet | Model::Claude3_7SonnetThinking => {
122                "anthropic.claude-3-7-sonnet-20250219-v1:0"
123            }
124            Model::AmazonNovaLite => "amazon.nova-lite-v1:0",
125            Model::AmazonNovaMicro => "amazon.nova-micro-v1:0",
126            Model::AmazonNovaPro => "amazon.nova-pro-v1:0",
127            Model::AmazonNovaPremier => "amazon.nova-premier-v1:0",
128            Model::DeepSeekR1 => "us.deepseek.r1-v1:0",
129            Model::AI21J2GrandeInstruct => "ai21.j2-grande-instruct",
130            Model::AI21J2JumboInstruct => "ai21.j2-jumbo-instruct",
131            Model::AI21J2Mid => "ai21.j2-mid",
132            Model::AI21J2MidV1 => "ai21.j2-mid-v1",
133            Model::AI21J2Ultra => "ai21.j2-ultra",
134            Model::AI21J2UltraV1_8k => "ai21.j2-ultra-v1:0:8k",
135            Model::AI21J2UltraV1 => "ai21.j2-ultra-v1",
136            Model::AI21JambaInstructV1 => "ai21.jamba-instruct-v1:0",
137            Model::AI21Jamba15LargeV1 => "ai21.jamba-1-5-large-v1:0",
138            Model::AI21Jamba15MiniV1 => "ai21.jamba-1-5-mini-v1:0",
139            Model::CohereCommandTextV14_4k => "cohere.command-text-v14:7:4k",
140            Model::CohereCommandRV1 => "cohere.command-r-v1:0",
141            Model::CohereCommandRPlusV1 => "cohere.command-r-plus-v1:0",
142            Model::CohereCommandLightTextV14_4k => "cohere.command-light-text-v14:7:4k",
143            Model::MetaLlama38BInstructV1 => "meta.llama3-8b-instruct-v1:0",
144            Model::MetaLlama370BInstructV1 => "meta.llama3-70b-instruct-v1:0",
145            Model::MetaLlama318BInstructV1_128k => "meta.llama3-1-8b-instruct-v1:0:128k",
146            Model::MetaLlama318BInstructV1 => "meta.llama3-1-8b-instruct-v1:0",
147            Model::MetaLlama3170BInstructV1_128k => "meta.llama3-1-70b-instruct-v1:0:128k",
148            Model::MetaLlama3170BInstructV1 => "meta.llama3-1-70b-instruct-v1:0",
149            Model::MetaLlama3211BInstructV1 => "meta.llama3-2-11b-instruct-v1:0",
150            Model::MetaLlama3290BInstructV1 => "meta.llama3-2-90b-instruct-v1:0",
151            Model::MetaLlama321BInstructV1 => "meta.llama3-2-1b-instruct-v1:0",
152            Model::MetaLlama323BInstructV1 => "meta.llama3-2-3b-instruct-v1:0",
153            Model::MistralMistral7BInstructV0 => "mistral.mistral-7b-instruct-v0:2",
154            Model::MistralMixtral8x7BInstructV0 => "mistral.mixtral-8x7b-instruct-v0:1",
155            Model::MistralMistralLarge2402V1 => "mistral.mistral-large-2402-v1:0",
156            Model::MistralMistralSmall2402V1 => "mistral.mistral-small-2402-v1:0",
157            Model::PalmyraWriterX4 => "writer.palmyra-x4-v1:0",
158            Model::PalmyraWriterX5 => "writer.palmyra-x5-v1:0",
159            Self::Custom { name, .. } => name,
160        }
161    }
162
163    pub fn display_name(&self) -> &str {
164        match self {
165            Self::Claude3_5SonnetV2 => "Claude 3.5 Sonnet v2",
166            Self::Claude3_5Sonnet => "Claude 3.5 Sonnet",
167            Self::Claude3Opus => "Claude 3 Opus",
168            Self::Claude3Sonnet => "Claude 3 Sonnet",
169            Self::Claude3Haiku => "Claude 3 Haiku",
170            Self::Claude3_5Haiku => "Claude 3.5 Haiku",
171            Self::Claude3_7Sonnet => "Claude 3.7 Sonnet",
172            Self::Claude3_7SonnetThinking => "Claude 3.7 Sonnet Thinking",
173            Self::AmazonNovaLite => "Amazon Nova Lite",
174            Self::AmazonNovaMicro => "Amazon Nova Micro",
175            Self::AmazonNovaPro => "Amazon Nova Pro",
176            Self::AmazonNovaPremier => "Amazon Nova Premier",
177            Self::DeepSeekR1 => "DeepSeek R1",
178            Self::AI21J2GrandeInstruct => "AI21 Jurassic2 Grande Instruct",
179            Self::AI21J2JumboInstruct => "AI21 Jurassic2 Jumbo Instruct",
180            Self::AI21J2Mid => "AI21 Jurassic2 Mid",
181            Self::AI21J2MidV1 => "AI21 Jurassic2 Mid V1",
182            Self::AI21J2Ultra => "AI21 Jurassic2 Ultra",
183            Self::AI21J2UltraV1_8k => "AI21 Jurassic2 Ultra V1 8K",
184            Self::AI21J2UltraV1 => "AI21 Jurassic2 Ultra V1",
185            Self::AI21JambaInstructV1 => "AI21 Jamba Instruct",
186            Self::AI21Jamba15LargeV1 => "AI21 Jamba 1.5 Large",
187            Self::AI21Jamba15MiniV1 => "AI21 Jamba 1.5 Mini",
188            Self::CohereCommandTextV14_4k => "Cohere Command Text V14 4K",
189            Self::CohereCommandRV1 => "Cohere Command R V1",
190            Self::CohereCommandRPlusV1 => "Cohere Command R Plus V1",
191            Self::CohereCommandLightTextV14_4k => "Cohere Command Light Text V14 4K",
192            Self::MetaLlama38BInstructV1 => "Meta Llama 3 8B Instruct V1",
193            Self::MetaLlama370BInstructV1 => "Meta Llama 3 70B Instruct V1",
194            Self::MetaLlama318BInstructV1_128k => "Meta Llama 3 1.8B Instruct V1 128K",
195            Self::MetaLlama318BInstructV1 => "Meta Llama 3 1.8B Instruct V1",
196            Self::MetaLlama3170BInstructV1_128k => "Meta Llama 3 1 70B Instruct V1 128K",
197            Self::MetaLlama3170BInstructV1 => "Meta Llama 3 1 70B Instruct V1",
198            Self::MetaLlama3211BInstructV1 => "Meta Llama 3 2 11B Instruct V1",
199            Self::MetaLlama3290BInstructV1 => "Meta Llama 3 2 90B Instruct V1",
200            Self::MetaLlama321BInstructV1 => "Meta Llama 3 2 1B Instruct V1",
201            Self::MetaLlama323BInstructV1 => "Meta Llama 3 2 3B Instruct V1",
202            Self::MistralMistral7BInstructV0 => "Mistral 7B Instruct V0",
203            Self::MistralMixtral8x7BInstructV0 => "Mistral Mixtral 8x7B Instruct V0",
204            Self::MistralMistralLarge2402V1 => "Mistral Large 2402 V1",
205            Self::MistralMistralSmall2402V1 => "Mistral Small 2402 V1",
206            Self::PalmyraWriterX5 => "Writer Palmyra X5",
207            Self::PalmyraWriterX4 => "Writer Palmyra X4",
208            Self::Custom {
209                display_name, name, ..
210            } => display_name.as_deref().unwrap_or(name),
211        }
212    }
213
214    pub fn max_token_count(&self) -> usize {
215        match self {
216            Self::Claude3_5SonnetV2
217            | Self::Claude3Opus
218            | Self::Claude3Sonnet
219            | Self::Claude3_5Haiku
220            | Self::Claude3_7Sonnet => 200_000,
221            Self::AmazonNovaPremier => 1_000_000,
222            Self::PalmyraWriterX5 => 1_000_000,
223            Self::PalmyraWriterX4 => 128_000,
224            Self::Custom { max_tokens, .. } => *max_tokens,
225            _ => 200_000,
226        }
227    }
228
229    pub fn max_output_tokens(&self) -> u32 {
230        match self {
231            Self::Claude3Opus | Self::Claude3Sonnet | Self::Claude3_5Haiku => 4_096,
232            Self::Claude3_7Sonnet | Self::Claude3_7SonnetThinking => 128_000,
233            Self::Claude3_5SonnetV2 | Self::PalmyraWriterX4 | Self::PalmyraWriterX5 => 8_192,
234            Self::Custom {
235                max_output_tokens, ..
236            } => max_output_tokens.unwrap_or(4_096),
237            _ => 4_096,
238        }
239    }
240
241    pub fn default_temperature(&self) -> f32 {
242        match self {
243            Self::Claude3_5SonnetV2
244            | Self::Claude3Opus
245            | Self::Claude3Sonnet
246            | Self::Claude3_5Haiku
247            | Self::Claude3_7Sonnet => 1.0,
248            Self::Custom {
249                default_temperature,
250                ..
251            } => default_temperature.unwrap_or(1.0),
252            _ => 1.0,
253        }
254    }
255
256    pub fn supports_tool_use(&self) -> bool {
257        match self {
258            // Anthropic Claude 3 models (all support tool use)
259            Self::Claude3Opus
260            | Self::Claude3Sonnet
261            | Self::Claude3_5Sonnet
262            | Self::Claude3_5SonnetV2
263            | Self::Claude3_7Sonnet
264            | Self::Claude3_7SonnetThinking
265            | Self::Claude3_5Haiku => true,
266
267            // Amazon Nova models (all support tool use)
268            Self::AmazonNovaPremier
269            | Self::AmazonNovaPro
270            | Self::AmazonNovaLite
271            | Self::AmazonNovaMicro => true,
272
273            // AI21 Jamba 1.5 models support tool use
274            Self::AI21Jamba15LargeV1 | Self::AI21Jamba15MiniV1 => true,
275
276            // Cohere Command R models support tool use
277            Self::CohereCommandRV1 | Self::CohereCommandRPlusV1 => true,
278
279            // All other models don't support tool use
280            // Including Meta Llama 3.2, AI21 Jurassic, and others
281            _ => false,
282        }
283    }
284
285    pub fn mode(&self) -> BedrockModelMode {
286        match self {
287            Model::Claude3_7SonnetThinking => BedrockModelMode::Thinking {
288                budget_tokens: Some(4096),
289            },
290            _ => BedrockModelMode::Default,
291        }
292    }
293
294    pub fn cross_region_inference_id(&self, region: &str) -> Result<String, anyhow::Error> {
295        let region_group = if region.starts_with("us-gov-") {
296            "us-gov"
297        } else if region.starts_with("us-") {
298            "us"
299        } else if region.starts_with("eu-") {
300            "eu"
301        } else if region.starts_with("ap-") || region == "me-central-1" || region == "me-south-1" {
302            "apac"
303        } else if region.starts_with("ca-") || region.starts_with("sa-") {
304            // Canada and South America regions - default to US profiles
305            "us"
306        } else {
307            // Unknown region
308            return Err(anyhow!("Unsupported Region"));
309        };
310
311        let model_id = self.id();
312
313        match (self, region_group) {
314            // Custom models can't have CRI IDs
315            (Model::Custom { .. }, _) => Ok(self.id().into()),
316
317            // Models with US Gov only
318            (Model::Claude3_5Sonnet, "us-gov") | (Model::Claude3Haiku, "us-gov") => {
319                Ok(format!("{}.{}", region_group, model_id))
320            }
321
322            // Models available only in US
323            (Model::Claude3Opus, "us")
324            | (Model::Claude3_5Sonnet, "us")
325            | (Model::Claude3_7Sonnet, "us")
326            | (Model::Claude3_7SonnetThinking, "us")
327            | (Model::AmazonNovaPremier, "us") => Ok(format!("{}.{}", region_group, model_id)),
328
329            // Models available in US, EU, and APAC
330            (Model::Claude3_5SonnetV2, "us")
331            | (Model::Claude3_5SonnetV2, "apac")
332            | (Model::Claude3_5Sonnet, _)
333            | (Model::Claude3Haiku, _)
334            | (Model::Claude3Sonnet, _)
335            | (Model::AmazonNovaLite, _)
336            | (Model::AmazonNovaMicro, _)
337            | (Model::AmazonNovaPro, _) => Ok(format!("{}.{}", region_group, model_id)),
338
339            // Models with limited EU availability
340            (Model::MetaLlama321BInstructV1, "us")
341            | (Model::MetaLlama321BInstructV1, "eu")
342            | (Model::MetaLlama323BInstructV1, "us")
343            | (Model::MetaLlama323BInstructV1, "eu") => {
344                Ok(format!("{}.{}", region_group, model_id))
345            }
346
347            // US-only models (all remaining Meta models)
348            (Model::MetaLlama38BInstructV1, "us")
349            | (Model::MetaLlama370BInstructV1, "us")
350            | (Model::MetaLlama318BInstructV1, "us")
351            | (Model::MetaLlama318BInstructV1_128k, "us")
352            | (Model::MetaLlama3170BInstructV1, "us")
353            | (Model::MetaLlama3170BInstructV1_128k, "us")
354            | (Model::MetaLlama3211BInstructV1, "us")
355            | (Model::MetaLlama3290BInstructV1, "us") => {
356                Ok(format!("{}.{}", region_group, model_id))
357            }
358
359            // Writer models only available in the US
360            (Model::PalmyraWriterX4, "us") | (Model::PalmyraWriterX5, "us") => {
361                // They have some goofiness
362                Ok(format!("{}.{}", region_group, model_id))
363            }
364
365            // Any other combination is not supported
366            _ => Ok(self.id().into()),
367        }
368    }
369}
370
371#[cfg(test)]
372mod tests {
373    use super::*;
374
375    #[test]
376    fn test_us_region_inference_ids() -> anyhow::Result<()> {
377        // Test US regions
378        assert_eq!(
379            Model::Claude3_5SonnetV2.cross_region_inference_id("us-east-1")?,
380            "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
381        );
382        assert_eq!(
383            Model::Claude3_5SonnetV2.cross_region_inference_id("us-west-2")?,
384            "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
385        );
386        assert_eq!(
387            Model::AmazonNovaPro.cross_region_inference_id("us-east-2")?,
388            "us.amazon.nova-pro-v1:0"
389        );
390        Ok(())
391    }
392
393    #[test]
394    fn test_eu_region_inference_ids() -> anyhow::Result<()> {
395        // Test European regions
396        assert_eq!(
397            Model::Claude3Sonnet.cross_region_inference_id("eu-west-1")?,
398            "eu.anthropic.claude-3-sonnet-20240229-v1:0"
399        );
400        assert_eq!(
401            Model::AmazonNovaMicro.cross_region_inference_id("eu-north-1")?,
402            "eu.amazon.nova-micro-v1:0"
403        );
404        Ok(())
405    }
406
407    #[test]
408    fn test_apac_region_inference_ids() -> anyhow::Result<()> {
409        // Test Asia-Pacific regions
410        assert_eq!(
411            Model::Claude3_5SonnetV2.cross_region_inference_id("ap-northeast-1")?,
412            "apac.anthropic.claude-3-5-sonnet-20241022-v2:0"
413        );
414        assert_eq!(
415            Model::AmazonNovaLite.cross_region_inference_id("ap-south-1")?,
416            "apac.amazon.nova-lite-v1:0"
417        );
418        Ok(())
419    }
420
421    #[test]
422    fn test_gov_region_inference_ids() -> anyhow::Result<()> {
423        // Test Government regions
424        assert_eq!(
425            Model::Claude3_5Sonnet.cross_region_inference_id("us-gov-east-1")?,
426            "us-gov.anthropic.claude-3-5-sonnet-20240620-v1:0"
427        );
428        assert_eq!(
429            Model::Claude3Haiku.cross_region_inference_id("us-gov-west-1")?,
430            "us-gov.anthropic.claude-3-haiku-20240307-v1:0"
431        );
432        Ok(())
433    }
434
435    #[test]
436    fn test_meta_models_inference_ids() -> anyhow::Result<()> {
437        // Test Meta models
438        assert_eq!(
439            Model::MetaLlama370BInstructV1.cross_region_inference_id("us-east-1")?,
440            "us.meta.llama3-70b-instruct-v1:0"
441        );
442        assert_eq!(
443            Model::MetaLlama321BInstructV1.cross_region_inference_id("eu-west-1")?,
444            "eu.meta.llama3-2-1b-instruct-v1:0"
445        );
446        Ok(())
447    }
448
449    #[test]
450    fn test_mistral_models_inference_ids() -> anyhow::Result<()> {
451        // Mistral models don't follow the regional prefix pattern,
452        // so they should return their original IDs
453        assert_eq!(
454            Model::MistralMistralLarge2402V1.cross_region_inference_id("us-east-1")?,
455            "mistral.mistral-large-2402-v1:0"
456        );
457        assert_eq!(
458            Model::MistralMixtral8x7BInstructV0.cross_region_inference_id("eu-west-1")?,
459            "mistral.mixtral-8x7b-instruct-v0:1"
460        );
461        Ok(())
462    }
463
464    #[test]
465    fn test_ai21_models_inference_ids() -> anyhow::Result<()> {
466        // AI21 models don't follow the regional prefix pattern,
467        // so they should return their original IDs
468        assert_eq!(
469            Model::AI21J2UltraV1.cross_region_inference_id("us-east-1")?,
470            "ai21.j2-ultra-v1"
471        );
472        assert_eq!(
473            Model::AI21JambaInstructV1.cross_region_inference_id("eu-west-1")?,
474            "ai21.jamba-instruct-v1:0"
475        );
476        Ok(())
477    }
478
479    #[test]
480    fn test_cohere_models_inference_ids() -> anyhow::Result<()> {
481        // Cohere models don't follow the regional prefix pattern,
482        // so they should return their original IDs
483        assert_eq!(
484            Model::CohereCommandRV1.cross_region_inference_id("us-east-1")?,
485            "cohere.command-r-v1:0"
486        );
487        assert_eq!(
488            Model::CohereCommandTextV14_4k.cross_region_inference_id("ap-southeast-1")?,
489            "cohere.command-text-v14:7:4k"
490        );
491        Ok(())
492    }
493
494    #[test]
495    fn test_custom_model_inference_ids() -> anyhow::Result<()> {
496        // Test custom models
497        let custom_model = Model::Custom {
498            name: "custom.my-model-v1:0".to_string(),
499            max_tokens: 100000,
500            display_name: Some("My Custom Model".to_string()),
501            max_output_tokens: Some(8192),
502            default_temperature: Some(0.7),
503        };
504
505        // Custom model should return its name unchanged
506        assert_eq!(
507            custom_model.cross_region_inference_id("us-east-1")?,
508            "custom.my-model-v1:0"
509        );
510
511        Ok(())
512    }
513}