allama 0.2.0

Low-level, provider-agnostic LLM abstractions for Rust
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! What an extraction can know before it sends ([`ExtractionPlan`]), the
//! enforcement floor, and how enforcement is reported in terms of the
//! caller's type.

use std::borrow::Cow;

use schemars::JsonSchema;
use serde_json::Value;

use super::{
    AutoFallback, EXTRACTION_TOOL, ExtractError, ExtractFailure, ExtractionMetadata,
    ExtractionMode, Extractor, SchemaEnforcement, counted_field, derive_request, is_about,
    requested_mode, schema_name, select_mode,
};
use crate::{
    ChatProvider, ChatRequest, Error, Message, RequestField, Result, ValidationCoverage, Warning,
    WarningKind,
};

/// What an extraction of `T` would do, found without sending anything
/// ([`Extractor::plan`]).
///
/// ```
/// # #[cfg(feature = "mock")]
/// # fn example() -> allama::Result<()> {
/// use allama::ChatRequest;
/// use allama::extract::{ExtractionMode, Extractor, SchemaEnforcement};
/// use allama::mock::MockProvider;
/// use allama::schemars::{self, JsonSchema};
///
/// #[derive(serde::Deserialize, JsonSchema)]
/// struct Person {
///     name: String,
/// }
///
/// let provider = MockProvider::with_text("{}");
/// let request = ChatRequest::new("m").user("Ada").with_option(ExtractionMode::Native);
/// let plan = Extractor::new().plan::<Person>(&provider, &request)?;
/// assert_eq!(plan.mode, ExtractionMode::Native);
/// assert_eq!(plan.enforcement, SchemaEnforcement::Exact);
/// # Ok(()) }
/// ```
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ExtractionPlan {
    /// The mode the extraction would use: `ForcedTool` after an
    /// [automatic fallback](super#automatic-fallback-to-forced-tool-mode).
    pub mode: ExtractionMode,
    /// The enforcement the first pass would report, from the provider's
    /// validation. With [`ValidationCoverage::Capabilities`] the adapter's
    /// encoding did not run, so this is an upper bound: an adapter reports
    /// most degradations while encoding.
    pub enforcement: SchemaEnforcement,
    /// The warnings that decide [`enforcement`](Self::enforcement), in terms
    /// of the caller's type, as [`ExtractionMetadata::degradations`] would
    /// hold them.
    pub degradations: Vec<Warning>,
    /// The refusal that would switch an `Auto` extraction to forced-tool
    /// mode, when it would.
    pub auto_fallback: Option<AutoFallback>,
    /// How much of the call the provider's validation covered.
    pub coverage: ValidationCoverage,
}

impl Extractor {
    /// Plan an extraction of `T` from `request` without sending it: the mode
    /// it would use, the enforcement its first pass would report, and how
    /// much of that the provider could check.
    ///
    /// It runs the provider's validation
    /// ([`validate_chat_request`](ChatProvider::validate_chat_request)) on a
    /// schema-only copy of the extraction request: the same model, options,
    /// tools, response format and policy, with one placeholder message in
    /// place of the conversation, so the plan does not depend on what the
    /// conversation holds. Every first-party adapter validates with a dry
    /// run of the call ([`ValidationCoverage::Encoding`]).
    ///
    /// # Errors
    ///
    /// What the call would fail with before sending: a refusal under
    /// [`UnsupportedPolicy::Error`](crate::UnsupportedPolicy::Error), in
    /// terms of `T`, or an [`Error::Extract`] when no mode can be used.
    pub fn plan<T: JsonSchema>(
        &self,
        provider: &(impl ChatProvider + ?Sized),
        request: &ChatRequest,
    ) -> Result<ExtractionPlan> {
        Target::new::<T>(provider.provider_name(), &request.model).plan(provider, request, false)
    }

    /// Check the floor before sending, when the provider's validation is a
    /// dry run of the call.
    pub(super) fn check_floor_before_sending<P: ChatProvider + ?Sized>(
        &self,
        provider: &P,
        request: &ChatRequest,
        target: &Target,
        stream: bool,
    ) -> Result<()> {
        let Some(floor) = self.min_enforcement else {
            return Ok(());
        };
        let plan = target.plan(provider, request, stream)?;
        if plan.coverage != ValidationCoverage::Encoding || plan.enforcement.satisfies(floor) {
            return Ok(());
        }
        let mut metadata = ExtractionMetadata::new(plan.mode);
        metadata.auto_fallback = plan.auto_fallback;
        Err(ExtractFailure::new(
            ExtractError::EnforcementBelow {
                required: floor,
                achieved: plan.enforcement,
                reason: reason(&plan.degradations),
            },
            metadata,
        )
        .into())
    }

