-
Notifications
You must be signed in to change notification settings - Fork 1
/
claude.rs
107 lines (96 loc) · 3 KB
/
claude.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use core::fmt;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ClaudeModel {
#[serde(rename = "claude-3-5-sonnet-20240620")]
Claude35Sonnet,
#[serde(rename = "claude-3-opus-20240229")]
Claude3Opus,
#[serde(rename = "claude-3-sonnet-20240229")]
Claude3Sonnet,
#[serde(rename = "claude-3-haiku-20240307")]
Claude3Haiku,
}
impl ClaudeModel {
pub fn as_str(&self) -> &'static str {
match self {
Self::Claude35Sonnet => "claude-3-5-sonnet-20240620",
Self::Claude3Opus => "claude-3-opus-20240229",
Self::Claude3Sonnet => "claude-3-sonnet-20240229",
Self::Claude3Haiku => "claude-3-haiku-20240307",
}
}
}
impl Default for ClaudeModel {
fn default() -> Self {
Self::Claude35Sonnet
}
}
impl FromStr for ClaudeModel {
type Err = crate::error::AnthropicError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"claude-3-5-sonnet-20240620" => Ok(Self::Claude35Sonnet),
"claude-3-opus-20240229" => Ok(Self::Claude3Opus),
"claude-3-sonnet-20240229" => Ok(Self::Claude3Sonnet),
"claude-3-haiku-20240307" => Ok(Self::Claude3Haiku),
_ => Err(crate::error::AnthropicError::ModelNotSupported(
s.to_string(),
)),
}
}
}
impl fmt::Display for ClaudeModel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[cfg(test)]
mod tests {
use crate::error::AnthropicError;
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn should_serialize_to_correct_model_names() {
assert_eq!(
ClaudeModel::Claude35Sonnet.as_str(),
"claude-3-5-sonnet-20240620",
);
assert_eq!(ClaudeModel::Claude3Opus.as_str(), "claude-3-opus-20240229");
assert_eq!(
ClaudeModel::Claude3Sonnet.as_str(),
"claude-3-sonnet-20240229"
);
assert_eq!(
ClaudeModel::Claude3Haiku.as_str(),
"claude-3-haiku-20240307"
);
}
#[test]
fn should_deserialize_to_correct_models() {
assert_eq!(
ClaudeModel::Claude35Sonnet,
ClaudeModel::from_str("claude-3-5-sonnet-20240620").unwrap(),
);
assert_eq!(
ClaudeModel::Claude3Opus,
ClaudeModel::from_str("claude-3-opus-20240229").unwrap(),
);
assert_eq!(
ClaudeModel::Claude3Sonnet,
ClaudeModel::from_str("claude-3-sonnet-20240229").unwrap(),
);
assert_eq!(
ClaudeModel::Claude3Haiku,
ClaudeModel::from_str("claude-3-haiku-20240307").unwrap(),
);
}
#[test]
fn should_return_error_for_invalid_model() {
assert!(matches!(
ClaudeModel::from_str("claude-invalid-model"),
Err(AnthropicError::ModelNotSupported(_))
));
}
}