Skip to main content

aiway_model_protocol/
embedding.rs

1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3use crate::shared::Usage;
4
5#[derive(Serialize, Deserialize, Debug, Default, Builder, Clone, PartialEq)]
6#[builder(name = "EmbeddingParametersBuilder")]
7#[builder(setter(into, strip_option), default)]
8pub struct EmbeddingParameters {
9    /// Input text to embed, encoded as a string or array of tokens.
10    /// To embed multiple inputs in a single request, pass an array of strings or array of token arrays.
11    pub input: EmbeddingInput,
12    /// ID of the model to use.
13    pub model: String,
14    /// The format to return the embeddings in. Can be either float or base64.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub encoding_format: Option<EmbeddingEncodingFormat>,
17    /// The number of dimensions the resulting output embeddings should have. Only supported in 'text-embedding-3' and later models.
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub dimensions: Option<u32>,
20    /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub user: Option<String>,
23}
24
25#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
26pub struct EmbeddingResponse {
27    /// The object type, which is always "embedding".
28    pub object: String,
29    /// A list of embedding objects.
30    pub data: Vec<Embedding>,
31    /// The model used to generate the embeddings.
32    pub model: String,
33    /// Object containing usage information for the request.
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub usage: Option<Usage>,
36}
37
38#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
39#[serde(untagged)]
40pub enum EmbeddingInput {
41    /// The string that will be turned into an embedding.
42    String(String),
43    /// The array of strings that will be turned into an embedding.
44    StringArray(Vec<String>),
45    /// The array of integers that will be turned into an embedding.
46    IntegerArray(Vec<u32>),
47    /// The array of arrays containing integers that will be turned into an embedding.
48    IntegerArrayArray(Vec<Vec<u32>>),
49}
50
51#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
52#[serde(untagged)]
53pub enum EmbeddingOutput {
54    Float(Vec<f64>),
55    Base64(String),
56}
57
58#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
59pub struct Embedding {
60    /// The index of the embedding in the list of embeddings.
61    pub index: u32,
62    /// The embedding vector, which is a list of floats. Or String when encoding format is set to 'base64'.
63    pub embedding: EmbeddingOutput,
64    /// The object type, which is always "embedding".
65    pub object: String,
66}
67
68#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
69#[serde(rename_all = "snake_case")]
70pub enum EmbeddingEncodingFormat {
71    Float,
72    Base64,
73}
74
75impl Default for EmbeddingInput {
76    fn default() -> Self {
77        EmbeddingInput::String(String::new())
78    }
79}