xai_openapi/
embeddings.rs

1//! Embedding API types for `/v1/embeddings` endpoint.
2
3use serde::{Deserialize, Serialize};
4
5use crate::prelude::*;
6
7use crate::usage::EmbeddingUsage;
8
9/// Embedding request body.
10#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
11pub struct EmbeddingRequest {
12    /// Input text to embed, encoded as a string or list of tokens.
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub input: Option<EmbeddingInput>,
15
16    /// ID of the model to use.
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub model: Option<String>,
19
20    /// The format to return the embeddings in. Can be either `float` or `base64`.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub encoding_format: Option<String>,
23
24    /// The number of dimensions the resulting output embeddings should have.
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub dimensions: Option<i32>,
27
28    /// Flag to use the new format of the API.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub preview: Option<bool>,
31
32    /// A unique identifier representing your end-user.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub user: Option<String>,
35}
36
37/// Input for embedding request.
38#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum EmbeddingInput {
41    /// A string to be embedded.
42    String(String),
43    /// An array of strings to be embedded.
44    StringArray(Vec<String>),
45    /// A token in integer to be embedded.
46    Ints(Vec<i32>),
47    /// An array of tokens in integers to be embedded.
48    IntsArray(Vec<Vec<i32>>),
49}
50
51impl Default for EmbeddingInput {
52    fn default() -> Self {
53        EmbeddingInput::String(String::new())
54    }
55}
56
57/// Embedding response.
58#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
59pub struct EmbeddingResponse {
60    /// The object type of `data` field, which is always `"list"`.
61    pub object: String,
62
63    /// Model ID used to create embedding.
64    pub model: String,
65
66    /// A list of embedding objects.
67    pub data: Vec<Embedding>,
68
69    /// Token usage information.
70    #[serde(skip_serializing_if = "Option::is_none")]
71    pub usage: Option<EmbeddingUsage>,
72}
73
74/// An embedding object.
75#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
76pub struct Embedding {
77    /// Index of the embedding object in the data list.
78    pub index: i32,
79
80    /// Embedding content.
81    pub embedding: EmbeddingContent,
82
83    /// The object type, which is always `"embedding"`.
84    pub object: String,
85}
86
87/// Embedding content.
88#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
89#[serde(untagged)]
90pub enum EmbeddingContent {
91    /// Embedding as an array of floats.
92    Float(Vec<f32>),
93    /// Embedding in base64 string.
94    Base64(String),
95}
96
97impl Default for EmbeddingContent {
98    fn default() -> Self {
99        EmbeddingContent::Float(Vec::new())
100    }
101}