pub struct Bge { /* private fields */ }
Implementations§
source§impl Bge
impl Bge
sourcepub fn from_files<P>(tokenizer_file_path: P, model_file_path: P) -> Result<Self>
pub fn from_files<P>(tokenizer_file_path: P, model_file_path: P) -> Result<Self>
Creates a new instance of Bge
by loading a tokenizer and a model from the specified file paths.
§Arguments
tokenizer_file_path
- A path to the file containing the tokenizer configuration.model_file_path
- A path to the ONNX model file.
§Returns
If successful, returns an Ok(Self)
containing a new instance of Bge
. On failure, returns an Err(anyhow::Error)
detailing the error encountered during the loading process.
§Errors
This function can fail if:
- The paths provided do not point to valid files.
- The tokenizer or model file cannot be correctly parsed or loaded, possibly due to format issues or compatibility problems.
§Examples
let bge = bge::Bge::from_files("path/to/tokenizer.json", "path/to/model.onnx");
match bge {
Ok(instance) => println!("Bge instance created successfully."),
Err(e) => eprintln!("Failed to create Bge instance: {}", e),
}
sourcepub fn create_embeddings(&self, input: &str) -> Result<[f32; 384], BgeError>
pub fn create_embeddings(&self, input: &str) -> Result<[f32; 384], BgeError>
Generates embeddings for a given input text using the model.
This method tokenizes the input text, performs necessary preprocessing, and then runs the model to produce embeddings. The embeddings are normalized before being returned.
§Arguments
input
- The text input for which embeddings should be generated.
§Returns
If successful, returns a Result
containing a fixed-size array of f32
elements representing
the generated embeddings. On failure, returns a BgeError
detailing the nature of the error.
§Errors
This method can return an error in several cases:
BgeError::LargeInput
if the input text produces more tokens than the model can accept.BgeError::OnnxRuntimeError
for errors related to running the ONNX model.BgeError::Other
for all other errors, including issues with tokenization or tensor extraction.
§Examples
let embeddings = bge.create_embeddings("This is a sample text.");
match embeddings {
Ok(embeds) => println!("Embeddings: {:?}", embeds),
Err(e) => eprintln!("Error generating embeddings: {}", e),
}