llm_kit_provider/provider.rs
1use crate::embedding_model::EmbeddingModel;
2use crate::error::ProviderError;
3use crate::image_model::ImageModel;
4use crate::language_model::LanguageModel;
5use crate::reranking_model::RerankingModel;
6use crate::speech_model::SpeechModel;
7use crate::transcription_model::TranscriptionModel;
8use std::sync::Arc;
9
10/// Provider for language, text embedding, image generation, speech, transcription,
11/// and reranking models.
12///
13/// This trait defines the interface that all AI providers must implement to integrate
14/// with the LLM Kit. Providers are responsible for creating and managing model instances.
15///
16/// # Example
17///
18/// ```no_run
19/// use llm_kit_provider::{Provider, LanguageModel, EmbeddingModel, ImageModel, TranscriptionModel, SpeechModel, RerankingModel, ProviderError};
20/// use std::sync::Arc;
21///
22/// struct MyProvider {
23/// provider_id: String,
24/// }
25///
26/// impl Provider for MyProvider {
27/// fn specification_version(&self) -> &str {
28/// "v3"
29/// }
30///
31/// fn language_model(&self, model_id: &str) -> Result<Arc<dyn LanguageModel>, ProviderError> {
32/// // Implementation
33/// Err(ProviderError::no_such_model(model_id, &self.provider_id))
34/// }
35///
36/// fn text_embedding_model(&self, model_id: &str) -> Result<Arc<dyn EmbeddingModel<String>>, ProviderError> {
37/// Err(ProviderError::no_such_model(model_id, "embedding-not-supported"))
38/// }
39///
40/// fn image_model(&self, model_id: &str) -> Result<Arc<dyn ImageModel>, ProviderError> {
41/// Err(ProviderError::no_such_model(model_id, &self.provider_id))
42/// }
43///
44/// fn transcription_model(&self, model_id: &str) -> Result<Arc<dyn TranscriptionModel>, ProviderError> {
45/// Err(ProviderError::no_such_model(model_id, &self.provider_id))
46/// }
47///
48/// fn speech_model(&self, model_id: &str) -> Result<Arc<dyn SpeechModel>, ProviderError> {
49/// Err(ProviderError::no_such_model(model_id, &self.provider_id))
50/// }
51///
52/// fn reranking_model(&self, model_id: &str) -> Result<Arc<dyn RerankingModel>, ProviderError> {
53/// Err(ProviderError::no_such_model(model_id, &self.provider_id))
54/// }
55/// }
56/// ```
57#[allow(clippy::result_large_err)]
58pub trait Provider: Send + Sync {
59 /// The provider must specify which provider interface version it implements.
60 /// This will allow us to evolve the provider interface and retain backwards compatibility.
61 fn specification_version(&self) -> &str {
62 "v3"
63 }
64
65 /// Returns the language model with the given id.
66 ///
67 /// The model id is provider-specific and is used to identify which model
68 /// to instantiate and return.
69 ///
70 /// # Arguments
71 ///
72 /// * `model_id` - The provider-specific identifier for the model
73 ///
74 /// # Returns
75 ///
76 /// * `Ok(Arc<dyn LanguageModel>)` - The language model instance wrapped in Arc
77 /// * `Err(ProviderError::NoSuchModel)` - If the model ID is not recognized
78 ///
79 /// # Errors
80 ///
81 /// Returns `ProviderError::NoSuchModel` if no model with the given ID exists
82 /// in this provider.
83 ///
84 /// # Example
85 ///
86 /// ```no_run
87 /// use llm_kit_provider::{Provider, LanguageModel, ProviderError, EmbeddingModel, ImageModel, TranscriptionModel, SpeechModel, RerankingModel};
88 /// use std::sync::Arc;
89 ///
90 /// struct MyProvider;
91 /// impl Provider for MyProvider {
92 /// fn language_model(&self, model_id: &str) -> Result<Arc<dyn LanguageModel>, ProviderError> {
93 /// unimplemented!()
94 /// }
95 /// fn text_embedding_model(&self, model_id: &str) -> Result<Arc<dyn EmbeddingModel<String>>, ProviderError> {
96 /// unimplemented!()
97 /// }
98 /// fn image_model(&self, model_id: &str) -> Result<Arc<dyn ImageModel>, ProviderError> {
99 /// unimplemented!()
100 /// }
101 /// fn transcription_model(&self, model_id: &str) -> Result<Arc<dyn TranscriptionModel>, ProviderError> {
102 /// unimplemented!()
103 /// }
104 /// fn speech_model(&self, model_id: &str) -> Result<Arc<dyn SpeechModel>, ProviderError> {
105 /// unimplemented!()
106 /// }
107 /// fn reranking_model(&self, model_id: &str) -> Result<Arc<dyn RerankingModel>, ProviderError> {
108 /// unimplemented!()
109 /// }
110 /// }
111 ///
112 /// # fn example() -> Result<(), ProviderError> {
113 /// let provider = MyProvider;
114 /// let model = provider.language_model("gpt-4")?;
115 /// # Ok(())
116 /// # }
117 /// ```
118 fn language_model(&self, model_id: &str) -> Result<Arc<dyn LanguageModel>, ProviderError>;
119
120 /// Returns the text embedding model with the given id.
121 ///
122 /// The model id is provider-specific and is used to identify which embedding
123 /// model to instantiate and return.
124 ///
125 /// # Arguments
126 ///
127 /// * `model_id` - The provider-specific identifier for the embedding model
128 ///
129 /// # Returns
130 ///
131 /// * `Ok(Arc<dyn EmbeddingModel<String>>)` - The text embedding model instance
132 /// * `Err(ProviderError::NoSuchModel)` - If the model ID is not recognized
133 ///
134 /// # Errors
135 ///
136 /// Returns `ProviderError::NoSuchModel` if no embedding model with the given ID
137 /// exists in this provider.
138 fn text_embedding_model(
139 &self,
140 model_id: &str,
141 ) -> Result<Arc<dyn EmbeddingModel<String>>, ProviderError>;
142
143 /// Returns the image model with the given id.
144 ///
145 /// The model id is provider-specific and is used to identify which image
146 /// generation model to instantiate and return.
147 ///
148 /// # Arguments
149 ///
150 /// * `model_id` - The provider-specific identifier for the image model
151 ///
152 /// # Returns
153 ///
154 /// * `Ok(Arc<dyn ImageModel>)` - The image model instance
155 /// * `Err(ProviderError::NoSuchModel)` - If the model ID is not recognized
156 ///
157 /// # Errors
158 ///
159 /// Returns `ProviderError::NoSuchModel` if no image model with the given ID
160 /// exists in this provider.
161 fn image_model(&self, model_id: &str) -> Result<Arc<dyn ImageModel>, ProviderError>;
162
163 /// Returns the transcription model with the given id.
164 ///
165 /// The model id is provider-specific and is used to identify which
166 /// transcription model to instantiate and return.
167 ///
168 /// # Arguments
169 ///
170 /// * `model_id` - The provider-specific identifier for the transcription model
171 ///
172 /// # Returns
173 ///
174 /// * `Ok(Arc<dyn TranscriptionModel>)` - The transcription model instance
175 /// * `Err(ProviderError::NoSuchModel)` - If the model ID is not recognized
176 ///
177 /// # Errors
178 ///
179 /// Returns `ProviderError::NoSuchModel` if no transcription model with the given ID
180 /// exists in this provider.
181 fn transcription_model(
182 &self,
183 model_id: &str,
184 ) -> Result<Arc<dyn TranscriptionModel>, ProviderError>;
185
186 /// Returns the speech model with the given id.
187 ///
188 /// The model id is provider-specific and is used to identify which
189 /// speech synthesis model to instantiate and return.
190 ///
191 /// # Arguments
192 ///
193 /// * `model_id` - The provider-specific identifier for the speech model
194 ///
195 /// # Returns
196 ///
197 /// * `Ok(Arc<dyn SpeechModel>)` - The speech model instance
198 /// * `Err(ProviderError::NoSuchModel)` - If the model ID is not recognized
199 ///
200 /// # Errors
201 ///
202 /// Returns `ProviderError::NoSuchModel` if no speech model with the given ID
203 /// exists in this provider.
204 fn speech_model(&self, model_id: &str) -> Result<Arc<dyn SpeechModel>, ProviderError>;
205
206 /// Returns the reranking model with the given id.
207 ///
208 /// The model id is provider-specific and is used to identify which
209 /// reranking model to instantiate and return.
210 ///
211 /// # Arguments
212 ///
213 /// * `model_id` - The provider-specific identifier for the reranking model
214 ///
215 /// # Returns
216 ///
217 /// * `Ok(Arc<dyn RerankingModel>)` - The reranking model instance
218 /// * `Err(ProviderError::NoSuchModel)` - If the model ID is not recognized
219 ///
220 /// # Errors
221 ///
222 /// Returns `ProviderError::NoSuchModel` if no reranking model with the given ID
223 /// exists in this provider.
224 fn reranking_model(&self, model_id: &str) -> Result<Arc<dyn RerankingModel>, ProviderError>;
225}