coreason_engine/embeddings.rs
1// Copyright (c) 2026 CoReason, Inc
2//
3// This software is proprietary and dual-licensed
4// Licensed under the Prosperity Public License 3.0 (the "License")
5// A copy of the license is available at <https://prosperitylicense.com/versions/3.0.0>
6// For details, see the LICENSE file
7// Commercial use beyond a 30-day trial requires a separate license
8//
9// Source Code: <https://github.com/CoReason-AI/coreason-runtime>
10
11use std::path::Path;
12
13use tract_onnx::prelude::*;
14
15/// Pure-Rust ONNX Embedding Engine utilizing Sonos Tract
16
17pub struct TractEmbeddings {
18 model: Option<std::sync::Arc<RunnableModel<TypedFact, Box<dyn TypedOp>>>>,
19}
20
21impl TractEmbeddings {
22 /// Creates a new TractEmbeddings instance
23
24 pub fn new() -> Self {
25 Self { model: None }
26 }
27
28 /// Loads and optimizes an ONNX embedding model from path
29
30 pub fn load_model(&mut self, model_path: &Path) -> Result<(), String> {
31 let onnx_model = tract_onnx::onnx()
32 .model_for_path(model_path)
33 .map_err(|e| format!("Failed to read ONNX model: {}", e))?;
34
35 let model = onnx_model
36 .into_optimized()
37 .map_err(|e| format!("Failed to optimize ONNX model: {}", e))?
38 .into_runnable()
39 .map_err(|e| format!("Failed to build runnable ONNX model: {}", e))?;
40
41 self.model = Some(model);
42
43 println!("[TRACT] ONNX embedding model loaded successfully.");
44
45 Ok(())
46 }
47
48 /// Computes a vector embedding representation for input tokens
49
50 pub fn compute_embedding(&self, _tokens: &[i64]) -> Result<Vec<f32>, String> {
51 if self.model.is_none() {
52 return Err("Model not loaded".to_string());
53 }
54
55 // In native execution:
56
57 // let input = Tensor::from_shape(&[1, _tokens.len()], _tokens)...
58
59 // let result = plan.run(tensors_in)...
60
61 Ok(vec![0.0; 384]) // Returns typical 384-dimensional vector stub
62 }
63}