pub trait Embed {
// Required method
fn embed(&self, embedder: &mut TextEmbedder) -> Result<(), EmbedError>;
}
Expand description
Derive this trait for objects that need to be converted to vector embeddings. The Embed::embed method accumulates string values that need to be embedded by adding them to the TextEmbedder. If an error occurs, the method should return EmbedError.
§Example
use std::env;
use serde::{Deserialize, Serialize};
use rig::{Embed, embeddings::{TextEmbedder, EmbedError}};
struct WordDefinition {
id: String,
word: String,
definitions: String,
}
impl Embed for WordDefinition {
fn embed(&self, embedder: &mut TextEmbedder) -> Result<(), EmbedError> {
// Embeddings only need to be generated for `definition` field.
// Split the definitions by comma and collect them into a vector of strings.
// That way, different embeddings can be generated for each definition in the `definitions` string.
self.definitions
.split(",")
.for_each(|s| {
embedder.embed(s.to_string());
});
Ok(())
}
}
let fake_definition = WordDefinition {
id: "1".to_string(),
word: "apple".to_string(),
definitions: "a fruit, a tech company".to_string(),
};
assert_eq!(embeddings::to_texts(fake_definition).unwrap(), vec!["a fruit", " a tech company"]);