rig-core 0.45.0

An opinionated library for building LLM powered applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Portable generation options: provider-neutral knobs a request sets once
//! and every completion wire maps to its own JSON, or refuses. They hold only
//! what [`CompletionRequest`](super::CompletionRequest) has no field for, so
//! `temperature`, `max_tokens`, `tool_choice` and `output_schema` stay on the
//! request.
//!
//! ```
//! use rig_core::completion::{CacheRetention, CompletionRequest, Effort, GenerationOptions};
//!
//! let options = GenerationOptions::default()
//!     .reasoning(Effort::High)
//!     .cache(CacheRetention::Long);
//! let request = CompletionRequest::new("Plan the refactor.").options(options);
//! assert!(!request.options.is_default());
//! ```

use std::borrow::Cow;

use serde::{Deserialize, Serialize};

mod mapping;
mod merge;

pub use mapping::{Mapping, OptionFields, OptionMap};
#[cfg(feature = "image")]
pub(crate) use merge::deep_merge;
pub use merge::{BaseInput, FinalBody, RawAt, Rewrite, check, param, request_params};
pub(crate) use merge::{CatalogRefusal, catalog_refusals, collect_refusals};

/// Provider-neutral generation knobs for one request. An unset field leaves
/// the provider's default. A field the wire or model cannot honour is
/// reported through [`Self::unsupported_policy`], never silently dropped.
///
/// This is the reusable value: build it once and hand it to an agent
/// (`AgentBuilder::options`), overlay a run's on an agent's
/// ([`Self::overlay`]), or store it serialized with a request. For a single
/// request, `CompletionRequest`, `AgentBuilder` and `AgentRunner` also have
/// a shortcut per field (`.seed(7)`) that writes that one field into their
/// options.
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct GenerationOptions {
    /// How much the model reasons before answering.
    #[serde(default)]
    pub reasoning: Option<Reasoning>,
    /// How long the provider keeps the prompt prefix cached.
    #[serde(default)]
    pub cache: Option<CacheRetention>,
    /// The processing tier the request asks for.
    #[serde(default)]
    pub service_tier: Option<ServiceTier>,
    /// How long the answer should be.
    #[serde(default)]
    pub verbosity: Option<Verbosity>,
    /// Whether the model may call several tools in one turn.
    #[serde(default)]
    pub parallel_tool_calls: Option<bool>,
    /// Nucleus sampling probability mass.
    #[serde(default)]
    pub top_p: Option<f64>,
    /// Sampling seed, for providers that offer best-effort determinism.
    #[serde(default)]
    pub seed: Option<u64>,
    /// Sequences that end generation. Empty means none.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub stop: Vec<String>,
    /// A key the provider routes prompt-cache lookups by, such as a
    /// conversation's id: requests with the same key reach the server that
    /// holds their prefix. A hint: providers that route without one send
    /// nothing for it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cache_key: Option<String>,
    /// What happens to an option the wire or model cannot honour. `None`
    /// leaves the policy unset, which acts as [`OnUnsupported::Error`]
    /// ([`Self::unsupported_policy`]); a set policy counts as setting an
    /// option.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub on_unsupported: Option<OnUnsupported>,
}

impl GenerationOptions {
    /// No option set and no policy set; the same as [`Self::default`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Whether every field holds its default: no option set, and no
    /// policy set. A request whose options are default is not checked
    /// against its model's catalog rules; setting any field, the policy
    /// included, turns the checks on.
    pub fn is_default(&self) -> bool {
        *self == Self::default()
    }

    /// The policy in effect: the one set, or [`OnUnsupported::Error`].
    pub fn unsupported_policy(&self) -> OnUnsupported {
        self.on_unsupported.unwrap_or_default()
    }

    /// Set the reasoning level or budget.
    pub fn reasoning(mut self, reasoning: impl Into<Reasoning>) -> Self {
        self.reasoning = Some(reasoning.into());
        self
    }

    /// Set the cache retention.
    pub fn cache(mut self, cache: CacheRetention) -> Self {
        self.cache = Some(cache);
        self
    }

    /// Set the service tier.
    pub fn service_tier(mut self, tier: ServiceTier) -> Self {
        self.service_tier = Some(tier);
        self
    }

    /// Set the answer verbosity.
    pub fn verbosity(mut self, verbosity: Verbosity) -> Self {
        self.verbosity = Some(verbosity);
        self
    }

    /// Allow or forbid several tool calls in one turn.
    pub fn parallel_tool_calls(mut self, parallel: bool) -> Self {
        self.parallel_tool_calls = Some(parallel);
        self
    }

    /// Set the nucleus sampling probability mass.
    pub fn top_p(mut self, top_p: f64) -> Self {
        self.top_p = Some(top_p);
        self
    }

    /// Set the sampling seed.
    pub fn seed(mut self, seed: u64) -> Self {
        self.seed = Some(seed);
        self
    }

    /// Replace the stop sequences.
    pub fn stop<S: Into<String>>(mut self, stop: impl IntoIterator<Item = S>) -> Self {
        self.stop = stop.into_iter().map(Into::into).collect();
        self
    }

    /// Set the prompt-cache routing key.
    pub fn cache_key(mut self, key: impl Into<String>) -> Self {
        self.cache_key = Some(key.into());
        self
    }

    /// Set what happens to an option the wire or model cannot honour.
    pub fn on_unsupported(mut self, policy: OnUnsupported) -> Self {
        self.on_unsupported = Some(policy);
        self
    }