    /// The failure for a pass whose enforcement is below the floor.
    pub(super) fn below_floor(&self, metadata: &ExtractionMetadata) -> Option<ExtractFailure> {
        let floor = self.min_enforcement?;
        (!metadata.enforcement.satisfies(floor)).then(|| {
            ExtractFailure::new(
                ExtractError::EnforcementBelow {
                    required: floor,
                    achieved: metadata.enforcement,
                    reason: reason(&metadata.degradations),
                },
                metadata.clone(),
            )
        })
    }

    /// Whether a pass with `metadata`'s enforcement is checked for
    /// undeclared keys: every pass that is not `Exact`, unless allowed.
    pub(super) fn rejects_undeclared(&self, metadata: &ExtractionMetadata) -> bool {
        !self.allow_undeclared_keys && metadata.enforcement != SchemaEnforcement::Exact
    }
}

/// Why an enforcement is what it is: the counted degradations' messages.
fn reason(degradations: &[Warning]) -> String {
    if degradations.is_empty() {
        return "no pass was made".to_owned();
    }
    degradations
        .iter()
        .map(|w| w.message.as_str())
        .collect::<Vec<_>>()
        .join("; ")
}

/// What the degradations say the provider lacks, for the enforcement
/// warning: the limitation of each message (before its first `"; "`), so
/// the warning keeps one shape (`<limitation>; <outcome>`).
fn limitations(degradations: &[Warning]) -> String {
    if degradations.is_empty() {
        return "no pass was made".to_owned();
    }
    degradations
        .iter()
        .map(|w| {
            w.message
                .split_once("; ")
                .map_or(w.message.as_str(), |(l, _)| l)
        })
        .collect::<Vec<_>>()
        .join(", and ")
}

/// The type an extraction targets: its schema and how messages name it.
#[derive(Debug, Clone)]
pub(super) struct Target {
    /// The provider name.
    pub(super) provider: String,
    /// The model.
    pub(super) model: String,
    /// How messages name the type: the schema's `title` (the Rust type
    /// name, for a derived schema).
    pub(super) label: String,
    /// The schema name sent with the response format.
    pub(super) name: String,
    /// The schema of `T`.
    pub(super) schema: Value,
}

impl Target {
    pub(super) fn new<T: JsonSchema>(provider: &str, model: &str) -> Self {
        let schema = crate::schema::schema_for::<T>();
        let label = schema
            .get("title")
            .and_then(Value::as_str)
            .map(str::to_owned)
            .unwrap_or_else(|| short_type_name(std::any::type_name::<T>()));
        Self {
            provider: provider.to_owned(),
            model: model.to_owned(),
            label,
            name: schema_name::<T>(&schema),
            schema,
        }
    }

    /// The request a pass in `metadata`'s mode sends.
    pub(super) fn derive(
        &self,
        request: &ChatRequest,
        metadata: &ExtractionMetadata,
    ) -> Result<ChatRequest> {
        derive_request(
            request,
            metadata.mode,
            &self.provider,
            &self.name,
            self.schema.clone(),
        )
        .map_err(|error| ExtractFailure::new(error, metadata.clone()).into())
    }

    /// The error a call that failed returns: a refusal before sending in
    /// terms of the caller's type, and in `Auto` mode after a fallback, the
    /// native refusal as the primary cause. Any other error is returned as
    /// itself.
    pub(super) fn pre_send_error(&self, error: Error, metadata: &ExtractionMetadata) -> Error {
        if error.unsupported_stage() != Some(crate::UnsupportedStage::BeforeSending) {
            return metadata.attach_to(error);
        }
        match &metadata.auto_fallback {
            Some(fallback) if metadata.passes == 0 => {
                let forced = if error.message_origin() == crate::MessageOrigin::Authored {
                    format!(
                        "was refused too ({})",
                        refusal_in_caller_terms(&error.message(), &self.label)
                    )
                } else {
                    "was refused too".to_owned()
                };
                metadata.attach_to(self.auto_refused(fallback, &forced))
            }
            _ => metadata.attach_to(self.in_caller_terms(error)),
        }
    }

