chat_completions.rs 12.3 KB
Newer Older
1
2
3
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

4
5
6
7
use dynamo_runtime::protocols::annotated::AnnotationsProvider;
use serde::{Deserialize, Serialize};
use validator::Validate;

8
9
10
use crate::engines::ValidateRequest;

use super::{
11
    OpenAIOutputOptionsProvider, OpenAISamplingOptionsProvider, OpenAIStopConditionsProvider,
12
13
14
    common_ext::{
        CommonExt, CommonExtProvider, choose_with_deprecation, emit_nvext_deprecation_warning,
    },
15
16
    nvext::NvExt,
    nvext::NvExtProvider,
17
    validate,
18
};
19

20
pub mod aggregator;
21
22
mod delta;

Paul Hendricks's avatar
Paul Hendricks committed
23
pub use aggregator::DeltaAggregator;
24
25
pub use delta::DeltaGenerator;

26
/// A request structure for creating a chat completion, extending OpenAI's
27
/// `CreateChatCompletionRequest` with [`NvExt`] extensions and common fields.
28
29
30
///
/// # Fields
/// - `inner`: The base OpenAI chat completion request, embedded using `serde(flatten)`.
31
32
33
/// - `common`: Common extension fields (ignore_eos, min_tokens) at root level, embedded using `serde(flatten)`.
/// - `nvext`: The optional NVIDIA extension field. See [`NvExt`] for more details.
///   Note: If ignore_eos is specified in both common and nvext, the common (root-level) value takes precedence.
Paul Hendricks's avatar
Paul Hendricks committed
34
#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
35
pub struct NvCreateChatCompletionRequest {
Paul Hendricks's avatar
Paul Hendricks committed
36
    #[serde(flatten)]
37
    pub inner: dynamo_async_openai::types::CreateChatCompletionRequest,
38

39
40
41
    #[serde(flatten, default)]
    pub common: CommonExt,

42
    #[serde(skip_serializing_if = "Option::is_none")]
43
    pub nvext: Option<NvExt>,
44
45
46
47

    /// Extra args to pass to the chat template rendering context
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub chat_template_args: Option<std::collections::HashMap<String, serde_json::Value>>,
48
49
}

50
51
52
53
54
55
/// A response structure for unary chat completion responses, embedding OpenAI's
/// `CreateChatCompletionResponse`.
///
/// # Fields
/// - `inner`: The base OpenAI unary chat completion response, embedded
///   using `serde(flatten)`.
56
pub type NvCreateChatCompletionResponse = dynamo_async_openai::types::CreateChatCompletionResponse;
57

58
59
60
61
62
63
/// A response structure for streamed chat completions, embedding OpenAI's
/// `CreateChatCompletionStreamResponse`.
///
/// # Fields
/// - `inner`: The base OpenAI streaming chat completion response, embedded
///   using `serde(flatten)`.
64
65
pub type NvCreateChatCompletionStreamResponse =
    dynamo_async_openai::types::CreateChatCompletionStreamResponse;
66

67
68
/// Implements `NvExtProvider` for `NvCreateChatCompletionRequest`,
/// providing access to NVIDIA-specific extensions.
69
impl NvExtProvider for NvCreateChatCompletionRequest {
70
    /// Returns a reference to the optional `NvExt` extension, if available.
71
72
73
74
    fn nvext(&self) -> Option<&NvExt> {
        self.nvext.as_ref()
    }

75
    /// Returns `None`, as raw prompt extraction is not implemented.
76
77
78
79
80
    fn raw_prompt(&self) -> Option<String> {
        None
    }
}

81
82
/// Implements `AnnotationsProvider` for `NvCreateChatCompletionRequest`,
/// enabling retrieval and management of request annotations.
83
impl AnnotationsProvider for NvCreateChatCompletionRequest {
84
    /// Retrieves the list of annotations from `NvExt`, if present.
Biswa Panda's avatar
Biswa Panda committed
85
86
87
88
89
90
    fn annotations(&self) -> Option<Vec<String>> {
        self.nvext
            .as_ref()
            .and_then(|nvext| nvext.annotations.clone())
    }

91
92
93
94
95
96
97
    /// Checks whether a specific annotation exists in the request.
    ///
    /// # Arguments
    /// * `annotation` - A string slice representing the annotation to check.
    ///
    /// # Returns
    /// `true` if the annotation exists, `false` otherwise.
Biswa Panda's avatar
Biswa Panda committed
98
99
100
101
102
103
104
105
    fn has_annotation(&self, annotation: &str) -> bool {
        self.nvext
            .as_ref()
            .and_then(|nvext| nvext.annotations.as_ref())
            .map(|annotations| annotations.contains(&annotation.to_string()))
            .unwrap_or(false)
    }
}
106

