diff options
| author | sigoden <sigoden@gmail.com> | 2025-02-03 22:40:13 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-03 22:40:13 +0800 |
| commit | e8aa5d12a366a1fe14192e56321d39a7c9e5d87f (patch) | |
| tree | 066263a768cb007b3c13ac0aff1b4a92d09132c3 /src/utils/mod.rs | |
| parent | 700d8a3245f1133c37e035039e511a1e5dce1a5d (diff) | |
| download | aichat-e8aa5d12a366a1fe14192e56321d39a7c9e5d87f.tar.gz | |
refactor: improve call_chat_completions function (#1143)
Diffstat (limited to 'src/utils/mod.rs')
| -rw-r--r-- | src/utils/mod.rs | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 049f361..38617a6 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -61,10 +61,6 @@ pub fn parse_bool(value: &str) -> Option<bool> { } } -pub fn strip_think_tag(text: &str) -> Cow<str> { - THINK_TAG_RE.replace_all(text, "") -} - pub fn estimate_token_length(text: &str) -> usize { let words: Vec<&str> = text.unicode_words().collect(); let mut output: f32 = 0.0; @@ -101,20 +97,16 @@ pub fn light_theme_from_colorfgbg(colorfgbg: &str) -> Option<bool> { Some(light) } -pub fn extract_block(input: &str) -> String { - let output: String = CODE_BLOCK_RE - .captures_iter(input) - .filter_map(|m| { - m.ok() - .and_then(|cap| cap.get(1)) - .map(|m| String::from(m.as_str())) - }) - .collect(); - if output.is_empty() { - input.trim().to_string() - } else { - output.trim().to_string() - } +pub fn strip_think_tag(text: &str) -> Cow<str> { + THINK_TAG_RE.replace_all(text, "") +} + +pub fn extract_code_block(text: &str) -> &str { + CODE_BLOCK_RE + .captures(text) + .ok() + .and_then(|v| v?.get(1).map(|v| v.as_str().trim())) + .unwrap_or(text) } pub fn convert_option_string(value: &str) -> Option<String> { |
