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_7Sonnet, "us")
325            | (Model::Claude3_7SonnetThinking, "us")
326            | (Model::AmazonNovaPremier, "us") => Ok(format!("{}.{}", region_group, model_id)),
327
328            // Models available in US, EU, and APAC
329            (Model::Claude3_5SonnetV2, "us")
330            | (Model::Claude3_5SonnetV2, "apac")
331            | (Model::Claude3_5Sonnet, _)
332            | (Model::Claude3Haiku, _)
333            | (Model::Claude3Sonnet, _)
334            | (Model::AmazonNovaLite, _)
335            | (Model::AmazonNovaMicro, _)
336            | (Model::AmazonNovaPro, _) => Ok(format!("{}.{}", region_group, model_id)),
337
338            // Models with limited EU availability
339            (Model::MetaLlama321BInstructV1, "us")
340            | (Model::MetaLlama321BInstructV1, "eu")
341            | (Model::MetaLlama323BInstructV1, "us")
342            | (Model::MetaLlama323BInstructV1, "eu") => {
343                Ok(format!("{}.{}", region_group, model_id))
344            }
345
346            // US-only models (all remaining Meta models)
347            (Model::MetaLlama38BInstructV1, "us")
348            | (Model::MetaLlama370BInstructV1, "us")
349            | (Model::MetaLlama318BInstructV1, "us")
350            | (Model::MetaLlama318BInstructV1_128k, "us")
351            | (Model::MetaLlama3170BInstructV1, "us")
352            | (Model::MetaLlama3170BInstructV1_128k, "us")
353            | (Model::MetaLlama3211BInstructV1, "us")
354            | (Model::MetaLlama3290BInstructV1, "us") => {
355                Ok(format!("{}.{}", region_group, model_id))
356            }
357
358            // Writer models only available in the US
359            (Model::PalmyraWriterX4, "us") | (Model::PalmyraWriterX5, "us") => {
360                // They have some goofiness
361                Ok(format!("{}.{}", region_group, model_id))
362            }
363
364            // Any other combination is not supported
365            _ => Ok(self.id().into()),
366        }
367    }
368}
369
370#[cfg(test)]
371mod tests {
372    use super::*;
373
374    #[test]
375    fn test_us_region_inference_ids() -> anyhow::Result<()> {
376        // Test US regions
377        assert_eq!(
378            Model::Claude3_5SonnetV2.cross_region_inference_id("us-east-1")?,
379            "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
380        );
381        assert_eq!(
382            Model::Claude3_5SonnetV2.cross_region_inference_id("us-west-2")?,
383            "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
384        );
385        assert_eq!(
386            Model::AmazonNovaPro.cross_region_inference_id("us-east-2")?,
387            "us.amazon.nova-pro-v1:0"
388        );
389        Ok(())
390    }
391
392    #[test]
393    fn test_eu_region_inference_ids() -> anyhow::Result<()> {
394        // Test European regions
395        assert_eq!(
396            Model::Claude3Sonnet.cross_region_inference_id("eu-west-1")?,
397            "eu.anthropic.claude-3-sonnet-20240229-v1:0"
398        );
399        assert_eq!(
400            Model::AmazonNovaMicro.cross_region_inference_id("eu-north-1")?,
401            "eu.amazon.nova-micro-v1:0"
402        );
403        Ok(())
404    }
405
406    #[test]
407    fn test_apac_region_inference_ids() -> anyhow::Result<()> {
408        // Test Asia-Pacific regions
409        assert_eq!(
410            Model::Claude3_5SonnetV2.cross_region_inference_id("ap-northeast-1")?,
411            "apac.anthropic.claude-3-5-sonnet-20241022-v2:0"
412        );
413        assert_eq!(
414            Model::AmazonNovaLite.cross_region_inference_id("ap-south-1")?,
415            "apac.amazon.nova-lite-v1:0"
416        );
417        Ok(())
418    }
419
420    #[test]
421    fn test_gov_region_inference_ids() -> anyhow::Result<()> {
422        // Test Government regions
423        assert_eq!(
424            Model::Claude3_5Sonnet.cross_region_inference_id("us-gov-east-1")?,
425            "us-gov.anthropic.claude-3-5-sonnet-20240620-v1:0"
426        );
427        assert_eq!(
428            Model::Claude3Haiku.cross_region_inference_id("us-gov-west-1")?,
429            "us-gov.anthropic.claude-3-haiku-20240307-v1:0"
430        );
431        Ok(())
432    }
433
434    #[test]
435    fn test_meta_models_inference_ids() -> anyhow::Result<()> {
436        // Test Meta models
437        assert_eq!(
438            Model::MetaLlama370BInstructV1.cross_region_inference_id("us-east-1")?,
439            "us.meta.llama3-70b-instruct-v1:0"
440        );
441        assert_eq!(
442            Model::MetaLlama321BInstructV1.cross_region_inference_id("eu-west-1")?,
443            "eu.meta.llama3-2-1b-instruct-v1:0"
444        );
445        Ok(())
446    }
447
448    #[test]
449    fn test_mistral_models_inference_ids() -> anyhow::Result<()> {
450        // Mistral models don't follow the regional prefix pattern,
451        // so they should return their original IDs
452        assert_eq!(
453            Model::MistralMistralLarge2402V1.cross_region_inference_id("us-east-1")?,
454            "mistral.mistral-large-2402-v1:0"
455        );
456        assert_eq!(
457            Model::MistralMixtral8x7BInstructV0.cross_region_inference_id("eu-west-1")?,
458            "mistral.mixtral-8x7b-instruct-v0:1"
459        );
460        Ok(())
461    }
462
463    #[test]
464    fn test_ai21_models_inference_ids() -> anyhow::Result<()> {
465        // AI21 models don't follow the regional prefix pattern,
466        // so they should return their original IDs
467        assert_eq!(
468            Model::AI21J2UltraV1.cross_region_inference_id("us-east-1")?,
469            "ai21.j2-ultra-v1"
470        );
471        assert_eq!(
472            Model::AI21JambaInstructV1.cross_region_inference_id("eu-west-1")?,
473            "ai21.jamba-instruct-v1:0"
474        );
475        Ok(())
476    }
477
478    #[test]
479    fn test_cohere_models_inference_ids() -> anyhow::Result<()> {
480        // Cohere models don't follow the regional prefix pattern,
481        // so they should return their original IDs
482        assert_eq!(
483            Model::CohereCommandRV1.cross_region_inference_id("us-east-1")?,
484            "cohere.command-r-v1:0"
485        );
486        assert_eq!(
487            Model::CohereCommandTextV14_4k.cross_region_inference_id("ap-southeast-1")?,
488            "cohere.command-text-v14:7:4k"
489        );
490        Ok(())
491    }
492
493    #[test]
494    fn test_custom_model_inference_ids() -> anyhow::Result<()> {
495        // Test custom models
496        let custom_model = Model::Custom {
497            name: "custom.my-model-v1:0".to_string(),
498            max_tokens: 100000,
499            display_name: Some("My Custom Model".to_string()),
500            max_output_tokens: Some(8192),
501            default_temperature: Some(0.7),
502        };
503
504        // Custom model should return its name unchanged
505        assert_eq!(
506            custom_model.cross_region_inference_id("us-east-1")?,
507            "custom.my-model-v1:0"
508        );
509
510        Ok(())
511    }
512}