11use std:: path:: PathBuf ;
22use std:: sync:: Arc ;
33
4- pub use spin_llm_local:: LocalLlmEngine ;
5-
64use spin_llm_remote_http:: RemoteHttpLlmEngine ;
75use spin_world:: async_trait;
86use spin_world:: v1:: llm:: { self as v1} ;
@@ -12,26 +10,48 @@ use url::Url;
1210
1311use crate :: { LlmEngine , LlmEngineCreator , RuntimeConfig } ;
1412
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 ;
2517
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+ }
3236 }
3337}
3438
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+
3555#[ async_trait]
3656impl LlmEngine for RemoteHttpLlmEngine {
3757 async fn infer (
@@ -77,6 +97,12 @@ pub enum LlmCompute {
7797impl LlmCompute {
7898 fn into_engine ( self , state_dir : PathBuf , use_gpu : bool ) -> Arc < Mutex < dyn LlmEngine > > {
7999 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" ) ]
80106 LlmCompute :: Spin => default_engine_creator ( state_dir, use_gpu) . create ( ) ,
81107 LlmCompute :: RemoteHttp ( config) => Arc :: new ( Mutex :: new ( RemoteHttpLlmEngine :: new (
82108 config. url ,
@@ -92,15 +118,35 @@ pub struct RemoteHttpCompute {
92118 auth_token : String ,
93119}
94120
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+ }
105151 }
106152}
0 commit comments