opt.rs 5.28 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.

16
use std::{fmt, io::IsTerminal as _, path::PathBuf};
17

18
use dynamo_runtime::protocols::ENDPOINT_SCHEME;
19

20
21
const BATCH_PREFIX: &str = "batch:";

22
#[derive(PartialEq)]
23
24
25
26
pub enum Input {
    /// Run an OpenAI compatible HTTP server
    Http,

27
28
29
30
    /// Single prompt on stdin
    Stdin,

    /// Interactive chat
31
    Text,
32
33
34

    /// Pull requests from a namespace/component/endpoint path.
    Endpoint(String),
35

36
37
    /// Batch mode. Run all the prompts, write the outputs, exit.
    Batch(PathBuf),
38
39
40
41
42
43
44
45
46
}

impl TryFrom<&str> for Input {
    type Error = anyhow::Error;

    fn try_from(s: &str) -> anyhow::Result<Self> {
        match s {
            "http" => Ok(Input::Http),
            "text" => Ok(Input::Text),
47
            "stdin" => Ok(Input::Stdin),
48
            endpoint_path if endpoint_path.starts_with(ENDPOINT_SCHEME) => {
49
                Ok(Input::Endpoint(endpoint_path.to_string()))
50
            }
51
52
53
54
            batch_patch if batch_patch.starts_with(BATCH_PREFIX) => {
                let path = batch_patch.strip_prefix(BATCH_PREFIX).unwrap();
                Ok(Input::Batch(PathBuf::from(path)))
            }
55
56
57
58
59
60
61
62
63
64
            e => Err(anyhow::anyhow!("Invalid in= option '{e}'")),
        }
    }
}

impl fmt::Display for Input {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
            Input::Http => "http",
            Input::Text => "text",
65
            Input::Stdin => "stdin",
66
            Input::Endpoint(path) => path,
67
            Input::Batch(path) => &path.display().to_string(),
68
69
70
71
72
        };
        write!(f, "{s}")
    }
}

73
74
75
76
77
78
79
80
81
82
impl Default for Input {
    fn default() -> Self {
        if std::io::stdin().is_terminal() {
            Input::Text
        } else {
            Input::Stdin
        }
    }
}

83
84
85
pub enum Output {
    /// Accept un-preprocessed requests, echo the prompt back as the response
    EchoFull,
86

87
88
89
    /// Accept preprocessed requests, echo the tokens back as the response
    EchoCore,

90
91
    /// Listen for models on nats/etcd, add/remove dynamically
    Dynamic,
92

93
94
95
    #[cfg(feature = "mistralrs")]
    /// Run inference on a model in a GGUF file using mistralrs w/ candle
    MistralRs,
96

97
98
99
    #[cfg(feature = "llamacpp")]
    /// Run inference using llama.cpp
    LlamaCpp,
Graham King's avatar
Graham King committed
100

101
102
103
    /// Run inference using sglang
    SgLang,

104
105
106
    /// Run inference using trtllm
    Trtllm,

107
108
    // Start vllm in a sub-process connecting via nats
    // Sugar for `python vllm_inc.py --endpoint <thing> --model <thing>`
Graham King's avatar
Graham King committed
109
    Vllm,
110
111
112
113
114
115
116
}

impl TryFrom<&str> for Output {
    type Error = anyhow::Error;

    fn try_from(s: &str) -> anyhow::Result<Self> {
        match s {
117
118
119
            #[cfg(feature = "mistralrs")]
            "mistralrs" => Ok(Output::MistralRs),

120
121
122
            #[cfg(feature = "llamacpp")]
            "llamacpp" | "llama_cpp" => Ok(Output::LlamaCpp),

123
            "sglang" => Ok(Output::SgLang),
124
            "trtllm" => Ok(Output::Trtllm),
Graham King's avatar
Graham King committed
125
            "vllm" => Ok(Output::Vllm),
126

127
            "echo_full" => Ok(Output::EchoFull),
128
            "echo_core" => Ok(Output::EchoCore),
129

130
131
132
            "dyn" => Ok(Output::Dynamic),

            // Deprecated, should only use `out=dyn`
133
            endpoint_path if endpoint_path.starts_with(ENDPOINT_SCHEME) => {
134
135
136
137
138
                tracing::warn!(
                    "out=dyn://<path> is deprecated, the path is not used. Please use 'out=dyn'"
                );
                //let path = endpoint_path.strip_prefix(ENDPOINT_SCHEME).unwrap();
                Ok(Output::Dynamic)
139
140
            }

141
142
143
144
145
146
147
148
            e => Err(anyhow::anyhow!("Invalid out= option '{e}'")),
        }
    }
}

impl fmt::Display for Output {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
149
150
151
            #[cfg(feature = "mistralrs")]
            Output::MistralRs => "mistralrs",

152
153
154
            #[cfg(feature = "llamacpp")]
            Output::LlamaCpp => "llamacpp",

155
            Output::SgLang => "sglang",
156
            Output::Trtllm => "trtllm",
Graham King's avatar
Graham King committed
157
            Output::Vllm => "vllm",
158

159
            Output::EchoFull => "echo_full",
160
            Output::EchoCore => "echo_core",
161

162
            Output::Dynamic => "dyn",
163
164
165
166
        };
        write!(f, "{s}")
    }
}
167

168
169
170
171
172
173
174
175
176
177
178
179
180
181
impl Output {
    #[allow(unused_mut)]
    pub fn available_engines() -> Vec<String> {
        let mut out = vec!["echo_core".to_string(), "echo_full".to_string()];
        #[cfg(feature = "mistralrs")]
        {
            out.push(Output::MistralRs.to_string());
        }

        #[cfg(feature = "llamacpp")]
        {
            out.push(Output::LlamaCpp.to_string());
        }

182
        out.push(Output::SgLang.to_string());
183
        out.push(Output::Trtllm.to_string());
184
        out.push(Output::Vllm.to_string());
185
186
187
188

        out
    }
}