post_cortex_embeddings/embeddings/backend.rs
1// Copyright (c) 2025 Julius ML
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12
13//! Pluggable embedding backend trait.
14//!
15//! [`LocalEmbeddingEngine`](super::engine::LocalEmbeddingEngine) wraps any
16//! `EmbeddingBackend` and adds adaptive batching, concurrency control, timeouts,
17//! and performance tracking around the backend's raw `process_batch` call.
18//!
19//! Drop-in replacements (e.g. `model2vec-rs` static embeddings) implement this
20//! trait and become available to all callers without touching the engine.
21
22use anyhow::Result;
23use async_trait::async_trait;
24
25/// A backend capable of producing embedding vectors for a batch of texts.
26#[async_trait]
27pub trait EmbeddingBackend: Send + Sync {
28 /// Dimension of the produced embedding vectors.
29 fn embedding_dimension(&self) -> usize;
30
31 /// Whether this backend runs a BERT-style model. BERT backends are routed
32 /// through the engine's concurrency controller, timeout, and adaptive
33 /// batching machinery; non-BERT backends are invoked directly.
34 fn is_bert_based(&self) -> bool;
35
36 /// Process a batch of texts and return one embedding vector per text.
37 /// The engine slices large inputs into chunks no larger than the engine's
38 /// `current_batch_size` before calling this method.
39 async fn process_batch(&self, texts: Vec<String>) -> Result<Vec<Vec<f32>>>;
40}