opt.rs 2.87 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
use dynamo_runtime::protocols::ENDPOINT_SCHEME;
5
use std::fmt;
6

7
8
9
pub enum Output {
    /// Accept un-preprocessed requests, echo the prompt back as the response
    EchoFull,
10

11
12
13
    /// Accept preprocessed requests, echo the tokens back as the response
    EchoCore,

14
    /// Listen for models on nats/etcd, add/remove dynamically
15
16
17
18
19
20
21
22
    Auto,

    /// Static remote: The dyn://namespace.component.endpoint name of a remote worker we expect to
    /// exists. THIS DISABLES AUTO-DISCOVERY. Only this endpoint will be connected.
    /// `--model-name and `--model-path` must also be set.
    ///
    /// A static remote setup avoids having to run etcd.
    Static(String),
23

24
25
26
    #[cfg(feature = "mistralrs")]
    /// Run inference on a model in a GGUF file using mistralrs w/ candle
    MistralRs,
27

28
29
30
    #[cfg(feature = "llamacpp")]
    /// Run inference using llama.cpp
    LlamaCpp,
Graham King's avatar
Graham King committed
31

32
    Mocker,
33
34
35
36
37
38
39
}

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

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

43
44
45
            #[cfg(feature = "llamacpp")]
            "llamacpp" | "llama_cpp" => Ok(Output::LlamaCpp),

46
            "mocker" => Ok(Output::Mocker),
47
            "echo_full" => Ok(Output::EchoFull),
48
            "echo_core" => Ok(Output::EchoCore),
49

50
            "dyn" | "auto" => Ok(Output::Auto),
51

52
            endpoint_path if endpoint_path.starts_with(ENDPOINT_SCHEME) => {
53
54
                let path = endpoint_path.strip_prefix(ENDPOINT_SCHEME).unwrap();
                Ok(Output::Static(path.to_string()))
55
56
            }

57
58
59
60
61
62
63
64
            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 {
65
66
67
            #[cfg(feature = "mistralrs")]
            Output::MistralRs => "mistralrs",

68
69
70
            #[cfg(feature = "llamacpp")]
            Output::LlamaCpp => "llamacpp",

71
            Output::Mocker => "mocker",
72
            Output::EchoFull => "echo_full",
73
            Output::EchoCore => "echo_core",
74

75
76
            Output::Auto => "auto",
            Output::Static(endpoint) => &format!("{ENDPOINT_SCHEME}{endpoint}"),
77
78
79
80
        };
        write!(f, "{s}")
    }
}
81

82
83
84
impl Output {
    #[allow(unused_mut)]
    pub fn available_engines() -> Vec<String> {
85
86
87
88
89
        let mut out = vec![
            "echo_core".to_string(),
            "echo_full".to_string(),
            Output::Mocker.to_string(),
        ];
90
91
92
93
94
95
96
97
98
99
100
101
102
        #[cfg(feature = "mistralrs")]
        {
            out.push(Output::MistralRs.to_string());
        }

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

        out
    }
}