pub enum Embedding {
OneDim(Vec<f32>),
TwoDim(Vec<Vec<f32>>),
ThreeDim(Vec<Vec<Vec<f32>>>),
}Expand description
Embedding vector that supports multiple dimensionalities.
The OpenAI API typically returns 1D vectors, but this enum provides
flexibility for future API changes or custom use cases. The #[serde(untagged)]
attribute allows automatic deserialization based on the JSON structure.
§Variants
OneDim- Standard 1D embedding vector (e.g.,[0.1, 0.2, 0.3])TwoDim- 2D embedding matrix (e.g.,[[0.1, 0.2], [0.3, 0.4]])ThreeDim- 3D embedding tensor
§Examples
use openai_tools::embedding::response::Embedding;
// Parse a 1D embedding
let json = r#"[0.1, 0.2, 0.3]"#;
let embedding: Embedding = serde_json::from_str(json).unwrap();
assert!(embedding.is_1d());
let vector = embedding.as_1d().unwrap();
assert_eq!(vector.len(), 3);Variants§
OneDim(Vec<f32>)
1D embedding: Vec
TwoDim(Vec<Vec<f32>>)
2D embedding: Vec<Vec
ThreeDim(Vec<Vec<Vec<f32>>>)
3D embedding: Vec<Vec<Vec
Implementations§
Source§impl Embedding
impl Embedding
Sourcepub fn as_1d(&self) -> Option<&Vec<f32>>
pub fn as_1d(&self) -> Option<&Vec<f32>>
Returns the embedding as a 1D vector if it is 1D, otherwise returns None.
§Returns
Some(&Vec<f32>)- Reference to the 1D vector if the embedding is 1DNone- If the embedding is 2D or 3D
§Example
use openai_tools::embedding::response::Embedding;
let embedding: Embedding = serde_json::from_str("[0.1, 0.2, 0.3]").unwrap();
if let Some(vec) = embedding.as_1d() {
println!("Dimension: {}", vec.len());
}Sourcepub fn as_2d(&self) -> Option<&Vec<Vec<f32>>>
pub fn as_2d(&self) -> Option<&Vec<Vec<f32>>>
Returns the embedding as a 2D vector if it is 2D, otherwise returns None.
§Returns
Some(&Vec<Vec<f32>>)- Reference to the 2D vector if the embedding is 2DNone- If the embedding is 1D or 3D
Sourcepub fn as_3d(&self) -> Option<&Vec<Vec<Vec<f32>>>>
pub fn as_3d(&self) -> Option<&Vec<Vec<Vec<f32>>>>
Returns the embedding as a 3D vector if it is 3D, otherwise returns None.
§Returns
Some(&Vec<Vec<Vec<f32>>>)- Reference to the 3D vector if the embedding is 3DNone- If the embedding is 1D or 2D
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Embedding
impl<'de> Deserialize<'de> for Embedding
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Embedding
impl RefUnwindSafe for Embedding
impl Send for Embedding
impl Sync for Embedding
impl Unpin for Embedding
impl UnwindSafe for Embedding
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more