pub struct Classifier<T> { /* private fields */ }Expand description
Generic AI classification facade.
T is the output type. It must implement serde::de::DeserializeOwned so
the raw JSON from the provider can be deserialized into it.
§Example
ⓘ
use ferro_ai::{Classifier, ClassifierConfig, AnthropicProvider};
use serde::Deserialize;
#[derive(Deserialize)]
struct Intent { category: String, confidence: f64 }
async fn classify_message(text: &str) -> ferro_ai::Error {
let provider = AnthropicProvider::from_env().unwrap();
let classifier = Classifier::<Intent>::new(
std::sync::Arc::new(provider),
ClassifierConfig::default(),
);
let schema = serde_json::json!({ /* ... */ });
let result = classifier.classify("You classify intents.", text, &schema).await?;
println!("category: {}", result.value.category);
Ok(())
}Implementations§
Source§impl<T: DeserializeOwned> Classifier<T>
impl<T: DeserializeOwned> Classifier<T>
Sourcepub fn new(
provider: Arc<dyn ClassificationProvider>,
config: ClassifierConfig,
) -> Self
pub fn new( provider: Arc<dyn ClassificationProvider>, config: ClassifierConfig, ) -> Self
Create a new classifier with the given provider and configuration.
Sourcepub async fn classify(
&self,
system_prompt: &str,
user_prompt: &str,
schema: &Value,
) -> Result<ClassificationResult<T>, Error>
pub async fn classify( &self, system_prompt: &str, user_prompt: &str, schema: &Value, ) -> Result<ClassificationResult<T>, Error>
Classify using the given prompts and JSON schema.
Retries on transient errors up to config.max_retries additional times.
Fails immediately on permanent errors (auth, bad request, schema mismatch).
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Classifier<T>
impl<T> !UnwindSafe for Classifier<T>
impl<T> Freeze for Classifier<T>
impl<T> Send for Classifier<T>where
T: Send,
impl<T> Sync for Classifier<T>where
T: Sync,
impl<T> Unpin for Classifier<T>where
T: Unpin,
impl<T> UnsafeUnpin for Classifier<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more