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>
impl<'a, L: Llm> PredictModule<'a, L, NoValidation>
Source§impl<'a, L: Llm, V: Validate> PredictModule<'a, L, V>
impl<'a, L: Llm, V: Validate> PredictModule<'a, L, V>
Sourcepub fn validate<V2: Validate>(self, validator: V2) -> PredictModule<'a, L, V2>
pub fn validate<V2: Validate>(self, validator: V2) -> PredictModule<'a, L, V2>
Set a validator, returning a new module with the validator type changed.
Sourcepub fn demo(self, input: impl Into<String>, output: impl Into<String>) -> Self
pub fn demo(self, input: impl Into<String>, output: impl Into<String>) -> Self
Add a few-shot demonstration example.
Sourcepub fn instruction(self, instruction: impl Into<String>) -> Self
pub fn instruction(self, instruction: impl Into<String>) -> Self
Set the instruction.
Sourcepub fn save_composable_state(&self) -> ModuleState<'_>
pub fn save_composable_state(&self) -> ModuleState<'_>
Save composable state (instruction + demos).
Sourcepub fn load_composable_state(&mut self, state: &ModuleState<'_>) -> bool
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>
impl<'a, L, V> ComposableModule for PredictModule<'a, L, V>
Source§type Input<'b> = &'b str
where
Self: 'b
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
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
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>
fn forward<'b>(&'b self, input: Self::Input<'b>) -> Self::ForwardFut<'b>
Execute the module on the given input.
Source§fn save_state(&self) -> ModuleState<'_>
fn save_state(&self) -> ModuleState<'_>
Snapshot the module’s learnable state (instruction, demos, children).
Source§fn load_state(&mut self, state: &ModuleState<'_>) -> bool
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>where
V: RefUnwindSafe,
L: RefUnwindSafe,
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>where
V: UnwindSafe,
L: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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