portkey_sdk/model/embeddings.rs
1use serde::{Deserialize, Serialize};
2
3/// Input for the embeddings API.
4///
5/// The input can be a single string, an array of strings, an array of token integers,
6/// or an array of token arrays.
7#[derive(Debug, Clone, Serialize, Deserialize)]
8#[serde(untagged)]
9pub enum EmbeddingInput {
10 /// A single string input
11 String(String),
12
13 /// An array of strings
14 StringArray(Vec<String>),
15
16 /// An array of token integers
17 TokenArray(Vec<i32>),
18
19 /// An array of token arrays
20 TokenArrayArray(Vec<Vec<i32>>),
21}
22
23/// The format to return embeddings in.
24#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
25#[serde(rename_all = "lowercase")]
26pub enum EncodingFormat {
27 /// Return embeddings as floating point arrays (default)
28 #[default]
29 Float,
30
31 /// Return embeddings encoded as base64 strings
32 Base64,
33}
34
35/// Request to create embeddings.
36///
37/// # Example
38///
39/// ```rust
40/// use portkey_sdk::model::{CreateEmbeddingRequest, EmbeddingInput, EncodingFormat};
41///
42/// let request = CreateEmbeddingRequest {
43/// model: "text-embedding-ada-002".to_string(),
44/// input: EmbeddingInput::String("The quick brown fox jumped over the lazy dog".to_string()),
45/// encoding_format: Some(EncodingFormat::Float),
46/// dimensions: None,
47/// user: None,
48/// };
49/// ```
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct CreateEmbeddingRequest {
52 /// ID of the model to use.
53 ///
54 /// Common models:
55 /// - `text-embedding-ada-002`
56 /// - `text-embedding-3-small`
57 /// - `text-embedding-3-large`
58 pub model: String,
59
60 /// Input text to embed.
61 ///
62 /// Can be a string, array of strings, array of tokens, or array of token arrays.
63 /// The input must not exceed the max input tokens for the model (8192 tokens for
64 /// `text-embedding-ada-002`), cannot be an empty string, and any array must be
65 /// 2048 dimensions or less.
66 pub input: EmbeddingInput,
67
68 /// The format to return the embeddings in.
69 ///
70 /// Can be either `float` or `base64`. Defaults to `float`.
71 #[serde(skip_serializing_if = "Option::is_none")]
72 pub encoding_format: Option<EncodingFormat>,
73
74 /// The number of dimensions the resulting output embeddings should have.
75 ///
76 /// Only supported in `text-embedding-3` and later models.
77 #[serde(skip_serializing_if = "Option::is_none")]
78 pub dimensions: Option<i32>,
79
80 /// A unique identifier representing your end-user.
81 ///
82 /// This can help providers monitor and detect abuse.
83 #[serde(skip_serializing_if = "Option::is_none")]
84 pub user: Option<String>,
85}
86
87impl CreateEmbeddingRequest {
88 /// Creates a new embedding request with the minimum required fields.
89 ///
90 /// # Arguments
91 ///
92 /// * `model` - The model ID to use (e.g., "text-embedding-ada-002", "text-embedding-3-small")
93 /// * `input` - The input text or texts to embed
94 ///
95 /// # Example
96 ///
97 /// ```
98 /// use portkey_sdk::model::{CreateEmbeddingRequest, EmbeddingInput};
99 ///
100 /// let request = CreateEmbeddingRequest::new(
101 /// "text-embedding-ada-002",
102 /// "Hello, world!",
103 /// );
104 /// ```
105 pub fn new(model: impl Into<String>, input: impl Into<EmbeddingInput>) -> Self {
106 Self {
107 model: model.into(),
108 input: input.into(),
109 encoding_format: None,
110 dimensions: None,
111 user: None,
112 }
113 }
114}
115
116impl From<String> for EmbeddingInput {
117 fn from(s: String) -> Self {
118 EmbeddingInput::String(s)
119 }
120}
121
122impl From<&str> for EmbeddingInput {
123 fn from(s: &str) -> Self {
124 EmbeddingInput::String(s.to_string())
125 }
126}
127
128impl From<Vec<String>> for EmbeddingInput {
129 fn from(v: Vec<String>) -> Self {
130 EmbeddingInput::StringArray(v)
131 }
132}
133
134/// An embedding vector returned by the embedding endpoint.
135///
136/// # Example
137///
138/// ```json
139/// {
140/// "object": "embedding",
141/// "embedding": [
142/// 0.0023064255,
143/// -0.009327292,
144/// ...
145/// -0.0028842222
146/// ],
147/// "index": 0
148/// }
149/// ```
150#[derive(Debug, Clone, Serialize, Deserialize)]
151pub struct Embedding {
152 /// The index of the embedding in the list of embeddings.
153 pub index: i32,
154
155 /// The object type, which is always "embedding".
156 pub object: String,
157
158 /// The embedding vector.
159 ///
160 /// This is a list of floats. The length of the vector depends on the model.
161 /// For `text-embedding-ada-002`, this will be 1536 floats.
162 pub embedding: Vec<f64>,
163}
164
165/// Usage statistics for an embeddings request.
166#[derive(Debug, Clone, Serialize, Deserialize)]
167pub struct EmbeddingUsage {
168 /// The number of tokens used by the prompt.
169 pub prompt_tokens: i32,
170
171 /// The total number of tokens used by the request.
172 pub total_tokens: i32,
173}
174
175/// Response from the embeddings API.
176///
177/// # Example
178///
179/// ```rust
180/// use portkey_sdk::model::{CreateEmbeddingResponse, Embedding, EmbeddingUsage};
181///
182/// let response = CreateEmbeddingResponse {
183/// object: "list".to_string(),
184/// model: "text-embedding-ada-002".to_string(),
185/// data: vec![
186/// Embedding {
187/// index: 0,
188/// object: "embedding".to_string(),
189/// embedding: vec![0.0023064255, -0.009327292],
190/// }
191/// ],
192/// usage: EmbeddingUsage {
193/// prompt_tokens: 8,
194/// total_tokens: 8,
195/// },
196/// };
197/// ```
198#[derive(Debug, Clone, Serialize, Deserialize)]
199pub struct CreateEmbeddingResponse {
200 /// The object type, which is always "list".
201 pub object: String,
202
203 /// The name of the model used to generate the embedding.
204 pub model: String,
205
206 /// The list of embeddings generated by the model.
207 pub data: Vec<Embedding>,
208
209 /// The usage information for the request.
210 pub usage: EmbeddingUsage,
211}