    /// Every option but the policy, borrowed, as a wire maps it.
    /// Destructures `self` with no `..`, so a new field fails to compile
    /// here first.
    pub fn fields(&self) -> OptionFields<'_> {
        let Self {
            reasoning,
            cache,
            service_tier,
            verbosity,
            parallel_tool_calls,
            top_p,
            seed,
            stop,
            cache_key,
            on_unsupported: _,
        } = self;
        OptionFields {
            reasoning: reasoning.as_ref(),
            cache: cache.as_ref(),
            service_tier: service_tier.as_ref(),
            verbosity: verbosity.as_ref(),
            parallel_tool_calls: *parallel_tool_calls,
            top_p: *top_p,
            seed: *seed,
            stop,
            cache_key: cache_key.as_deref(),
        }
    }

    /// `self` with every field `over` sets put on top: a `Some` option, a
    /// non-empty `stop` list, a set `on_unsupported` (so a run can restore
    /// `Error` over an agent's `Ignore`). Every other
    /// field keeps `self`'s value. An agent's options overlaid with a run's
    /// give the run's where it sets one.
    pub fn overlay(self, over: &GenerationOptions) -> GenerationOptions {
        let GenerationOptions {
            reasoning,
            cache,
            service_tier,
            verbosity,
            parallel_tool_calls,
            top_p,
            seed,
            stop,
            cache_key,
            on_unsupported,
        } = over;
        GenerationOptions {
            reasoning: reasoning.or(self.reasoning),
            cache: cache.or(self.cache),
            service_tier: service_tier.or(self.service_tier),
            verbosity: verbosity.or(self.verbosity),
            parallel_tool_calls: parallel_tool_calls.or(self.parallel_tool_calls),
            top_p: top_p.or(self.top_p),
            seed: seed.or(self.seed),
            stop: if stop.is_empty() {
                self.stop
            } else {
                stop.clone()
            },
            cache_key: cache_key.clone().or(self.cache_key),
            on_unsupported: on_unsupported.or(self.on_unsupported),
        }
    }
}

/// How much the model reasons before answering.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Reasoning {
    /// No reasoning.
    Off,
    /// A portable effort level.
    Effort(Effort),
    /// An explicit reasoning-token budget.
    Budget {
        /// The most tokens the model may spend reasoning.
        tokens: u32,
    },
}

impl From<Effort> for Reasoning {
    fn from(effort: Effort) -> Self {
        Self::Effort(effort)
    }
}

/// A portable reasoning effort level, lowest first.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Effort {
    /// The least reasoning the model offers.
    Minimal,
    /// Low effort.
    Low,
    /// Medium effort.
    Medium,
    /// High effort.
    High,
    /// Extra-high effort.
    XHigh,
    /// The most reasoning the model offers.
    Max,
}

impl Effort {
    /// The level's lower-case wire word, as its serde name spells it:
    /// `"minimal"`, `"low"`, `"medium"`, `"high"`, `"xhigh"` or `"max"`.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Minimal => "minimal",
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
            Self::XHigh => "xhigh",
            Self::Max => "max",
        }
    }
}

/// How long the provider keeps the prompt prefix cached.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CacheRetention {
    /// Do not cache.
    None,
    /// The provider's short retention, typically minutes.
    Short,
    /// The provider's long retention, typically an hour or more.
    Long,
}

/// The processing tier a request asks for.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ServiceTier {
    /// The provider chooses.
    Auto,
    /// The standard tier.
    Default,
    /// Cheaper, slower processing.
    Flex,
    /// Faster, dearer processing.
    Priority,
}

/// How long the answer should be.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Verbosity {
    /// Terse.
    Low,
    /// Balanced.
    Medium,
    /// Detailed.
    High,
}

impl Verbosity {
    /// The level's lower-case wire word: `"low"`, `"medium"` or `"high"`.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
        }
    }
}

/// What happens to an option the wire or model cannot honour.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum OnUnsupported {
    /// The request fails with [`UnsupportedOption`].
    #[default]
    Error,
    /// The option is skipped with a warning.
    Ignore,
}

/// An option the wire or model cannot honour. Carried by
/// [`ProviderError::UnsupportedOption`](crate::error::ProviderError::UnsupportedOption).
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("`{option}` is not supported by {provider} model `{model}`: {reason}")]
pub struct UnsupportedOption {
    /// The [`GenerationOptions`] field name, such as `"reasoning"`, a
    /// provider option's `"<provider>.<section>.<field>"`, such as
    /// `"openrouter.*.provider"`, or the body key a model's catalog entry
    /// refuses, such as `"temperature"` or `"tools"`.
    pub option: Cow<'static, str>,
    /// The provider that refused it.
    pub provider: String,
    /// The model the request resolved to.
    pub model: String,
    /// Why it cannot be honoured.
    pub reason: String,
}

impl UnsupportedOption {
    /// The refusal of `option` by `provider` for `model`, because of `reason`.
    pub fn new(
        option: impl Into<Cow<'static, str>>,
        provider: impl Into<String>,
        model: impl Into<String>,
        reason: impl Into<String>,
    ) -> Self {
        Self {
            option: option.into(),
            provider: provider.into(),
            model: model.into(),
            reason: reason.into(),
        }
    }
}

/// Why a dry run of a completion request failed: what
/// [`DynModel::check`](crate::DynModel::check) returns.
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum CheckError {
    /// Every option the wire or model cannot honour, in the order the wire
    /// meets them. The request builds once they are removed.
    #[error("{}", unsupported_list(.0))]
    Unsupported(Vec<UnsupportedOption>),
    /// The request cannot be built for another reason, such as an empty
    /// history. Refusals met before it are not listed, since the build
    /// stopped there.
    #[error(transparent)]
    Invalid(crate::error::ProviderError),
}

/// The refusals, one per clause.
fn unsupported_list(refusals: &[UnsupportedOption]) -> String {
    refusals
        .iter()
        .map(ToString::to_string)
        .collect::<Vec<_>>()
        .join("; ")
}

#[cfg(test)]
mod tests;