llm_toolkit/extract/
core.rs1use super::error::ParseError;
2use serde::{Deserialize, Serialize};
3
4pub trait ResponseParser<T> {
6    fn parse(&self, content: &str) -> Result<T, ParseError>;
8
9    fn extract_content(&self, text: &str) -> String;
11
12    fn fallback_parse(&self, content: &str, error: &ParseError) -> Result<T, ParseError>;
14}
15
16pub trait ContentExtractor {
18    fn extract_tagged(&self, text: &str, tag: &str) -> Option<String>;
20
21    fn extract_json_like(&self, text: &str) -> Option<String>;
23
24    fn extract_pattern(&self, text: &str, pattern: &str) -> Option<String>;
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
30pub enum ExtractionStrategy {
31    TaggedContent(String),
33
34    JsonBrackets,
36
37    FirstJsonObject,
39
40    KeywordSearch(Vec<String>),
42
43    RegexPattern(String),
45
46    OriginalText,
48}
49
50#[derive(Debug, Clone)]
52pub struct ParsingConfig {
53    pub primary_tag: String,
55
56    pub extraction_strategies: Vec<ExtractionStrategy>,
58
59    pub debug_mode: bool,
61
62    pub max_content_length: Option<usize>,
64}
65
66impl Default for ParsingConfig {
67    fn default() -> Self {
68        Self {
69            primary_tag: "answer".to_string(),
70            extraction_strategies: vec![
71                ExtractionStrategy::TaggedContent("answer".to_string()),
72                ExtractionStrategy::JsonBrackets,
73                ExtractionStrategy::OriginalText,
74            ],
75            debug_mode: false,
76            max_content_length: Some(50_000), }
78    }
79}
80
81impl ParsingConfig {
82    pub fn with_tag(tag: &str) -> Self {
84        Self {
85            primary_tag: tag.to_string(),
86            extraction_strategies: vec![ExtractionStrategy::TaggedContent(tag.to_string())],
87            ..Default::default()
88        }
89    }
90
91    pub fn add_strategy(mut self, strategy: ExtractionStrategy) -> Self {
93        self.extraction_strategies.push(strategy);
94        self
95    }
96
97    pub fn with_debug(mut self) -> Self {
99        self.debug_mode = true;
100        self
101    }
102}