diff options
| author | sigoden <sigoden@gmail.com> | 2023-10-27 09:33:34 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-27 09:33:34 +0800 |
| commit | 47818159a7ca3910cdc6ed00109a331d2ac2f906 (patch) | |
| tree | 967e28746d01e71a4227e441078613f0abdbded8 /src/utils/mod.rs | |
| parent | 2fad63be6a826db1340734b98e3d507660611bf6 (diff) | |
| download | aichat-47818159a7ca3910cdc6ed00109a331d2ac2f906.tar.gz | |
refactor: streaming input text in dry-run mode (#154)
Diffstat (limited to 'src/utils/mod.rs')
| -rw-r--r-- | src/utils/mod.rs | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs index ae75969..3ae1004 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,5 +1,6 @@ mod tiktoken; +use self::tiktoken::cl100k_base; pub use self::tiktoken::{cl100k_base_singleton, count_tokens, text_to_tokens, tokens_to_text}; use arboard::Clipboard; @@ -41,3 +42,10 @@ pub fn copy(src: &str) -> Result<(), arboard::Error> { let mut clipboard = Clipboard::new()?; clipboard.set_text(src) } + +pub fn split_text(text: &str) -> Result<Vec<String>, anyhow::Error> { + let bpe = cl100k_base()?; + let tokens = bpe.encode_with_special_tokens(text); + let data: Result<Vec<String>, _> = tokens.into_iter().map(|v| bpe.decode(&[v])).collect(); + data +} |
