chat_completions.rs 3.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Paul Hendricks's avatar
Paul Hendricks committed
16
17
18
19
use super::nvext::NvExt;
use super::nvext::NvExtProvider;
use super::OpenAISamplingOptionsProvider;
use super::OpenAIStopConditionsProvider;
20
use serde::{Deserialize, Serialize};
Paul Hendricks's avatar
Paul Hendricks committed
21
use triton_distributed_runtime::protocols::annotated::AnnotationsProvider;
22
23
24
25
26
use validator::Validate;

mod aggregator;
mod delta;

Paul Hendricks's avatar
Paul Hendricks committed
27
pub use aggregator::DeltaAggregator;
28
29
pub use delta::DeltaGenerator;

Paul Hendricks's avatar
Paul Hendricks committed
30
#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
31
pub struct NvCreateChatCompletionRequest {
Paul Hendricks's avatar
Paul Hendricks committed
32
33
    #[serde(flatten)]
    pub inner: async_openai::types::CreateChatCompletionRequest,
34
35
36
    pub nvext: Option<NvExt>,
}

Paul Hendricks's avatar
Paul Hendricks committed
37
#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
38
pub struct NvCreateChatCompletionResponse {
Paul Hendricks's avatar
Paul Hendricks committed
39
40
    #[serde(flatten)]
    pub inner: async_openai::types::CreateChatCompletionResponse,
41
42
}

Paul Hendricks's avatar
Paul Hendricks committed
43
#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
44
pub struct ChatCompletionContent {
Paul Hendricks's avatar
Paul Hendricks committed
45
46
    #[serde(flatten)]
    pub inner: async_openai::types::ChatCompletionStreamResponseDelta,
47
48
}

Paul Hendricks's avatar
Paul Hendricks committed
49
50
51
52
#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
pub struct ChatCompletionResponseDelta {
    #[serde(flatten)]
    pub inner: async_openai::types::CreateChatCompletionStreamResponse,
53
54
}

55
impl NvExtProvider for NvCreateChatCompletionRequest {
56
57
58
59
60
61
62
63
64
    fn nvext(&self) -> Option<&NvExt> {
        self.nvext.as_ref()
    }

    fn raw_prompt(&self) -> Option<String> {
        None
    }
}

65
impl AnnotationsProvider for NvCreateChatCompletionRequest {
Biswa Panda's avatar
Biswa Panda committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    fn annotations(&self) -> Option<Vec<String>> {
        self.nvext
            .as_ref()
            .and_then(|nvext| nvext.annotations.clone())
    }

    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)
    }
}
80

81
impl OpenAISamplingOptionsProvider for NvCreateChatCompletionRequest {
82
    fn get_temperature(&self) -> Option<f32> {
Paul Hendricks's avatar
Paul Hendricks committed
83
        self.inner.temperature
84
85
86
    }

    fn get_top_p(&self) -> Option<f32> {
Paul Hendricks's avatar
Paul Hendricks committed
87
        self.inner.top_p
88
89
90
    }

    fn get_frequency_penalty(&self) -> Option<f32> {
Paul Hendricks's avatar
Paul Hendricks committed
91
        self.inner.frequency_penalty
92
93
94
    }

    fn get_presence_penalty(&self) -> Option<f32> {
Paul Hendricks's avatar
Paul Hendricks committed
95
        self.inner.presence_penalty
96
97
98
99
100
101
102
    }

    fn nvext(&self) -> Option<&NvExt> {
        self.nvext.as_ref()
    }
}

Paul Hendricks's avatar
Paul Hendricks committed
103
#[allow(deprecated)]
104
impl OpenAIStopConditionsProvider for NvCreateChatCompletionRequest {
Paul Hendricks's avatar
Paul Hendricks committed
105
106
107
    fn get_max_tokens(&self) -> Option<u32> {
        // ALLOW: max_tokens is deprecated in favor of max_completion_tokens
        self.inner.max_tokens
108
109
    }

Paul Hendricks's avatar
Paul Hendricks committed
110
111
112
    fn get_min_tokens(&self) -> Option<u32> {
        // TODO THIS IS WRONG min_tokens does not exist
        None
113
114
115
    }

    fn get_stop(&self) -> Option<Vec<String>> {
Paul Hendricks's avatar
Paul Hendricks committed
116
117
118
119
        // TODO THIS IS WRONG should instead do
        // Vec<String> -> async_openai::types::Stop
        // self.inner.stop.clone()
        None
120
121
122
123
124
125
    }

    fn nvext(&self) -> Option<&NvExt> {
        self.nvext.as_ref()
    }
}