Skip to main content

ferrin_spec/language_model/
mod.rs

1//! Language model interface: call options, prompt, tools, content, stream
2//! parts, results and the [`LanguageModel`] trait.
3
4use std::future::Future;
5
6use crate::error::ProviderError;
7use crate::shared::ModelId;
8use crate::shared::ProviderId;
9
10pub mod call_options;
11pub mod content;
12pub mod finish_reason;
13pub mod prompt;
14pub mod result;
15pub mod stream_part;
16pub mod supported_urls;
17pub mod tool;
18pub mod usage;
19
20pub use call_options::CallOptions;
21pub use call_options::CallOptionsRecord;
22pub use call_options::ReasoningEffort;
23pub use call_options::ResponseFormat;
24pub use call_options::ToolChoice;
25pub use content::Content;
26pub use content::CustomKind;
27pub use content::InvalidCustomKind;
28pub use content::ProviderToolResult;
29pub use content::Source;
30pub use content::ToolCall;
31pub use finish_reason::FinishReason;
32pub use finish_reason::FinishReasonKind;
33pub use prompt::Prompt;
34pub use prompt::PromptMessage;
35pub use result::GenerateResult;
36pub use result::RequestMetadata;
37pub use result::ResponseMetadata;
38pub use result::StreamResult;
39pub use stream_part::StreamError;
40pub use stream_part::StreamErrorCode;
41pub use stream_part::StreamPart;
42pub use supported_urls::SupportedUrls;
43pub use tool::ToolDefinition;
44pub use usage::InputTokens;
45pub use usage::OutputTokens;
46pub use usage::Usage;
47pub use usage::add_token_counts;
48
49/// A text generation model.
50///
51/// Implement this trait in a provider crate for every language model API.
52/// Implementations must be cheap to clone behind an `Arc` and safe to call
53/// concurrently. See the adapter contract in the provider specification:
54/// unsupported options produce warnings, tool input is passed through as raw
55/// JSON text, streams start with `StreamStart` and end with `Finish` or
56/// `Error`, and the cancellation token aborts the HTTP request.
57pub trait LanguageModel: Send + Sync + 'static {
58    /// Provider identifier, for example `openai.responses`.
59    fn provider(&self) -> &ProviderId;
60
61    /// Model identifier as sent to the provider.
62    fn model_id(&self) -> &ModelId;
63
64    /// URL patterns (by media type) the provider can fetch itself.
65    ///
66    /// Files whose URL does not match are downloaded by the core and inlined.
67    fn supported_urls(&self) -> impl Future<Output = SupportedUrls> + Send;
68
69    /// Generates a complete response.
70    fn do_generate(
71        &self,
72        options: CallOptions,
73    ) -> impl Future<Output = Result<GenerateResult, ProviderError>> + Send;
74
75    /// Generates a streamed response.
76    fn do_stream(
77        &self,
78        options: CallOptions,
79    ) -> impl Future<Output = Result<StreamResult, ProviderError>> + Send;
80}