Skip to main content

uni_db/api/
xervo.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2024-2026 Dragonscale Team
3
4use std::sync::Arc;
5
6use crate::api::Uni;
7use uni_common::{Result, UniError};
8use uni_xervo::runtime::ModelRuntime;
9use uni_xervo::traits::{GenerationOptions, GenerationResult};
10
11fn into_uni_error<E: std::fmt::Display>(err: E) -> UniError {
12    UniError::Internal(anyhow::anyhow!(err.to_string()))
13}
14
15/// Facade for using Uni-Xervo runtime from the Uni API surface.
16#[derive(Clone)]
17pub struct UniXervo {
18    runtime: Arc<ModelRuntime>,
19}
20
21impl UniXervo {
22    pub(crate) fn new(runtime: Arc<ModelRuntime>) -> Self {
23        Self { runtime }
24    }
25
26    /// Embed text inputs using a configured model alias.
27    pub async fn embed(&self, alias: &str, texts: &[&str]) -> Result<Vec<Vec<f32>>> {
28        let embedder = self
29            .runtime
30            .embedding(alias)
31            .await
32            .map_err(into_uni_error)?;
33        embedder.embed(texts.to_vec()).await.map_err(into_uni_error)
34    }
35
36    /// Generate text using a configured model alias.
37    pub async fn generate(
38        &self,
39        alias: &str,
40        messages: &[String],
41        options: GenerationOptions,
42    ) -> Result<GenerationResult> {
43        let generator = self
44            .runtime
45            .generator(alias)
46            .await
47            .map_err(into_uni_error)?;
48        generator
49            .generate(messages, options)
50            .await
51            .map_err(into_uni_error)
52    }
53
54    /// Access the underlying Uni-Xervo runtime.
55    pub fn raw_runtime(&self) -> &Arc<ModelRuntime> {
56        &self.runtime
57    }
58}
59
60impl Uni {
61    /// Access Uni-Xervo runtime facade configured for this database.
62    pub fn xervo(&self) -> Result<UniXervo> {
63        let runtime = self.xervo_runtime.clone().ok_or_else(|| {
64            UniError::Internal(anyhow::anyhow!("Uni-Xervo runtime is not configured"))
65        })?;
66        Ok(UniXervo::new(runtime))
67    }
68}