Task

Struct Task 

Source
pub struct Task<M: CreateChatSession, Constraints = NoConstraints> { /* private fields */ }
Expand description

A task session lets you efficiently run a task with a model. The task session will reuse the model’s cache to avoid re-feeding the task prompt repeatedly.

§Example

use kalosm::language::*;

#[tokio::main]
async fn main() {
    let mut llm = Llama::new_chat().await.unwrap();
    let mut task = llm.task("You are a math assistant who helps students with their homework. You solve equations and answer questions. When solving problems, you will always solve problems step by step.");

    println!("question 1");
    // The first time we use the task, it will load the model and prompt.
    task.run("What is 2 + 2?")
        .to_std_out()
        .await
        .unwrap();
     
    println!("question 2");
    // After the first time, the model and prompt are cached.
    task.run("What is 4 + 4?")
        .to_std_out()
        .await
        .unwrap();
}

Implementations§

Source§

impl<M: CreateChatSession> Task<M>

Source

pub fn new(model: M, description: impl ToString) -> Self

Create a new task with no constraints and the default sampler.

Source

pub fn from_chat(chat: Chat<M>) -> Self

Create a new task from an existing chat session.

Source§

impl<M: CreateChatSession, Constraints> Task<M, Constraints>

Source

pub fn with_example(self, input: impl ToString, output: impl ToString) -> Self

Add an example to the task. Examples help the model perform better by allowing it to mimic the format of the examples.

§Example
use kalosm::language::*;

#[tokio::main]
async fn main() {
    let model = Llama::new_chat().await.unwrap();
    let task = model.task("You are a math assistant who helps students with their homework. You solve equations and answer questions. When solving problems, you will always solve problems step by step.")
        .with_example("What is 1 + 2?", "Step 1: 1 + 2 = 3\nOutput: 3");
    let mut stream = task("What is 2 + 2?");
    stream.to_std_out().await.unwrap();
}
Source

pub fn with_examples( self, examples: impl IntoIterator<Item = (impl ToString, impl ToString)>, ) -> Self

Add multiple examples to the task. Examples help the model perform better by allowing it to mimic the format of the examples.

§Example
use kalosm::language::*;

#[tokio::main]
async fn main() {
    let model = Llama::new_chat().await.unwrap();
    let task = model.task("You are a math assistant who helps students with their homework. You solve equations and answer questions. When solving problems, you will always solve problems step by step.")
        .with_examples([
            ("What is 1 + 2?", "Step 1: 1 + 2 = 3\nOutput: 3"),
            ("What is 3 + 4?", "Step 1: 3 + 4 = 7\nOutput: 7"),
            ("What is (4 + 8) / 3?", "Step 1: 4 + 8 = 12\nStep 2: 12 / 3 = 4\nOutput: 4"),
        ]);
    let mut stream = task("What is 3 + 4?");
    stream.to_std_out().await.unwrap();
}
Source

pub fn with_constraints<NewConstraints>( self, constraints: NewConstraints, ) -> Task<M, NewConstraints>

Set the constraints for the task. The constraints force the format of all outputs of the task to fit the constraints. This can be used to make the model return a specific type. This method does the same thing as ChatResponseBuilder::with_constraints except it is called once on the task instead of any time you run the task.

§Example
use kalosm::language::*;
use std::sync::Arc;

#[tokio::main]
async fn main() {
    let model = Llama::new_chat().await.unwrap();
    let task = model
        .task("You are a math assistant. Respond with just the number answer and nothing else.")
        .with_constraints(Arc::new(i32::new_parser()));
    let mut stream = task("What is 2 + 2?");
    stream.to_std_out().await.unwrap();
    let result: i32 = stream.await.unwrap();
    println!("{result}");
}
Source

pub fn typed<T>( self, ) -> Task<M, <M as CreateDefaultChatConstraintsForType<T>>::DefaultConstraints>

Create a task with the default constraints for the given type. This is the same as calling Task::with_constraints with the default constraints for the given type.

§Example
use kalosm::language::*;

#[tokio::main]
async fn main() {
    let model = Llama::new_chat().await.unwrap();
    let task = model
        .task("You are a math assistant. Respond with just the number answer and nothing else.")
        .typed();
    let mut stream = task("What is 2 + 2?");
    stream.to_std_out().await.unwrap();
    let result: i32 = stream.await.unwrap();
    println!("{result}");
}
Source

pub fn chat(&self) -> &Chat<M>

Get a reference to the underlying chat session.

Source§

impl<M: CreateChatSession, Constraints: Clone> Task<M, Constraints>

Source

pub fn run( &self, message: impl ToString, ) -> ChatResponseBuilder<'static, M, Constraints>

Run the task with a message.

§Example
use kalosm::language::*;

#[tokio::main]
async fn main() {
    let mut llm = Llama::new_chat().await.unwrap();
    let task = llm.task("You are a math assistant who helps students with their homework. You solve equations and answer questions. When solving problems, you will always solve problems step by step.");

    let result = task("What is 2 + 2?").await.unwrap();
    println!("{result}");
}

Trait Implementations§

Source§

impl<M: CreateChatSession, Constraints: Clone> Clone for Task<M, Constraints>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<M: Debug + CreateChatSession, Constraints: Debug> Debug for Task<M, Constraints>

Source§

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

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

impl<M: CreateChatSession + 'static> Deref for Task<M>

Source§

type Target = dyn Fn(&str) -> ChatResponseBuilder<'static, M>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<M: CreateChatSession + 'static, Constraints: ModelConstraints + Clone + 'static> Deref for Task<M, Constraints>

Source§

type Target = dyn Fn(&str) -> ChatResponseBuilder<'static, M, Constraints>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<M, Constraints = NoConstraints> !Freeze for Task<M, Constraints>

§

impl<M, Constraints = NoConstraints> !RefUnwindSafe for Task<M, Constraints>

§

impl<M, Constraints> Send for Task<M, Constraints>
where Constraints: Send, M: Sync + Send, <M as CreateChatSession>::ChatSession: Send,

§

impl<M, Constraints> Sync for Task<M, Constraints>
where Constraints: Sync, M: Sync + Send, <M as CreateChatSession>::ChatSession: Send,

§

impl<M, Constraints> Unpin for Task<M, Constraints>
where Constraints: Unpin, <M as CreateChatSession>::Error: Unpin,

§

impl<M, Constraints = NoConstraints> !UnwindSafe for Task<M, Constraints>

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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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 = 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.
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
Source§

impl<T> ErasedDestructor for T
where T: 'static,