summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2025-02-03 22:35:01 +0800
committerGitHub <noreply@github.com>2025-02-03 22:35:01 +0800
commit700d8a3245f1133c37e035039e511a1e5dce1a5d (patch)
tree7b1b2cf2e816adb0af399c9ca34051d3fcbef47f /src/utils
parent07e58aaacd8c7f486a0d2a7c8d35a195f27d487e (diff)
downloadaichat-700d8a3245f1133c37e035039e511a1e5dce1a5d.tar.gz
feat: strip reasoning contents (#1141)
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/mod.rs6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 9dde751..049f361 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -26,11 +26,13 @@ use anyhow::{Context, Result};
use fancy_regex::Regex;
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use is_terminal::IsTerminal;
+use std::borrow::Cow;
use std::{env, path::PathBuf, process};
use unicode_segmentation::UnicodeSegmentation;
lazy_static::lazy_static! {
pub static ref CODE_BLOCK_RE: Regex = Regex::new(r"(?ms)```\w*(.*)```").unwrap();
+ pub static ref THINK_TAG_RE: Regex = Regex::new(r"(?s)^\s*<think>.*?</think>(\s*|$)").unwrap();
pub static ref IS_STDOUT_TERMINAL: bool = std::io::stdout().is_terminal();
pub static ref NO_COLOR: bool = env::var("NO_COLOR").ok().and_then(|v| parse_bool(&v)).unwrap_or_default() || !*IS_STDOUT_TERMINAL;
}
@@ -59,6 +61,10 @@ 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;