#[cfg(feature = "model2vec")]
use std::path::PathBuf;
use pyo3::prelude::*;
use crate::domain;
use crate::embed::Embedder;
#[cfg(feature = "model2vec")]
use super::errors::MatraError;
#[cfg(feature = "model2vec")]
#[pyclass(frozen, name = "Model2Vec", module = "matra")]
pub(super) struct PyModel2Vec {
pub(super) inner: crate::embed::model2vec::Model2Vec,
}
#[cfg(feature = "model2vec")]
#[pymethods]
impl PyModel2Vec {
#[staticmethod]
fn from_dir(dir: PathBuf) -> PyResult<Self> {
let inner = crate::embed::model2vec::Model2Vec::from_dir(dir).map_err(MatraError)?;
Ok(Self { inner })
}
#[staticmethod]
#[pyo3(signature = (dir=None))]
fn potion_base_8m(py: Python<'_>, dir: Option<PathBuf>) -> PyResult<Self> {
let cfg = crate::config::Config::from_sources(|key| std::env::var(key).ok(), None)
.map(|cfg| match dir {
Some(dir) => cfg.with_model_dir(dir),
None => cfg,
})
.map_err(MatraError)?;
let inner = crate::embed::model2vec::Model2Vec::from_config_with_notice(&cfg, |n| {
super::cli::announce(py, n);
})
.map_err(MatraError)?;
Ok(Self { inner })
}
#[staticmethod]
#[pyo3(signature = (config_file=None))]
fn from_config(py: Python<'_>, config_file: Option<PathBuf>) -> PyResult<Self> {
let cfg = match config_file {
Some(path) => crate::config::Config::from_file(path),
None => crate::config::Config::resolve(),
}
.map_err(MatraError)?;
let inner = crate::embed::model2vec::Model2Vec::from_config_with_notice(&cfg, |n| {
super::cli::announce(py, n);
})
.map_err(MatraError)?;
Ok(Self { inner })
}
#[getter]
fn model_hash(&self) -> &str {
self.inner.model_hash()
}
#[getter]
fn dimensions(&self) -> usize {
self.inner.dimensions()
}
fn identity<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
super::to_py(py, &self.inner.identity())
}
fn embed(&self, texts: Vec<String>) -> PyResult<Vec<Vec<f32>>> {
let refs: Vec<&str> = texts.iter().map(String::as_str).collect();
let out = self.inner.embed(&refs).map_err(MatraError)?;
Ok(out.into_iter().map(|e| e.0).collect())
}
}
pub(super) struct PyEmbedder {
obj: Py<PyAny>,
identity: domain::Identity,
}
impl PyEmbedder {
pub(super) fn new(obj: &Bound<'_, PyAny>) -> PyResult<Self> {
use super::errors::invalid_input;
let raw = obj.call_method0("identity").map_err(|e| {
invalid_input(
"embedder_identity_failed",
format!("calling identity() on the embedder failed: {e}"),
)
})?;
let identity = pythonize::depythonize::<domain::Identity>(&raw).map_err(|e| {
invalid_input(
"embedder_identity_shape",
format!(
"the embedder's identity() must return a dict with a `name` string \
and optional `model`, `sha256` and `license` strings: {e}"
),
)
})?;
Ok(Self {
obj: obj.clone().unbind(),
identity,
})
}
}
impl Embedder for PyEmbedder {
fn embed(&self, texts: &[&str]) -> domain::Result<Vec<domain::Embedding>> {
Python::attach(|py| {
let returned = self
.obj
.bind(py)
.call_method1("embed", (texts.to_vec(),))
.map_err(|e| domain::Error::InvalidInput {
reason: "embedder_raised",
message: format!("the embedder's embed() raised {e}"),
})?;
let rows = returned.extract::<Vec<Bound<'_, PyAny>>>().map_err(|e| {
domain::Error::InvalidInput {
reason: "embedder_return_shape",
message: format!(
"the embedder's embed() must return a sequence of vectors: {e}"
),
}
})?;
rows.iter()
.enumerate()
.map(|(i, row)| {
let values = row.extract::<Vec<f64>>().map_err(|e| {
domain::Error::InvalidInput {
reason: "embedder_return_shape",
message: format!(
"the embedder's embed() returned a vector at index {i} that is not a sequence of numbers: {e}"
),
}
})?;
Ok(domain::Embedding(
values.into_iter().map(|v| v as f32).collect(),
))
})
.collect()
})
}
fn identity(&self) -> domain::Identity {
self.identity.clone()
}
}