openai_rst/embedding.rs
1//! This module defines the structures and methods for handling text embeddings.
2//! It includes:
3//! - `EmbeddingData`: Struct representing the data of an embedding.
4//! - `EmbeddingRequest`: Struct for creating a request to generate embeddings.
5//! - `EmbeddingResponse`: Struct for the response from an embedding request.
6//! - `Usage`: Struct for tracking token usage in embedding operations.
7//! - `impl_builder_methods!`: Macro for generating builder methods for structs.
8
9use crate::{impl_builder_methods, models::Model};
10use serde::{Deserialize, Serialize};
11use std::{collections::HashMap, option::Option};
12
13/// Represents the data of an embedding.
14#[derive(Debug, Deserialize, Serialize)]
15pub struct EmbeddingData {
16 /// Object type, typically "embedding".
17 pub object: String,
18 /// Vector representing the embedding.
19 pub embedding: Vec<f32>,
20 /// Index of the embedding.
21 pub index: i32,
22}
23
24/// Represents a request to generate embeddings.
25#[derive(Debug, Serialize, Clone)]
26pub struct EmbeddingRequest {
27 /// Model to be used for generating embeddings.
28 pub model: Model,
29 /// Input text for which embeddings are to be generated.
30 pub input: String,
31 /// Optional dimensions of the embedding.
32 #[serde(skip_serializing_if = "Option::is_none")]
33 pub dimensions: Option<i32>,
34 /// Optional user identifier.
35 #[serde(skip_serializing_if = "Option::is_none")]
36 pub user: Option<String>,
37}
38
39impl EmbeddingRequest {
40 /// Creates a new `EmbeddingRequest` with the specified model and input text.
41 pub fn new(model: Model, input: String) -> Self {
42 Self {
43 model,
44 input,
45 dimensions: None,
46 user: None,
47 }
48 }
49}
50
51impl_builder_methods!(
52 EmbeddingRequest,
53 user: String
54);
55
56/// Represents the response from an embedding request.
57#[derive(Debug, Deserialize, Serialize)]
58pub struct EmbeddingResponse {
59 /// Object type, typically "list".
60 pub object: String,
61 /// List of embedding data.
62 pub data: Vec<EmbeddingData>,
63 /// Model used for generating embeddings.
64 pub model: Model,
65 /// Usage information for the embedding request.
66 pub usage: Usage,
67 /// Optional headers from the response.
68 pub headers: Option<HashMap<String, String>>,
69}
70
71/// Represents token usage in embedding operations.
72#[derive(Debug, Deserialize, Serialize)]
73pub struct Usage {
74 /// Number of tokens used in the prompt.
75 pub prompt_tokens: i32,
76 /// Total number of tokens used.
77 pub total_tokens: i32,
78}