dynamo_llm/protocols/openai/embeddings/
nvext.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use derive_builder::Builder;
17use serde::{Deserialize, Serialize};
18use validator::{Validate, ValidationError};
19
20pub trait NvExtProvider {
21    fn nvext(&self) -> Option<&NvExt>;
22}
23
24/// NVIDIA LLM extensions to the OpenAI API
25#[derive(Serialize, Deserialize, Builder, Validate, Debug, Clone)]
26#[validate(schema(function = "validate_nv_ext"))]
27pub struct NvExt {
28    /// Annotations
29    /// User requests triggers which result in the request issue back out-of-band information in the SSE
30    /// stream using the `event:` field.
31    #[serde(default, skip_serializing_if = "Option::is_none")]
32    #[builder(default, setter(strip_option))]
33    pub annotations: Option<Vec<String>>,
34}
35
36impl Default for NvExt {
37    fn default() -> Self {
38        NvExt::builder().build().unwrap()
39    }
40}
41
42impl NvExt {
43    pub fn builder() -> NvExtBuilder {
44        NvExtBuilder::default()
45    }
46}
47
48fn validate_nv_ext(_nv_ext: &NvExt) -> Result<(), ValidationError> {
49    Ok(())
50}
51
52impl NvExtBuilder {
53    pub fn add_annotation(&mut self, annotation: impl Into<String>) -> &mut Self {
54        self.annotations
55            .get_or_insert_with(|| Some(vec![]))
56            .as_mut()
57            .expect("stop should always be Some(Vec)")
58            .push(annotation.into());
59        self
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    // Test default builder configuration
68    #[test]
69    fn test_nv_ext_builder_default() {
70        let nv_ext = NvExt::builder().build().unwrap();
71        assert_eq!(nv_ext.annotations, None);
72    }
73}