    /// An `Unsupported` refusal reworded in terms of the caller's type.
    fn in_caller_terms(&self, mut error: Error) -> Error {
        if let Error::Unsupported { message, .. } = &mut error {
            *message = refusal_in_caller_terms(message, &self.label);
        }
        error
    }

    /// An `Auto` extraction whose native output was refused (`fallback`, the
    /// primary cause, so the error names the response format) and whose
    /// forced-tool attempt `forced`.
    fn auto_refused(&self, fallback: &AutoFallback, forced: &str) -> Error {
        let native = if fallback.message.is_empty() {
            String::new()
        } else {
            format!(
                " ({})",
                refusal_in_caller_terms(&fallback.message, &self.label)
            )
        };
        Error::unsupported_before_sending(
            format!(
                "`{}` cannot be extracted from `{}` with its output constrained: native output was refused{native}, and forced-tool mode {forced}",
                self.label, self.model
            ),
            Some(crate::Subject::Field {
                field: RequestField::ResponseFormat,
            }),
        )
        .authored()
        .with_provider(self.provider.clone())
    }

    /// Emit the enforcement of a pass that is not `Exact` as a warning
    /// event in terms of the caller's type, coded per type so it is logged
    /// at `WARN` once per provider, model and type.
    pub(super) fn report_enforcement(&self, metadata: &ExtractionMetadata) {
        let (kind, how) = match metadata.enforcement {
            SchemaEnforcement::Exact => return,
            SchemaEnforcement::Approximate => (WarningKind::Approximated, "approximately"),
            _ => (WarningKind::Unsupported, "without constraining the output"),
        };
        let Some(field) = counted_field(metadata.mode) else {
            return;
        };
        let mut warning = Warning::new(
            kind,
            crate::Subject::Field { field },
            format!(
                "{}; `{}` is extracted from `{}` {how} (`{}` mode)",
                limitations(&metadata.degradations),
                self.label,
                self.model,
                metadata.mode,
            ),
        )
        .with_provider(self.provider.clone());
        warning.code = Some(Cow::Owned(format!(
            "extraction.{}.{}",
            metadata.enforcement, self.label
        )));
        crate::warning::emit(&self.provider, &self.model, &mut warning);
    }

    /// See [`Extractor::plan`].
    pub(super) fn plan<P: ChatProvider + ?Sized>(
        &self,
        provider: &P,
        request: &ChatRequest,
        stream: bool,
    ) -> Result<ExtractionPlan> {
        let requested = requested_mode(request);
        let (mode, guessed) = select_mode(provider, request);
        let mut metadata = ExtractionMetadata::new(mode);
        let mut request = request.clone();
        request.system.clear();
        request.messages = vec![Message::user("placeholder")];
        loop {
            let derived = self.derive(&request, &metadata)?;
            let validation = if stream {
                provider.validate_chat_stream_request(&derived)
            } else {
                provider.validate_chat_request(&derived)
            };
            match validation {
                Ok(validation) => {
                    let field = counted_field(metadata.mode);
                    let mut degradations: Vec<Warning> = Vec::new();
                    for warning in validation
                        .warnings()
                        .iter()
                        .filter(|w| field.is_some_and(|field| is_about(w, field)))
                    {
                        let warning = in_caller_terms(warning, &self.label);
                        if !degradations.contains(&warning) {
                            degradations.push(warning);
                        }
                    }
                    return Ok(ExtractionPlan {
                        mode: metadata.mode,
                        enforcement: SchemaEnforcement::from_degradations(&degradations),
                        degradations,
                        auto_fallback: metadata.auto_fallback,
                        coverage: validation.coverage(),
                    });
                }
                Err(error) => {
                    let Some(fallback) = metadata.auto_fallback_for(requested, guessed, &error)
                    else {
                        return Err(self.pre_send_error(error, &metadata));
                    };
                    metadata.fall_back(fallback);
                }
            }
        }
    }
}

/// `warning` reworded in terms of the caller's type, `label`.
pub(super) fn in_caller_terms(warning: &Warning, label: &str) -> Warning {
    let mut warning = warning.clone();
    warning.message = message_in_caller_terms(&warning.message, label);
    warning
}

