alith_knowledge/
string.rs1use alith_core::{
2 chunking::{ChunkError, Chunker, chunk_text},
3 knowledge::{Knowledge, KnowledgeError},
4};
5use anyhow::Result;
6
7#[derive(Debug, Default)]
8pub struct StringKnowledge {
9 content: String,
10}
11
12impl StringKnowledge {
13 pub fn new(content: impl ToString) -> Self {
14 Self {
15 content: content.to_string(),
16 }
17 }
18}
19
20impl Chunker for StringKnowledge {
21 fn chunk(&self) -> std::result::Result<Vec<String>, ChunkError> {
22 Ok(chunk_text(
23 &self.content,
24 self.chunk_size() as u32,
25 self.overlap_percent(),
26 )
27 .map_err(|err| ChunkError::Normal(err.to_string()))?
28 .unwrap_or_default())
29 }
30}
31
32impl Knowledge for StringKnowledge {
33 fn load(&self) -> Result<String, KnowledgeError> {
34 Ok(self.content.clone())
35 }
36
37 fn enrich(&self, _input: &str) -> Result<String, KnowledgeError> {
38 Ok(self.content.clone())
39 }
40}