Skip to main content

lm_studio_api_extended/embedding/
input.rs

1// The Input struct represents a single input message or text 
2// for embedding.
3// It is used to encapsulate the content that will be processed 
4// by the embedding API.
5use crate::prelude::*;
6
7// A message or text to be embedded
8#[derive(Debug, Clone, From, Deserialize)]
9pub enum EmbeddingInput {
10    Sentence(String),
11    Sentences(Vec<String>)
12}
13
14/*
15The reason for making it an enum is to easily support 
16different input types now and allow for easy future extension.
17*/
18
19impl From<&str> for EmbeddingInput {
20    fn from(value: &str) -> Self {
21        EmbeddingInput::Sentence(value.to_string())
22    }
23}
24
25impl From<String> for EmbeddingInput {
26    fn from(value: String) -> Self {
27        EmbeddingInput::Sentence(value)
28    }
29}
30
31impl From<Vec<String>> for EmbeddingInput {
32    fn from(value: Vec<String>) -> Self {
33        EmbeddingInput::Sentences(value)       
34    }
35}
36
37impl From<Vec<&str>> for EmbeddingInput {
38    fn from(value: Vec<&str>) -> Self {
39        let converted = value.into_iter()
40            .map(|val| val.to_string())
41            .collect::<Vec<String>>();
42
43        EmbeddingInput::Sentences(converted)
44    }
45}
46
47impl From<&[&str]> for EmbeddingInput {
48    fn from(value: &[&str]) -> Self {
49        let converted = value
50            .iter()
51            .map(|val| val.to_string())
52            .collect::<Vec<String>>();
53
54        EmbeddingInput::Sentences(converted)
55    }
56}
57
58impl Serialize for EmbeddingInput {
59    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
60        where
61            S: serde::Serializer {
62        
63        match self {
64            EmbeddingInput::Sentence(text) => serializer.serialize_str(text),
65            EmbeddingInput::Sentences(vec) => vec.serialize(serializer)
66        }
67    }
68}