dynamo_llm/protocols/openai/
embeddings.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use dynamo_runtime::protocols::annotated::AnnotationsProvider;
5use serde::{Deserialize, Serialize};
6use validator::Validate;
7
8mod aggregator;
9mod nvext;
10
11pub use aggregator::DeltaAggregator;
12pub use nvext::{NvExt, NvExtProvider};
13
14#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
15pub struct NvCreateEmbeddingRequest {
16    #[serde(flatten)]
17    pub inner: dynamo_async_openai::types::CreateEmbeddingRequest,
18
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub nvext: Option<NvExt>,
21}
22
23/// A response structure for unary chat completion responses, embedding OpenAI's
24/// `CreateChatCompletionResponse`.
25///
26/// # Fields
27/// - `inner`: The base OpenAI unary chat completion response, embedded
28///   using `serde(flatten)`.
29#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
30pub struct NvCreateEmbeddingResponse {
31    #[serde(flatten)]
32    pub inner: dynamo_async_openai::types::CreateEmbeddingResponse,
33}
34
35impl NvCreateEmbeddingResponse {
36    pub fn empty() -> Self {
37        Self {
38            inner: dynamo_async_openai::types::CreateEmbeddingResponse {
39                object: "list".to_string(),
40                model: "embedding".to_string(),
41                data: vec![],
42                usage: dynamo_async_openai::types::EmbeddingUsage {
43                    prompt_tokens: 0,
44                    total_tokens: 0,
45                },
46            },
47        }
48    }
49}
50
51/// Implements `NvExtProvider` for `NvCreateEmbeddingRequest`,
52/// providing access to NVIDIA-specific extensions.
53impl NvExtProvider for NvCreateEmbeddingRequest {
54    /// Returns a reference to the optional `NvExt` extension, if available.
55    fn nvext(&self) -> Option<&NvExt> {
56        self.nvext.as_ref()
57    }
58}
59
60/// Implements `AnnotationsProvider` for `NvCreateEmbeddingRequest`,
61/// enabling retrieval and management of request annotations.
62impl AnnotationsProvider for NvCreateEmbeddingRequest {
63    /// Retrieves the list of annotations from `NvExt`, if present.
64    fn annotations(&self) -> Option<Vec<String>> {
65        self.nvext
66            .as_ref()
67            .and_then(|nvext| nvext.annotations.clone())
68    }
69
70    /// Checks whether a specific annotation exists in the request.
71    ///
72    /// # Arguments
73    /// * `annotation` - A string slice representing the annotation to check.
74    ///
75    /// # Returns
76    /// `true` if the annotation exists, `false` otherwise.
77    fn has_annotation(&self, annotation: &str) -> bool {
78        self.nvext
79            .as_ref()
80            .and_then(|nvext| nvext.annotations.as_ref())
81            .map(|annotations| annotations.contains(&annotation.to_string()))
82            .unwrap_or(false)
83    }
84}