Skip to main content

bitrouter_core/models/language/
language_model.rs

1use dynosaur::dynosaur;
2use regex::Regex;
3
4use crate::{errors::Result, models::shared::types::Record};
5
6use super::{
7    call_options::LanguageModelCallOptions, generate_result::LanguageModelGenerateResult,
8    stream_result::LanguageModelStreamResult,
9};
10
11/// The main trait for a language model provider, which can generate content based on a prompt and options.
12///
13/// Each implementation represents a concrete upstream model instance (e.g. "gpt-4o via OpenAI chat completions",
14/// "claude-3-5-sonnet via Anthropic messages"). The model ID is stored on the instance,
15/// not passed per-request.
16#[dynosaur(pub DynLanguageModel = dyn(box) LanguageModel)]
17pub trait LanguageModel: Send + Sync {
18    /// Provider name, e.g. "openai", "anthropic", etc.
19    fn provider_name(&self) -> &str;
20
21    /// The upstream model ID, e.g. "gpt-4o", "claude-3-5-sonnet-20241022", etc.
22    fn model_id(&self) -> &str;
23
24    /// Media type -> Regex for supported URLs of that media type
25    ///
26    /// Matched URLs are supported natively by the model and are not downloaded.
27    fn supported_urls(&self) -> impl Future<Output = Record<String, Regex>> + Send;
28
29    /// Generates content based on the given options.
30    fn generate(
31        &self,
32        options: LanguageModelCallOptions,
33    ) -> impl Future<Output = Result<LanguageModelGenerateResult>> + Send;
34
35    /// Generates content based on the given options, but returns a stream of partial results.
36    fn stream(
37        &self,
38        options: LanguageModelCallOptions,
39    ) -> impl Future<Output = Result<LanguageModelStreamResult>> + Send;
40}