Skip to main content

ferrin_spec/dynamic/
transcription_model.rs

1//! Object-safe transcription model.
2
3use super::BoxFuture;
4use super::ModelRef;
5use super::model_ref::ref_conversions;
6use crate::error::ProviderError;
7use crate::shared::ModelId;
8use crate::shared::ProviderId;
9use crate::transcription_model::TranscriptionModel;
10use crate::transcription_model::TranscriptionOptions;
11use crate::transcription_model::TranscriptionResult;
12use crate::transcription_model::TranscriptionStreamOptions;
13use crate::transcription_model::TranscriptionStreamResult;
14
15/// Object-safe counterpart of [`TranscriptionModel`].
16pub trait DynTranscriptionModel: Send + Sync + 'static {
17    /// See [`TranscriptionModel::provider`].
18    fn provider(&self) -> &ProviderId;
19    /// See [`TranscriptionModel::model_id`].
20    fn model_id(&self) -> &ModelId;
21    /// See [`TranscriptionModel::do_generate`].
22    fn do_generate(
23        &self,
24        options: TranscriptionOptions,
25    ) -> BoxFuture<'_, Result<TranscriptionResult, ProviderError>>;
26    /// See [`TranscriptionModel::supports_stream`].
27    fn supports_stream(&self) -> bool;
28    /// See [`TranscriptionModel::do_stream`].
29    fn do_stream(
30        &self,
31        options: TranscriptionStreamOptions,
32    ) -> BoxFuture<'_, Result<TranscriptionStreamResult, ProviderError>>;
33}
34
35impl<T: TranscriptionModel> DynTranscriptionModel for T {
36    fn provider(&self) -> &ProviderId {
37        TranscriptionModel::provider(self)
38    }
39
40    fn model_id(&self) -> &ModelId {
41        TranscriptionModel::model_id(self)
42    }
43
44    fn do_generate(
45        &self,
46        options: TranscriptionOptions,
47    ) -> BoxFuture<'_, Result<TranscriptionResult, ProviderError>> {
48        Box::pin(TranscriptionModel::do_generate(self, options))
49    }
50
51    fn supports_stream(&self) -> bool {
52        TranscriptionModel::supports_stream(self)
53    }
54
55    fn do_stream(
56        &self,
57        options: TranscriptionStreamOptions,
58    ) -> BoxFuture<'_, Result<TranscriptionStreamResult, ProviderError>> {
59        Box::pin(TranscriptionModel::do_stream(self, options))
60    }
61}
62
63/// Shared reference to a transcription model (or an unresolved model id).
64pub type TranscriptionModelRef = ModelRef<dyn DynTranscriptionModel>;
65
66ref_conversions!(
67    TranscriptionModelRef,
68    TranscriptionModel,
69    DynTranscriptionModel
70);