107
108
/// Implements `OpenAISamplingOptionsProvider` for `NvCreateChatCompletionRequest`,
/// exposing OpenAI's sampling parameters for chat completion.
109
impl OpenAISamplingOptionsProvider for NvCreateChatCompletionRequest {
110
    /// Retrieves the temperature parameter for sampling, if set.
111
    fn get_temperature(&self) -> Option<f32> {
Paul Hendricks's avatar
Paul Hendricks committed
112
        self.inner.temperature
113
114
    }

115
    /// Retrieves the top-p (nucleus sampling) parameter, if set.
116
    fn get_top_p(&self) -> Option<f32> {
Paul Hendricks's avatar
Paul Hendricks committed
117
        self.inner.top_p
118
119
    }

120
    /// Retrieves the frequency penalty parameter, if set.
121
    fn get_frequency_penalty(&self) -> Option<f32> {
Paul Hendricks's avatar
Paul Hendricks committed
122
        self.inner.frequency_penalty
123
124
    }

125
    /// Retrieves the presence penalty parameter, if set.
126
    fn get_presence_penalty(&self) -> Option<f32> {
Paul Hendricks's avatar
Paul Hendricks committed
127
        self.inner.presence_penalty
128
129
    }

130
    /// Returns a reference to the optional `NvExt` extension, if available.
131
132
133
    fn nvext(&self) -> Option<&NvExt> {
        self.nvext.as_ref()
    }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    /// Retrieves the seed value for random number generation, if set.
    fn get_seed(&self) -> Option<i64> {
        self.inner.seed
    }

    /// Retrieves the number of completions to generate for each prompt, if set.
    fn get_n(&self) -> Option<u8> {
        self.inner.n
    }

    /// Retrieves the best_of parameter, if set.
    fn get_best_of(&self) -> Option<u8> {
        None // Not supported in chat completions
    }
148
149
}

150
151
152
153
154
155
156
157
158
159
/// Implements `CommonExtProvider` for `NvCreateChatCompletionRequest`,
/// providing access to common extension fields.
impl CommonExtProvider for NvCreateChatCompletionRequest {
    /// Returns a reference to the CommonExt struct.
    fn common_ext(&self) -> Option<&CommonExt> {
        Some(&self.common)
    }

    /// Guided Decoding Options
    fn get_guided_json(&self) -> Option<&serde_json::Value> {
160
161
162
163
164
165
        // Note: This one needs special handling since it returns a reference
        if let Some(nvext) = &self.nvext
            && nvext.guided_json.is_some()
        {
            emit_nvext_deprecation_warning("guided_json", true, self.common.guided_json.is_some());
        }
166
167
168
169
170
171
172
        self.common
            .guided_json
            .as_ref()
            .or_else(|| self.nvext.as_ref().and_then(|nv| nv.guided_json.as_ref()))
    }

    fn get_guided_regex(&self) -> Option<String> {
173
174
175
176
177
        choose_with_deprecation(
            "guided_regex",
            self.common.guided_regex.as_ref(),
            self.nvext.as_ref().and_then(|nv| nv.guided_regex.as_ref()),
        )
178
179
180
    }

    fn get_guided_grammar(&self) -> Option<String> {
181
182
183
184
185
186
187
        choose_with_deprecation(
            "guided_grammar",
            self.common.guided_grammar.as_ref(),
            self.nvext
                .as_ref()
                .and_then(|nv| nv.guided_grammar.as_ref()),
        )
188
189
190
    }

    fn get_guided_choice(&self) -> Option<Vec<String>> {
191
192
193
194
195
        choose_with_deprecation(
            "guided_choice",
            self.common.guided_choice.as_ref(),
            self.nvext.as_ref().and_then(|nv| nv.guided_choice.as_ref()),
        )
196
197
198
    }

    fn get_guided_decoding_backend(&self) -> Option<String> {
199
200
201
        choose_with_deprecation(
            "guided_decoding_backend",
            self.common.guided_decoding_backend.as_ref(),
202
203
            self.nvext
                .as_ref()
204
205
                .and_then(|nv| nv.guided_decoding_backend.as_ref()),
        )
206
    }
207
208
209
210
211
212
213
214
215

    fn get_top_k(&self) -> Option<i32> {
        choose_with_deprecation(
            "top_k",
            self.common.top_k.as_ref(),
            self.nvext.as_ref().and_then(|nv| nv.top_k.as_ref()),
        )
    }

216
217
218
219
220
221
222
223
    fn get_min_p(&self) -> Option<f32> {
        choose_with_deprecation(
            "min_p",
            self.common.min_p.as_ref(),
            self.nvext.as_ref().and_then(|nv| nv.min_p.as_ref()),
        )
    }

224
225
226
227
228
229
230
231
232
    fn get_repetition_penalty(&self) -> Option<f32> {
        choose_with_deprecation(
            "repetition_penalty",
            self.common.repetition_penalty.as_ref(),
            self.nvext
                .as_ref()
                .and_then(|nv| nv.repetition_penalty.as_ref()),
        )
    }
233
234
235
236

    fn get_include_stop_str_in_output(&self) -> Option<bool> {
        self.common.include_stop_str_in_output
    }
237
238
}

