Skip to main content

PredictModule

Struct PredictModule 

Source
pub struct PredictModule<'a, L: Llm, V: Validate = NoValidation> { /* private fields */ }
Expand description

A simple composable module that wraps an LLM call with optional demos.

PredictModule implements ComposableModule with:

  • Input: &'a str (the prompt)
  • Output: String (the LLM response)

It stores an instruction, optional validator, and few-shot demos that are prepended to the prompt.

§Examples

use kkachi::composable::PredictModule;
use kkachi::recursive::{MockLlm, NoValidation};

let llm = MockLlm::new(|p, _| format!("response to: {}", p));
let module = PredictModule::new("predict", &llm, "Answer the question.");

// Use as a ComposableModule
let output = module.forward("What is 2+2?").await.unwrap();
assert!(output.contains("response to:"));

Implementations§

Source§

impl<'a, L: Llm> PredictModule<'a, L, NoValidation>

Source

pub fn new(name: &'a str, llm: &'a L, instruction: impl Into<String>) -> Self

Create a new predict module with no validator.

Source§

impl<'a, L: Llm, V: Validate> PredictModule<'a, L, V>

Source

pub fn validate<V2: Validate>(self, validator: V2) -> PredictModule<'a, L, V2>

Set a validator, returning a new module with the validator type changed.

Source

pub fn demo(self, input: impl Into<String>, output: impl Into<String>) -> Self

Add a few-shot demonstration example.

Source

pub fn instruction(self, instruction: impl Into<String>) -> Self

Set the instruction.

Source

pub fn save_composable_state(&self) -> ModuleState<'_>

Save composable state (instruction + demos).

Source

pub fn load_composable_state(&mut self, state: &ModuleState<'_>) -> bool

Load composable state (instruction + demos).

Returns true if the state name matched and was applied.

Trait Implementations§

Source§

impl<'a, L, V> ComposableModule for PredictModule<'a, L, V>
where L: Llm + 'static, V: Validate + 'static,

Source§

type Input<'b> = &'b str where Self: 'b

Input type for this module. The lifetime allows borrowing from the caller.
Source§

type Output<'b> = String where Self: 'b

Output type for this module. The lifetime allows borrowing from self.
Source§

type ForwardFut<'b> = Pin<Box<dyn Future<Output = Result<String, Error>> + Send + 'b>> where Self: 'b

Future returned by forward.
Source§

fn forward<'b>(&'b self, input: Self::Input<'b>) -> Self::ForwardFut<'b>

Execute the module on the given input.
Source§

fn name(&self) -> &str

Human-readable name for this module (used in state trees and logging).
Source§

fn save_state(&self) -> ModuleState<'_>

Snapshot the module’s learnable state (instruction, demos, children).
Source§

fn load_state(&mut self, state: &ModuleState<'_>) -> bool

Restore learnable state from a snapshot. Read more

Auto Trait Implementations§

§

impl<'a, L, V> Freeze for PredictModule<'a, L, V>
where V: Freeze,

§

impl<'a, L, V> RefUnwindSafe for PredictModule<'a, L, V>

§

impl<'a, L, V> Send for PredictModule<'a, L, V>

§

impl<'a, L, V> Sync for PredictModule<'a, L, V>

§

impl<'a, L, V> Unpin for PredictModule<'a, L, V>
where V: Unpin,

§

impl<'a, L, V> UnsafeUnpin for PredictModule<'a, L, V>
where V: UnsafeUnpin,

§

impl<'a, L, V> UnwindSafe for PredictModule<'a, L, V>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.