/// A refusal's message in terms of the caller's type. A refusal leaves a
/// hint's outcome out (`Degradations`), so it never says the schema "was
/// sent without `strict`".
fn refusal_in_caller_terms(message: &str, label: &str) -> String {
    message_in_caller_terms(message, label)
}

/// `message` with the synthetic tool and the response schema named as the
/// caller's type, and JSON pointers into the schema written as field paths
/// (`/properties/attributes` as `Order.attributes`).
fn message_in_caller_terms(message: &str, label: &str) -> String {
    let schema = format!("the schema of `{label}`");
    let message = message
        .replace(&format!("tool `{EXTRACTION_TOOL}`"), &schema)
        .replace("the response schema", &schema)
        .replace(&format!("`{EXTRACTION_TOOL}`"), &format!("`{label}`"));
    let mut out = String::with_capacity(message.len());
    let mut rest = message.as_str();
    while let Some(start) = rest.find("`/") {
        let Some(len) = rest[start + 1..].find('`') else {
            break;
        };
        let pointer = &rest[start + 1..start + 1 + len];
        out.push_str(&rest[..start]);
        match field_path(label, pointer) {
            Some(path) => out.push_str(&format!("`{path}`")),
            None => out.push_str(&format!("`{pointer}`")),
        }
        rest = &rest[start + len + 2..];
    }
    out.push_str(rest);
    out
}

/// The field path a JSON pointer into the schema of `label` names, such as
/// `Order.items[].price`; `None` when it goes through a keyword that has no
/// field-path form.
fn field_path(label: &str, pointer: &str) -> Option<String> {
    let mut base = label.to_owned();
    let mut path = String::new();
    let mut segments = pointer
        .split('/')
        .skip(1)
        .filter(|s| !s.is_empty())
        .map(|s| s.replace("~1", "/").replace("~0", "~"));
    while let Some(segment) = segments.next() {
        match segment.as_str() {
            "properties" => path.push_str(&format!(".{}", segments.next()?)),
            "items" | "additionalItems" => path.push_str("[]"),
            "prefixItems" => path.push_str(&format!("[{}]", segments.next()?)),
            "additionalProperties" => path.push_str("[..]"),
            "$defs" | "definitions" => {
                base = segments.next()?;
                path.clear();
            }
            "anyOf" | "oneOf" | "allOf" => {
                segments.next()?;
            }
            _ => return None,
        }
    }
    Some(format!("{base}{path}"))
}

/// The last path segment of a type name, generics included:
/// `Wrapped<Inner>` for `my::Wrapped<my::Inner>`.
fn short_type_name(name: &str) -> String {
    let outer = name.split('<').next().unwrap_or(name);
    let short = outer.rsplit("::").next().unwrap_or(outer);
    match name.find('<') {
        Some(at) => format!("{short}{}", &name[at..]),
        None => short.to_owned(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn messages_name_the_callers_type() {
        assert_eq!(
            message_in_caller_terms(
                "tool `submit_structured_output` is not strict-representable: the subschema at \
                 `/properties/attributes` is a map; it was sent without `strict`",
                "Order"
            ),
            "the schema of `Order` is not strict-representable: the subschema at \
             `Order.attributes` is a map; it was sent without `strict`"
        );
        assert_eq!(
            refusal_in_caller_terms(
                "cannot send `response_format`: the response schema is not strict-representable: \
                 the subschema at `/$defs/Item/properties/tags/items` uses `not`",
                "Order"
            ),
            "cannot send `response_format`: the schema of `Order` is not strict-representable: \
             the subschema at `Item.tags[]` uses `not`"
        );
        // Pointers through other keywords, and unterminated ticks, stay.
        assert_eq!(
            message_in_caller_terms("at `/patternProperties/x` and `/", "T"),
            "at `/patternProperties/x` and `/"
        );
        assert_eq!(field_path("T", "/"), Some("T".into()));
        assert_eq!(
            field_path("T", "/properties/a~1b/anyOf/0/properties/c"),
            Some("T.a/b.c".into())
        );
    }

    #[test]
    fn short_type_names_keep_generics() {
        assert_eq!(short_type_name("my::Person"), "Person");
        assert_eq!(short_type_name("a::Wrapped<b::Inner>"), "Wrapped<b::Inner>");
    }
}