spin_factor_llm/
spin.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::path::PathBuf;
use std::sync::Arc;

use spin_factors::runtime_config::toml::GetTomlValue;
use spin_llm_remote_http::RemoteHttpLlmEngine;
use spin_world::async_trait;
use spin_world::v1::llm::{self as v1};
use spin_world::v2::llm::{self as v2};
use tokio::sync::Mutex;
use url::Url;

use crate::{LlmEngine, LlmEngineCreator, RuntimeConfig};

#[cfg(feature = "llm")]
mod local {
    use super::*;
    pub use spin_llm_local::LocalLlmEngine;

    #[async_trait]
    impl LlmEngine for LocalLlmEngine {
        async fn infer(
            &mut self,
            model: v2::InferencingModel,
            prompt: String,
            params: v2::InferencingParams,
        ) -> Result<v2::InferencingResult, v2::Error> {
            self.infer(model, prompt, params).await
        }

        async fn generate_embeddings(
            &mut self,
            model: v2::EmbeddingModel,
            data: Vec<String>,
        ) -> Result<v2::EmbeddingsResult, v2::Error> {
            self.generate_embeddings(model, data).await
        }

        fn summary(&self) -> Option<String> {
            Some("local model".to_string())
        }
    }
}

/// The default engine creator for the LLM factor when used in the Spin CLI.
pub fn default_engine_creator(
    state_dir: Option<PathBuf>,
) -> anyhow::Result<impl LlmEngineCreator + 'static> {
    #[cfg(feature = "llm")]
    let engine = {
        use anyhow::Context as _;
        let models_dir_parent = match state_dir {
            Some(ref dir) => dir.clone(),
            None => std::env::current_dir().context("failed to get current working directory")?,
        };
        spin_llm_local::LocalLlmEngine::new(models_dir_parent.join("ai-models"))
    };
    #[cfg(not(feature = "llm"))]
    let engine = {
        let _ = state_dir;
        noop::NoopLlmEngine
    };
    let engine = Arc::new(Mutex::new(engine)) as Arc<Mutex<dyn LlmEngine>>;
    Ok(move || engine.clone())
}

#[async_trait]
impl LlmEngine for RemoteHttpLlmEngine {
    async fn infer(
        &mut self,
        model: v1::InferencingModel,
        prompt: String,
        params: v2::InferencingParams,
    ) -> Result<v2::InferencingResult, v2::Error> {
        self.infer(model, prompt, params).await
    }

    async fn generate_embeddings(
        &mut self,
        model: v2::EmbeddingModel,
        data: Vec<String>,
    ) -> Result<v2::EmbeddingsResult, v2::Error> {
        self.generate_embeddings(model, data).await
    }

    fn summary(&self) -> Option<String> {
        Some(format!("model at {}", self.url()))
    }
}

pub fn runtime_config_from_toml(
    table: &impl GetTomlValue,
    state_dir: Option<PathBuf>,
) -> anyhow::Result<Option<RuntimeConfig>> {
    let Some(value) = table.get("llm_compute") else {
        return Ok(None);
    };
    let config: LlmCompute = value.clone().try_into()?;

    Ok(Some(RuntimeConfig {
        engine: config.into_engine(state_dir)?,
    }))
}

#[derive(Debug, serde::Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum LlmCompute {
    Spin,
    RemoteHttp(RemoteHttpCompute),
}

impl LlmCompute {
    fn into_engine(self, state_dir: Option<PathBuf>) -> anyhow::Result<Arc<Mutex<dyn LlmEngine>>> {
        let engine: Arc<Mutex<dyn LlmEngine>> = match self {
            #[cfg(not(feature = "llm"))]
            LlmCompute::Spin => {
                let _ = state_dir;
                Arc::new(Mutex::new(noop::NoopLlmEngine))
            }
            #[cfg(feature = "llm")]
            LlmCompute::Spin => default_engine_creator(state_dir)?.create(),
            LlmCompute::RemoteHttp(config) => Arc::new(Mutex::new(RemoteHttpLlmEngine::new(
                config.url,
                config.auth_token,
            ))),
        };
        Ok(engine)
    }
}

#[derive(Debug, serde::Deserialize)]
pub struct RemoteHttpCompute {
    url: Url,
    auth_token: String,
}

/// A noop engine used when the local engine feature is disabled.
#[cfg(not(feature = "llm"))]
mod noop {
    use super::*;

    #[derive(Clone, Copy)]
    pub(super) struct NoopLlmEngine;

    #[async_trait]
    impl LlmEngine for NoopLlmEngine {
        async fn infer(
            &mut self,
            _model: v2::InferencingModel,
            _prompt: String,
            _params: v2::InferencingParams,
        ) -> Result<v2::InferencingResult, v2::Error> {
            Err(v2::Error::RuntimeError(
                "Local LLM operations are not supported in this version of Spin.".into(),
            ))
        }

        async fn generate_embeddings(
            &mut self,
            _model: v2::EmbeddingModel,
            _data: Vec<String>,
        ) -> Result<v2::EmbeddingsResult, v2::Error> {
            Err(v2::Error::RuntimeError(
                "Local LLM operations are not supported in this version of Spin.".into(),
            ))
        }

        fn summary(&self) -> Option<String> {
            Some("noop model".to_owned())
        }
    }
}