gemini_adk_fluent_rs/live/
extraction.rs1use std::future::Future;
4use std::sync::Arc;
5
6use serde::Serialize;
7use serde::de::DeserializeOwned;
8
9use gemini_adk_rs::live::extractor::{ExtractionTrigger, LlmExtractor, TurnExtractor};
10use gemini_adk_rs::llm::BaseLlm;
11
12use super::Live;
13
14impl Live {
15 pub fn extract_turns<T>(self, llm: Arc<dyn BaseLlm>, prompt: impl Into<String>) -> Self
27 where
28 T: DeserializeOwned + Serialize + schemars::JsonSchema + Send + Sync + 'static,
29 {
30 self.extract_turns_windowed::<T>(llm, prompt, 3)
31 }
32
33 pub fn extract_record(mut self, spec: gemini_adk_rs::extract::Extract) -> Self {
39 self.config = self.config.input_transcription(true);
40 self.extractors.push(spec.into_extractor());
41 self
42 }
43
44 pub fn extract_turns_windowed<T>(
46 mut self,
47 llm: Arc<dyn BaseLlm>,
48 prompt: impl Into<String>,
49 window_size: usize,
50 ) -> Self
51 where
52 T: DeserializeOwned + Serialize + schemars::JsonSchema + Send + Sync + 'static,
53 {
54 self.config = self
56 .config
57 .input_transcription(true)
58 .output_transcription(true);
59
60 let name = std::any::type_name::<T>()
62 .rsplit("::")
63 .next()
64 .unwrap_or("Extraction")
65 .to_string();
66
67 let schema = gemini_adk_rs::tool::wire_schema::<T>();
68
69 self.warm_up_llms.push(llm.clone());
71
72 let extractor = LlmExtractor::new(name, llm, prompt, window_size)
73 .with_schema(schema)
74 .with_min_words(3);
75 self.extractors.push(Arc::new(extractor));
76 self
77 }
78
79 pub fn extract_turns_triggered<T>(
85 mut self,
86 llm: Arc<dyn BaseLlm>,
87 prompt: impl Into<String>,
88 window_size: usize,
89 trigger: ExtractionTrigger,
90 ) -> Self
91 where
92 T: DeserializeOwned + Serialize + schemars::JsonSchema + Send + Sync + 'static,
93 {
94 self.config = self
96 .config
97 .input_transcription(true)
98 .output_transcription(true);
99
100 let name = std::any::type_name::<T>()
101 .rsplit("::")
102 .next()
103 .unwrap_or("Extraction")
104 .to_string();
105
106 let schema = gemini_adk_rs::tool::wire_schema::<T>();
107
108 self.warm_up_llms.push(llm.clone());
109
110 let extractor = LlmExtractor::new(name, llm, prompt, window_size)
111 .with_schema(schema)
112 .with_min_words(3)
113 .with_trigger(trigger);
114 self.extractors.push(Arc::new(extractor));
115 self
116 }
117
118 pub fn extract_turns_configured<T>(
125 mut self,
126 llm: Arc<dyn BaseLlm>,
127 prompt: impl Into<String>,
128 window_size: usize,
129 trigger: ExtractionTrigger,
130 configure: impl FnOnce(LlmExtractor) -> LlmExtractor,
131 ) -> Self
132 where
133 T: DeserializeOwned + Serialize + schemars::JsonSchema + Send + Sync + 'static,
134 {
135 self.config = self
136 .config
137 .input_transcription(true)
138 .output_transcription(true);
139
140 let name = std::any::type_name::<T>()
141 .rsplit("::")
142 .next()
143 .unwrap_or("Extraction")
144 .to_string();
145
146 let schema = gemini_adk_rs::tool::wire_schema::<T>();
147
148 self.warm_up_llms.push(llm.clone());
149
150 let extractor = LlmExtractor::new(name, llm, prompt, window_size)
151 .with_schema(schema)
152 .with_min_words(3)
153 .with_trigger(trigger);
154 self.extractors.push(Arc::new(configure(extractor)));
155 self
156 }
157
158 pub fn extract_json(
171 mut self,
172 llm: Arc<dyn BaseLlm>,
173 name: impl Into<String>,
174 schema: serde_json::Value,
175 prompt: impl Into<String>,
176 ) -> Self {
177 self.config = self
178 .config
179 .input_transcription(true)
180 .output_transcription(true);
181 self.warm_up_llms.push(llm.clone());
182 let extractor = LlmExtractor::new(name.into(), llm, prompt.into(), 3)
183 .with_schema(schema)
184 .with_min_words(3);
185 self.extractors.push(Arc::new(extractor));
186 self
187 }
188
189 pub fn extractor(mut self, extractor: Arc<dyn TurnExtractor>) -> Self {
191 self.config = self
193 .config
194 .input_transcription(true)
195 .output_transcription(true);
196 self.extractors.push(extractor);
197 self
198 }
199
200 pub fn on_extracted<F, Fut>(mut self, f: F) -> Self
204 where
205 F: Fn(String, serde_json::Value) -> Fut + Send + Sync + 'static,
206 Fut: Future<Output = ()> + Send + 'static,
207 {
208 self.callbacks.on_extracted = Some(Arc::new(move |name, value| Box::pin(f(name, value))));
209 self
210 }
211
212 pub fn on_extraction_error<F, Fut>(mut self, f: F) -> Self
217 where
218 F: Fn(String, String) -> Fut + Send + Sync + 'static,
219 Fut: Future<Output = ()> + Send + 'static,
220 {
221 self.callbacks.on_extraction_error =
222 Some(Arc::new(move |name, error| Box::pin(f(name, error))));
223 self
224 }
225}