semantic-operators 0.1.0-alpha.2

Provider-neutral semantic operators with typed, confidence-aware answers
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
//! Validated provider-independent questions and regions.
use crate::{AnswerType, Error, ErrorKind};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
fn invalid(message: &str) -> Error {
    Error::new(ErrorKind::InvalidQuestion, message)
}
fn instructions(value: String) -> Result<String, Error> {
    if is_blank(&value) {
        Err(invalid("instructions must be a non-empty string"))
    } else {
        Ok(value)
    }
}
fn confidence(value: f64) -> Result<f64, Error> {
    if value.is_finite() && (0.0..=1.0).contains(&value) {
        Ok(value)
    } else {
        Err(invalid(
            "min_confidence must be a finite number from 0 to 1",
        ))
    }
}
fn labels<'a>(values: impl IntoIterator<Item = &'a str>) -> Result<(), Error> {
    let values: Vec<_> = values.into_iter().collect();
    if values.len() < 2 {
        return Err(invalid("need at least 2 outcomes"));
    }
    let mut seen = std::collections::HashSet::new();
    if values.iter().any(|v| v.is_empty() || !seen.insert(*v)) {
        return Err(invalid("outcomes must be unique non-empty strings"));
    }
    Ok(())
}
/// A Boolean question with optional descriptions of each outcome.
#[derive(Clone, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "BooleanFields")]
pub struct Boolean {
    instructions: String,
    min_confidence: f64,
    true_description: Option<String>,
    false_description: Option<String>,
}
impl Boolean {
    /// Validate instructions and construct a question without a confidence threshold.
    ///
    /// # Errors
    /// Returns `InvalidQuestion` when instructions are empty or contain only
    /// reference-defined whitespace.
    pub fn new(text: impl Into<String>) -> Result<Self, Error> {
        Ok(Self {
            instructions: instructions(text.into())?,
            min_confidence: 0.0,
            true_description: None,
            false_description: None,
        })
    }
    /// Set descriptions for true and false, respectively.
    pub fn with_descriptions(mut self, yes: Option<String>, no: Option<String>) -> Self {
        self.true_description = yes;
        self.false_description = no;
        self
    }
    /// Description of the true outcome.
    pub fn true_description(&self) -> Option<&str> {
        self.true_description.as_deref()
    }
    /// Description of the false outcome.
    pub fn false_description(&self) -> Option<&str> {
        self.false_description.as_deref()
    }
}
/// A choice among ordered named outcomes.
#[derive(Clone, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "ChoiceFields")]
pub struct Choice {
    instructions: String,
    min_confidence: f64,
    options: IndexMap<String, Option<String>>,
}
impl Choice {
    /// Validate instructions and at least two distinct nonempty option names.
    ///
    /// # Errors
    /// Returns `InvalidQuestion` for blank instructions, fewer than two options,
    /// or an empty option name. The ordered map supplies unique names.
    pub fn new(
        text: impl Into<String>,
        options: IndexMap<String, Option<String>>,
    ) -> Result<Self, Error> {
        labels(options.keys().map(String::as_str))?;
        Ok(Self {
            instructions: instructions(text.into())?,
            min_confidence: 0.0,
            options,
        })
    }
    /// Ordered option names and descriptions.
    pub fn options(&self) -> &IndexMap<String, Option<String>> {
        &self.options
    }
}
/// An expected score on an ordered rubric.
#[derive(Clone, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "ScoreFields")]
pub struct Score {
    instructions: String,
    min_confidence: f64,
    levels: Vec<String>,
}
impl Score {
    /// Validate instructions and at least two unique nonempty levels.
    ///
    /// # Errors
    /// Returns `InvalidQuestion` for blank instructions, fewer than two levels,
    /// or empty or duplicate level descriptions.
    pub fn new(text: impl Into<String>, levels: Vec<String>) -> Result<Self, Error> {
        labels(levels.iter().map(String::as_str))?;
        Ok(Self {
            instructions: instructions(text.into())?,
            min_confidence: 0.0,
            levels,
        })
    }
    /// Rubric levels in score order, starting at zero.
    pub fn levels(&self) -> &[String] {
        &self.levels
    }
}
/// A request to locate a region in an image.
#[derive(Clone, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "BoxQuestionFields")]
pub struct BoxQuestion {
    instructions: String,
    min_confidence: f64,
}
impl BoxQuestion {
    /// Construct a localization question.
    ///
    /// # Errors
    /// Returns `InvalidQuestion` when instructions are empty or contain only
    /// reference-defined whitespace.
    pub fn new(text: impl Into<String>) -> Result<Self, Error> {
        Ok(Self {
            instructions: instructions(text.into())?,
            min_confidence: 0.0,
        })
    }
}
macro_rules! common { ($($ty:ty),*) => {$(
    impl $ty {
        /// Original instructions, preserved without trimming.
        pub fn instructions(&self)->&str { &self.instructions }
        /// Minimum confidence required for a decision.
        pub fn min_confidence(&self)->f64 { self.min_confidence }
        /// Set a finite confidence threshold in [0, 1].
        ///
        /// # Errors
        /// Returns `InvalidQuestion` if the threshold is nonfinite or outside `[0, 1]`.
        pub fn with_min_confidence(mut self, value:f64)->Result<Self,Error> { self.min_confidence=confidence(value)?; Ok(self) }
    }
)*}; }
common!(Boolean, Choice, Score, BoxQuestion);
/// A heterogeneous question.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum Question {
    /// A yes/no question.
    Boolean(Boolean),
    /// An ordered choice.
    Choice(Choice),
    /// An expected rubric score.
    Score(Score),
    /// A localization question.
    Box(BoxQuestion),
}
impl Question {
    /// Original instructions.
    pub fn instructions(&self) -> &str {
        match self {
            Self::Boolean(q) => q.instructions(),
            Self::Choice(q) => q.instructions(),
            Self::Score(q) => q.instructions(),
            Self::Box(q) => q.instructions(),
        }
    }
    /// Decision threshold.
    pub fn min_confidence(&self) -> f64 {
        match self {
            Self::Boolean(q) => q.min_confidence(),
            Self::Choice(q) => q.min_confidence(),
            Self::Score(q) => q.min_confidence(),
            Self::Box(q) => q.min_confidence(),
        }
    }
    /// Schema name of this question type.
    pub fn kind(&self) -> &'static str {
        match self {
            Self::Boolean(_) => "boolean",
            Self::Choice(_) => "choice",
            Self::Score(_) => "score",
            Self::Box(_) => "box",
        }
    }
}
/// A question whose output has a statically known type.
pub trait TypedQuestion: Clone + Into<Question> {
    /// Type of a decided answer.
    type Output: AnswerType;
}
macro_rules! typed {
    ($ty:ty,$variant:ident,$output:ty) => {
        impl From<$ty> for Question {
            fn from(q: $ty) -> Self {
                Self::$variant(q)
            }
        }
        impl TypedQuestion for $ty {
            type Output = $output;
        }
    };
}
typed!(Boolean, Boolean, bool);
typed!(Choice, Choice, String);
typed!(Score, Score, f64);
typed!(BoxQuestion, Box, Region);
/// Normalized image edges. Zero-area regions are valid.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "RegionFields")]
pub struct Region {
    top: f64,
    bottom: f64,
    left: f64,
    right: f64,
}
impl Region {
    /// Validate finite edges in [0, 1], with top <= bottom and left <= right.
    ///
    /// # Errors
    /// Returns `InvalidArgument` if an edge is nonfinite or outside `[0, 1]`,
    /// if top exceeds bottom, or if left exceeds right. Zero-area regions are valid.
    pub fn new(top: f64, bottom: f64, left: f64, right: f64) -> Result<Self, Error> {
        if [top, bottom, left, right]
            .iter()
            .any(|v| !v.is_finite() || !(0.0..=1.0).contains(v))
            || top > bottom
            || left > right
        {
            return Err(Error::new(
                ErrorKind::InvalidArgument,
                "region edges must be ordered finite numbers from 0 to 1",
            ));
        }
        Ok(Self {
            top,
            bottom,
            left,
            right,
        })
    }
    /// Top edge.
    pub fn top(&self) -> f64 {
        self.top
    }
    /// Bottom edge.
    pub fn bottom(&self) -> f64 {
        self.bottom
    }
    /// Left edge.
    pub fn left(&self) -> f64 {
        self.left
    }
    /// Right edge.
    pub fn right(&self) -> f64 {
        self.right
    }
    /// Normalized width.
    pub fn width(&self) -> f64 {
        self.right - self.left
    }
    /// Normalized height.
    pub fn height(&self) -> f64 {
        self.bottom - self.top
    }
    /// Center as (x, y).
    pub fn center(&self) -> (f64, f64) {
        (
            (self.left + self.right) / 2.0,
            (self.top + self.bottom) / 2.0,
        )
    }
    /// Grow each side by a fraction of the region size, clipping to the image.
    ///
    /// # Errors
    /// Returns `InvalidArgument` for nonfinite padding or resulting invalid
    /// edges, such as negative padding that shrinks past the opposite edge.
    pub fn padded(&self, fraction: f64) -> Result<Self, Error> {
        if !fraction.is_finite() {
            return Err(Error::new(
                ErrorKind::InvalidArgument,
                "padding must be finite",
            ));
        }
        let dx = self.width() * fraction;
        let dy = self.height() * fraction;
        Self::new(
            (self.top - dy).max(0.0),
            (self.bottom + dy).min(1.0),
            (self.left - dx).max(0.0),
            (self.right + dx).min(1.0),
        )
    }
    /// Pixel edges (left, top, right, bottom), using ties-to-even rounding.
    ///
    /// ```
    /// use semantic_operators::Region;
    /// # fn main() -> semantic_operators::Result<()> {
    /// let region = Region::new(0.2, 0.6, 0.1, 0.5)?;
    /// assert_eq!(region.pixels(1000, 500)?, (100, 100, 500, 300));
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    /// Returns `InvalidArgument` if either pixel dimension is zero.
    pub fn pixels(&self, width: u32, height: u32) -> Result<(u32, u32, u32, u32), Error> {
        if width == 0 || height == 0 {
            return Err(Error::new(
                ErrorKind::InvalidArgument,
                "pixel dimensions must be positive",
            ));
        }
        Ok((
            (self.left * f64::from(width)).round_ties_even() as u32,
            (self.top * f64::from(height)).round_ties_even() as u32,
            (self.right * f64::from(width)).round_ties_even() as u32,
            (self.bottom * f64::from(height)).round_ties_even() as u32,
        ))
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct BooleanFields {
    instructions: String,
    #[serde(default)]
    min_confidence: f64,
    #[serde(default)]
    true_description: Option<String>,
    #[serde(default)]
    false_description: Option<String>,
}
impl TryFrom<BooleanFields> for Boolean {
    type Error = Error;
    fn try_from(v: BooleanFields) -> Result<Self, Error> {
        Self::new(v.instructions)?
            .with_descriptions(v.true_description, v.false_description)
            .with_min_confidence(v.min_confidence)
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ChoiceFields {
    instructions: String,
    #[serde(default)]
    min_confidence: f64,
    options: IndexMap<String, Option<String>>,
}
impl TryFrom<ChoiceFields> for Choice {
    type Error = Error;
    fn try_from(v: ChoiceFields) -> Result<Self, Error> {
        Self::new(v.instructions, v.options)?.with_min_confidence(v.min_confidence)
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ScoreFields {
    instructions: String,
    #[serde(default)]
    min_confidence: f64,
    levels: Vec<String>,
}
impl TryFrom<ScoreFields> for Score {
    type Error = Error;
    fn try_from(v: ScoreFields) -> Result<Self, Error> {
        Self::new(v.instructions, v.levels)?.with_min_confidence(v.min_confidence)
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct BoxQuestionFields {
    instructions: String,
    #[serde(default)]
    min_confidence: f64,
}
impl TryFrom<BoxQuestionFields> for BoxQuestion {
    type Error = Error;
    fn try_from(v: BoxQuestionFields) -> Result<Self, Error> {
        Self::new(v.instructions)?.with_min_confidence(v.min_confidence)
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RegionFields {
    top: f64,
    bottom: f64,
    left: f64,
    right: f64,
}
impl TryFrom<RegionFields> for Region {
    type Error = Error;
    fn try_from(v: RegionFields) -> Result<Self, Error> {
        Self::new(v.top, v.bottom, v.left, v.right)
    }
}

macro_rules! private_debug {
    ($($ty:ty),*) => {$(
        impl std::fmt::Debug for $ty {
            fn fmt(&self, f:&mut std::fmt::Formatter<'_>)->std::fmt::Result {
                f.debug_struct(stringify!($ty)).field("min_confidence",&self.min_confidence).finish_non_exhaustive()
            }
        }
    )*};
}
private_debug!(Boolean, Choice, Score, BoxQuestion);

// Python str.strip includes the four ASCII information separators in addition
// to Unicode White_Space; Rust str::trim does not.
pub(crate) fn is_blank(value: &str) -> bool {
    value
        .chars()
        .all(|c| c.is_whitespace() || ('\u{1c}'..='\u{1f}').contains(&c))
}