239
240
/// Implements `OpenAIStopConditionsProvider` for `NvCreateChatCompletionRequest`,
/// providing access to stop conditions that control chat completion behavior.
241
impl OpenAIStopConditionsProvider for NvCreateChatCompletionRequest {
242
    /// Retrieves the maximum number of tokens allowed in the response.
243
    #[allow(deprecated)]
Paul Hendricks's avatar
Paul Hendricks committed
244
    fn get_max_tokens(&self) -> Option<u32> {
245
        self.inner.max_completion_tokens.or(self.inner.max_tokens)
246
247
    }

248
    /// Retrieves the minimum number of tokens required in the response.
249
250
    /// Returns `min_tokens` Value
    /// `min_tokens` is not an OpenAI-supported parameter.
Paul Hendricks's avatar
Paul Hendricks committed
251
    fn get_min_tokens(&self) -> Option<u32> {
252
        self.common.min_tokens
253
254
    }

255
256
257
258
259
260
261
    /// Retrieves the stop conditions that terminate the chat completion response.
    ///
    /// Converts OpenAI's `Stop` enum to a `Vec<String>`, normalizing the representation.
    ///
    /// # Returns
    /// * `Some(Vec<String>)` if stop conditions are set.
    /// * `None` if no stop conditions are defined.
262
    fn get_stop(&self) -> Option<Vec<String>> {
263
        self.inner.stop.as_ref().map(|stop| match stop {
264
265
            dynamo_async_openai::types::Stop::String(s) => vec![s.clone()],
            dynamo_async_openai::types::Stop::StringArray(arr) => arr.clone(),
266
        })
267
268
    }

269
    /// Returns a reference to the optional `NvExt` extension, if available.
270
271
272
    fn nvext(&self) -> Option<&NvExt> {
        self.nvext.as_ref()
    }
273
274
275
276
277

    /// Get ignore_eos from CommonExt.
    fn get_common_ignore_eos(&self) -> Option<bool> {
        self.common.ignore_eos
    }
278
279
280
281
282
283
284
285
286
287

    /// Get the effective ignore_eos value, considering both CommonExt and NvExt.
    /// CommonExt (root-level) takes precedence over NvExt.
    fn get_ignore_eos(&self) -> Option<bool> {
        choose_with_deprecation(
            "ignore_eos",
            self.get_common_ignore_eos().as_ref(),
            NvExtProvider::nvext(self).and_then(|nv| nv.ignore_eos.as_ref()),
        )
    }
288
}
289

Greg Clark's avatar
Greg Clark committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
impl OpenAIOutputOptionsProvider for NvCreateChatCompletionRequest {
    fn get_logprobs(&self) -> Option<u32> {
        match self.inner.logprobs {
            Some(true) => match self.inner.top_logprobs {
                Some(top_logprobs) => Some(top_logprobs as u32),
                None => Some(1_u32),
            },
            Some(false) => None,
            None => None,
        }
    }

    fn get_prompt_logprobs(&self) -> Option<u32> {
        None
    }

    fn get_skip_special_tokens(&self) -> Option<bool> {
        None
    }

    fn get_formatted_prompt(&self) -> Option<bool> {
        None
    }
}

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/// Implements `ValidateRequest` for `NvCreateChatCompletionRequest`,
/// allowing us to validate the data.
impl ValidateRequest for NvCreateChatCompletionRequest {
    fn validate(&self) -> Result<(), anyhow::Error> {
        validate::validate_messages(&self.inner.messages)?;
        validate::validate_model(&self.inner.model)?;
        // none for store
        validate::validate_reasoning_effort(&self.inner.reasoning_effort)?;
        validate::validate_metadata(&self.inner.metadata)?;
        validate::validate_frequency_penalty(self.inner.frequency_penalty)?;
        validate::validate_logit_bias(&self.inner.logit_bias)?;
        // none for logprobs
        validate::validate_top_logprobs(self.inner.top_logprobs)?;
        // validate::validate_max_tokens(self.inner.max_tokens)?; // warning depricated field
        validate::validate_max_completion_tokens(self.inner.max_completion_tokens)?;
        validate::validate_n(self.inner.n)?;
        // none for modalities
        // none for prediction
        // none for audio
        validate::validate_presence_penalty(self.inner.presence_penalty)?;
        // none for response_format
        // none for seed
        validate::validate_service_tier(&self.inner.service_tier)?;
        validate::validate_stop(&self.inner.stop)?;
        // none for stream
        // none for stream_options
        validate::validate_temperature(self.inner.temperature)?;
        validate::validate_top_p(self.inner.top_p)?;
        validate::validate_tools(&self.inner.tools.as_deref())?;
        // none for tool_choice
        // none for parallel_tool_calls
        validate::validate_user(self.inner.user.as_deref())?;
        // none for function call
        // none for functions
349
350
        // Common Ext
        validate::validate_repetition_penalty(self.get_repetition_penalty())?;
351
352
353
354

        Ok(())
    }
}