pub struct HsPipeline { /* private fields */ }Expand description
Main HS code classification pipeline.
§Example — direct (sync)
use hs_predict::pipeline::HsPipeline;
use hs_predict::types::{ProductDescription, SubstanceIdentifier, PhysicalForm};
let pipeline = HsPipeline::new();
let product = ProductDescription {
identifier: SubstanceIdentifier::from_cas("1310-73-2"),
physical_form: Some(PhysicalForm::Solid),
purity_pct: None,
purity_type: None,
mixture_components: None,
intended_use: None,
additional_context: None,
};
let prediction = pipeline.classify(&product).unwrap();
assert_eq!(&prediction.hs_code, "281511");§Example — with PubChem enrichment (async, pubchem feature)
use hs_predict::pipeline::HsPipeline;
use hs_predict::pubchem::PubChemClient;
use hs_predict::types::{ProductDescription, SubstanceIdentifier, PhysicalForm};
let pipeline = HsPipeline::new().with_pubchem(PubChemClient::new());
let mut product = ProductDescription {
identifier: SubstanceIdentifier::from_cas("1310-73-2"),
physical_form: Some(PhysicalForm::Solid),
purity_pct: None,
purity_type: None,
mixture_components: None,
intended_use: None,
additional_context: None,
};
pipeline.enrich(&mut product).await?; // fills SMILES, InChI, IUPAC name …
let prediction = pipeline.classify(&product)?;
println!("{}", prediction.display()); // "28.15.11"§Example — with LLM fallback (async, llm feature)
use hs_predict::pipeline::HsPipeline;
use hs_predict::llm::{LlmClassifier, LlmPrompt, LlmResponse};
use futures::future::BoxFuture;
struct MyClient;
impl LlmClassifier for MyClient {
fn classify<'a>(&'a self, prompt: &'a LlmPrompt) -> BoxFuture<'a, hs_predict::Result<LlmResponse>> {
Box::pin(async move { todo!() })
}
}
let pipeline = HsPipeline::new().with_llm(MyClient);
use hs_predict::types::{ProductDescription, SubstanceIdentifier};
let product = ProductDescription {
identifier: SubstanceIdentifier::from_cas("12-34-5"),
physical_form: None, purity_pct: None, purity_type: None,
mixture_components: None, intended_use: None, additional_context: None,
};
let prediction = pipeline.classify_with_llm(&product).await?;
println!("{}", prediction.display());Implementations§
Source§impl HsPipeline
impl HsPipeline
Sourcepub fn with_mapping(
self,
cas: impl Into<String>,
hs_code: impl Into<String>,
) -> Self
pub fn with_mapping( self, cas: impl Into<String>, hs_code: impl Into<String>, ) -> Self
Add a user-provided CAS → HS code mapping.
These mappings override the embedded rule table with confidence = 1.0.
Sourcepub fn with_config(self, config: PipelineConfig) -> Self
pub fn with_config(self, config: PipelineConfig) -> Self
Override the default pipeline configuration.
Sourcepub fn with_llm(self, client: impl LlmClassifier + 'static) -> Self
pub fn with_llm(self, client: impl LlmClassifier + 'static) -> Self
Attach an LlmClassifier implementation to
enable the LLM fallback (Priority 4).
The LLM is called by classify_with_llm when
the rule-based pipeline returns a result with
recommended_action != Accept, or returns
LowConfidenceNoLlm.
Requires the llm Cargo feature.
Sourcepub fn with_pubchem(self, client: PubChemClient) -> Self
pub fn with_pubchem(self, client: PubChemClient) -> Self
Attach a PubChemClient to enable
automatic identifier enrichment before classification.
Requires the pubchem Cargo feature.
Sourcepub async fn enrich(&self, product: &mut ProductDescription) -> Result<()>
pub async fn enrich(&self, product: &mut ProductDescription) -> Result<()>
Enrich a ProductDescription with PubChem data.
Fills in any missing fields of the main identifier and each mixture component’s identifier (SMILES, InChI, InChIKey, IUPAC name, CID).
This is a best-effort operation:
- “Not found” and “no usable identifier” results are silently ignored.
- Network / parse errors are propagated.
- If no PubChem client is configured, returns
Ok(())immediately.
Requires the pubchem Cargo feature.
Sourcepub fn classify(&self, product: &ProductDescription) -> Result<HsPrediction>
pub fn classify(&self, product: &ProductDescription) -> Result<HsPrediction>
Classify a product and return an HS code prediction.
Priority order:
- User-provided mapping
- Embedded static rule table
- (v0.3) SMILES rule engine
- (v0.4) LLM fallback
Sourcepub async fn classify_with_llm(
&self,
product: &ProductDescription,
) -> Result<HsPrediction>
pub async fn classify_with_llm( &self, product: &ProductDescription, ) -> Result<HsPrediction>
Classify a product, falling back to the configured LLM when the rule-based pipeline returns a low-confidence or uncertain result.
§Priority order (same as classify + LLM)
- User-provided mapping →
Accept→ return immediately. - Embedded static rule table →
Accept→ return immediately. - SMILES rule engine →
Accept→ return immediately. - Any result with
recommended_action != Accept, orLowConfidenceNoLlm→ forward to LLM.
If no LLM client has been configured via with_llm,
returns HsPredictError::LlmNotConfigured.
§Validation
The LLM’s hs_code must be exactly 6 ASCII digits; otherwise
HsPredictError::ValidationFailed is returned.
§Chapter consistency
If the LLM chapter differs from the SMILES engine’s chapter hint, a warning note is appended — this is not a hard error.
Requires the llm Cargo feature.