diff options
| author | sigoden <sigoden@gmail.com> | 2024-04-29 15:34:24 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-29 15:34:24 +0800 |
| commit | c752ba9b272ddf8186f7786ac32a88a410e6eee8 (patch) | |
| tree | cf3058037e13eb2226e5041ddcf3a7f0c5f44a42 /src/utils/mod.rs | |
| parent | 3a00fb283e4f503901ce198ead65aebc46ae1875 (diff) | |
| download | aichat-c752ba9b272ddf8186f7786ac32a88a410e6eee8.tar.gz | |
feat: `.model` repl completions show max tokens and price (#462)
Diffstat (limited to 'src/utils/mod.rs')
| -rw-r--r-- | src/utils/mod.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 38cf766..771baf3 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -166,6 +166,33 @@ pub fn extract_block(input: &str) -> String { } } +pub fn format_option_value<T>(value: &Option<T>) -> String +where + T: std::fmt::Display, +{ + match value { + Some(value) => value.to_string(), + None => "-".to_string(), + } +} + +pub fn fuzzy_match(text: &str, pattern: &str) -> bool { + let text_chars: Vec<char> = text.chars().collect(); + let pattern_chars: Vec<char> = pattern.chars().collect(); + + let mut pattern_index = 0; + let mut text_index = 0; + + while pattern_index < pattern_chars.len() && text_index < text_chars.len() { + if pattern_chars[pattern_index] == text_chars[text_index] { + pattern_index += 1; + } + text_index += 1; + } + + pattern_index == pattern_chars.len() +} + #[cfg(test)] mod tests { use super::*; @@ -180,4 +207,11 @@ mod tests { fn test_count_tokens() { assert_eq!(count_tokens("😊 hello world"), 4); } + + #[test] + fn test_fuzzy_match() { + assert!(fuzzy_match("openai:gpt-4-turbo", "gpt4")); + assert!(fuzzy_match("openai:gpt-4-turbo", "oai4")); + assert!(!fuzzy_match("openai:gpt-4-turbo", "4gpt")); + } } |
