Skip to main content

DecisionRequest

Struct DecisionRequest 

Source
#[non_exhaustive]
pub struct DecisionRequest { pub model: String, pub input: DecisionInput, pub images: Vec<Image>, pub questions: Vec<(String, Question)>, pub options: RequestOptions, }
Expand description

Named questions about one input, answered by a decision model.

Every field is semantic: a provider either sends it as given or fails. There are no hints to degrade, so unlike chat and embedding requests a decision request carries no UnsupportedPolicy.

use allama::DecisionRequest;
use allama::decision::Question;

let request = DecisionRequest::new("nimble", "I was charged twice. Please refund me.")
    .question(
        "label",
        Question::choice("Which label fits this ticket?")
            .option("billing", "Payments and refunds")
            .option("bug", "Software errors"),
    )
    .question("refund", Question::yes_no("Is the customer requesting a refund?"));
assert_eq!(request.questions.len(), 2);

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§model: String

Model identifier.

§input: DecisionInput

What the questions are about.

§images: Vec<Image>

Images the model reads together with the input, in order, for models that report DecisionCapability::ImageInput. Every question is about the input and the images alike.

Image::detail is a hint no decision API honors: adapters warn about it.

§questions: Vec<(String, Question)>

Questions by unique name, in order.

§options: RequestOptions

Provider-specific options.

Implementations§

Source§

impl DecisionRequest

Source

pub fn new(model: impl Into<String>, input: impl Into<DecisionInput>) -> Self

A request for model about input, with no questions yet.

Source

pub fn image(self, image: impl Into<Image>) -> Self

Append an image the model reads together with the input.

use allama::{DecisionRequest, Image};
use allama::decision::Question;

let request = DecisionRequest::new("@cf/cloudflare/clef", "A product photo from a listing.")
    .image(Image::bytes("image/png", png_bytes))
    .question("damaged", Question::yes_no("Does the product look damaged?"));
assert_eq!(request.images.len(), 1);
Source

pub fn question( self, name: impl Into<String>, question: impl Into<Question>, ) -> Self

Append a question named name. Names must be unique.

Source

pub fn questions<I, N, Q>(self, questions: I) -> Self
where I: IntoIterator<Item = (N, Q)>, N: Into<String>, Q: Into<Question>,

Append several named questions.

Source

pub fn find_question(&self, name: &str) -> Option<&Question>

The question named name, if any.

Source

pub fn validate(&self, limits: &DecisionLimits) -> Result<()>

Check the request’s structure and limits before sending it.

Providers call this through validate_decision_request before any I/O.

§Errors

Returns Error::InvalidRequest when:

  • the model, input, a question name, instructions, an option key, a level label, or a description is blank;
  • question names, option keys, or level labels repeat within their scope;
  • a choice has fewer than two options or a score fewer than two levels;
  • the request exceeds limits, its images included.
Source

pub fn with_option<T: ProviderOptions + Serialize>(self, value: T) -> Self

Attach provider options of the exact type.

Source

pub fn with_opaque_option<T: Clone + Send + Sync + 'static>( self, value: T, ) -> Self

Attach an application value that is not a provider option: data a wrapper of yours reads back with option, such as a tenant id or a conversation id. Replaces any earlier value of that type.

The value needs no namespace and no serde: adapters never send it, and it is left out of the request’s serialized form and its equality (an opaque option, RequestOptions::insert_opaque).

Source

pub fn with_options_json( self, namespace: impl Into<String>, value: Value, ) -> Self

Attach provider options in their namespaced JSON form.

Source

pub fn option<T: 'static>(&self) -> Option<&T>

Borrow provider options of the exact type.

Source

pub fn resolve_option<T>(&self) -> Result<T>

Resolve provider options of type T, falling back to the default.

§Errors

See RequestOptions::resolve.

Source

pub fn option_mut<T: Clone + Send + Sync + 'static>(&mut self) -> Option<&mut T>

Mutably borrow provider options of the exact type, when attached.

Like option, the JSON form is not consulted; to change the options whichever form they were given in, use resolve_option_mut.

Source

pub fn resolve_option_mut<T>(&mut self) -> Result<&mut T>

Resolve provider options of type T for changing in place: the typed value, else the JSON form, else T::default(), stored as the typed value. Use it to add to the caller’s options without replacing them, as with_option would.

§Errors

See RequestOptions::resolve_mut.

Trait Implementations§

Source§

impl Clone for DecisionRequest

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DecisionRequest

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for DecisionRequest

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for DecisionRequest

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for DecisionRequest

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for DecisionRequest

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more