Skip to main content

outfox_openai/spec/embeddings/
embedding.rs

1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::error::OpenAIError;
5
6#[derive(Debug, Serialize, Clone, PartialEq, Deserialize)]
7#[serde(untagged)]
8pub enum EmbeddingInput {
9    String(String),
10    StringArray(Vec<String>),
11    // Minimum value is 0, maximum value is 100257 (inclusive).
12    IntegerArray(Vec<u32>),
13    ArrayOfIntegerArray(Vec<Vec<u32>>),
14}
15
16#[derive(Debug, Serialize, Default, Clone, PartialEq, Deserialize)]
17#[serde(rename_all = "lowercase")]
18pub enum EncodingFormat {
19    #[default]
20    Float,
21    Base64,
22}
23
24#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq, Deserialize)]
25#[builder(name = "CreateEmbeddingRequestArgs")]
26#[builder(pattern = "mutable")]
27#[builder(setter(into, strip_option), default)]
28#[builder(derive(Debug))]
29#[builder(build_fn(error = "OpenAIError"))]
30pub struct CreateEmbeddingRequest {
31    /// ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list)
32    /// API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models)
33    /// for descriptions of them.
34    pub model: String,
35
36    /// Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a
37    /// single request, pass an array of strings or array of token arrays. The input must not
38    /// exceed the max input tokens for the model (8192 tokens for all embedding models),
39    /// cannot be an empty string, and any array must be 2048 dimensions or less. [Example
40    /// Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens.
41    /// In addition to the per-input token limit, all embedding  models enforce a maximum of
42    /// 300,000 tokens summed across all inputs in a  single request.
43    pub input: EmbeddingInput,
44
45    /// The format to return the embeddings in. Can be either `float` or [`base64`](https://pypi.org/project/pybase64/).
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub encoding_format: Option<EncodingFormat>,
48
49    /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect
50    /// abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub user: Option<String>,
53
54    /// The number of dimensions the resulting output embeddings should have. Only supported in
55    /// `text-embedding-3` and later models.
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub dimensions: Option<u32>,
58}
59
60/// Represents an embedding vector returned by embedding endpoint.
61#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
62pub struct Embedding {
63    /// The index of the embedding in the list of embeddings.
64    pub index: u32,
65    /// The object type, which is always "embedding".
66    pub object: String,
67    /// The embedding vector, which is a list of floats. The length of vector
68    /// depends on the model as listed in the [embedding guide](https://platform.openai.com/docs/guides/embeddings).
69    pub embedding: Vec<f32>,
70}
71
72#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
73pub struct Base64EmbeddingVector(pub String);
74
75/// Represents an base64-encoded embedding vector returned by embedding endpoint.
76#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
77pub struct Base64Embedding {
78    /// The index of the embedding in the list of embeddings.
79    pub index: u32,
80    /// The object type, which is always "embedding".
81    pub object: String,
82    /// The embedding vector, encoded in base64.
83    pub embedding: Base64EmbeddingVector,
84}
85
86#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
87pub struct EmbeddingUsage {
88    /// The number of tokens used by the prompt.
89    pub prompt_tokens: u32,
90    /// The total number of tokens used by the request.
91    pub total_tokens: u32,
92}
93
94#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)]
95pub struct CreateEmbeddingResponse {
96    pub object: String,
97    /// The name of the model used to generate the embedding.
98    pub model: String,
99    /// The list of embeddings generated by the model.
100    pub data: Vec<Embedding>,
101    /// The usage information for the request.
102    pub usage: EmbeddingUsage,
103}
104
105#[derive(Debug, Deserialize, Clone, PartialEq, Serialize)]
106pub struct CreateBase64EmbeddingResponse {
107    pub object: String,
108    /// The name of the model used to generate the embedding.
109    pub model: String,
110    /// The list of embeddings generated by the model.
111    pub data: Vec<Base64Embedding>,
112    /// The usage information for the request.
113    pub usage: EmbeddingUsage,
114}