1
1
use std:: path:: PathBuf ;
2
2
use std:: sync:: Arc ;
3
3
4
- pub use spin_llm_local:: LocalLlmEngine ;
5
-
6
4
use spin_llm_remote_http:: RemoteHttpLlmEngine ;
7
5
use spin_world:: async_trait;
8
6
use spin_world:: v1:: llm:: { self as v1} ;
@@ -12,26 +10,48 @@ use url::Url;
12
10
13
11
use crate :: { LlmEngine , LlmEngineCreator , RuntimeConfig } ;
14
12
15
- #[ async_trait]
16
- impl LlmEngine for LocalLlmEngine {
17
- async fn infer (
18
- & mut self ,
19
- model : v1:: InferencingModel ,
20
- prompt : String ,
21
- params : v2:: InferencingParams ,
22
- ) -> Result < v2:: InferencingResult , v2:: Error > {
23
- self . infer ( model, prompt, params) . await
24
- }
13
+ #[ cfg( feature = "llm" ) ]
14
+ mod local {
15
+ use super :: * ;
16
+ pub use spin_llm_local:: LocalLlmEngine ;
25
17
26
- async fn generate_embeddings (
27
- & mut self ,
28
- model : v2:: EmbeddingModel ,
29
- data : Vec < String > ,
30
- ) -> Result < v2:: EmbeddingsResult , v2:: Error > {
31
- self . generate_embeddings ( model, data) . await
18
+ #[ async_trait]
19
+ impl LlmEngine for LocalLlmEngine {
20
+ async fn infer (
21
+ & mut self ,
22
+ model : v2:: InferencingModel ,
23
+ prompt : String ,
24
+ params : v2:: InferencingParams ,
25
+ ) -> Result < v2:: InferencingResult , v2:: Error > {
26
+ self . infer ( model, prompt, params) . await
27
+ }
28
+
29
+ async fn generate_embeddings (
30
+ & mut self ,
31
+ model : v2:: EmbeddingModel ,
32
+ data : Vec < String > ,
33
+ ) -> Result < v2:: EmbeddingsResult , v2:: Error > {
34
+ self . generate_embeddings ( model, data) . await
35
+ }
32
36
}
33
37
}
34
38
39
+ /// The default engine creator for the LLM factor when used in the Spin CLI.
40
+ pub fn default_engine_creator (
41
+ state_dir : PathBuf ,
42
+ use_gpu : bool ,
43
+ ) -> impl LlmEngineCreator + ' static {
44
+ #[ cfg( feature = "llm" ) ]
45
+ let engine = spin_llm_local:: LocalLlmEngine :: new ( state_dir. join ( "ai-models" ) , use_gpu) ;
46
+ #[ cfg( not( feature = "llm" ) ) ]
47
+ let engine = {
48
+ let _ = ( state_dir, use_gpu) ;
49
+ noop:: NoopLlmEngine
50
+ } ;
51
+ let engine = Arc :: new ( Mutex :: new ( engine) ) as Arc < Mutex < dyn LlmEngine > > ;
52
+ move || engine. clone ( )
53
+ }
54
+
35
55
#[ async_trait]
36
56
impl LlmEngine for RemoteHttpLlmEngine {
37
57
async fn infer (
@@ -77,6 +97,12 @@ pub enum LlmCompute {
77
97
impl LlmCompute {
78
98
fn into_engine ( self , state_dir : PathBuf , use_gpu : bool ) -> Arc < Mutex < dyn LlmEngine > > {
79
99
match self {
100
+ #[ cfg( not( feature = "llm" ) ) ]
101
+ LlmCompute :: Spin => {
102
+ let _ = ( state_dir, use_gpu) ;
103
+ Arc :: new ( Mutex :: new ( noop:: NoopLlmEngine ) )
104
+ }
105
+ #[ cfg( feature = "llm" ) ]
80
106
LlmCompute :: Spin => default_engine_creator ( state_dir, use_gpu) . create ( ) ,
81
107
LlmCompute :: RemoteHttp ( config) => Arc :: new ( Mutex :: new ( RemoteHttpLlmEngine :: new (
82
108
config. url ,
@@ -92,15 +118,35 @@ pub struct RemoteHttpCompute {
92
118
auth_token : String ,
93
119
}
94
120
95
- /// The default engine creator for the LLM factor when used in the Spin CLI.
96
- pub fn default_engine_creator (
97
- state_dir : PathBuf ,
98
- use_gpu : bool ,
99
- ) -> impl LlmEngineCreator + ' static {
100
- move || {
101
- Arc :: new ( Mutex :: new ( LocalLlmEngine :: new (
102
- state_dir. join ( "ai-models" ) ,
103
- use_gpu,
104
- ) ) ) as _
121
+ /// A noop engine used when the local engine feature is disabled.
122
+ #[ cfg( not( feature = "llm" ) ) ]
123
+ mod noop {
124
+ use super :: * ;
125
+
126
+ #[ derive( Clone , Copy ) ]
127
+ pub ( super ) struct NoopLlmEngine ;
128
+
129
+ #[ async_trait]
130
+ impl LlmEngine for NoopLlmEngine {
131
+ async fn infer (
132
+ & mut self ,
133
+ _model : v2:: InferencingModel ,
134
+ _prompt : String ,
135
+ _params : v2:: InferencingParams ,
136
+ ) -> Result < v2:: InferencingResult , v2:: Error > {
137
+ Err ( v2:: Error :: RuntimeError (
138
+ "Local LLM operations are not supported in this version of Spin." . into ( ) ,
139
+ ) )
140
+ }
141
+
142
+ async fn generate_embeddings (
143
+ & mut self ,
144
+ _model : v2:: EmbeddingModel ,
145
+ _data : Vec < String > ,
146
+ ) -> Result < v2:: EmbeddingsResult , v2:: Error > {
147
+ Err ( v2:: Error :: RuntimeError (
148
+ "Local LLM operations are not supported in this version of Spin." . into ( ) ,
149
+ ) )
150
+ }
105
151
}
106
152
}
0 commit comments