Trait Op

Source
pub trait Op: Send + Sync {
    type Input: Send + Sync;
    type Output: Send + Sync;

    // Required method
    fn call(
        &self,
        input: Self::Input,
    ) -> impl Future<Output = Self::Output> + Send;

    // Provided methods
    fn batch_call<I>(
        &self,
        n: usize,
        input: I,
    ) -> impl Future<Output = Vec<Self::Output>> + Send
       where I: IntoIterator<Item = Self::Input> + Send,
             I::IntoIter: Send,
             Self: Sized { ... }
    fn map<F, Input>(self, f: F) -> Sequential<Self, Map<F, Self::Output>>
       where F: Fn(Self::Output) -> Input + Send + Sync,
             Input: Send + Sync,
             Self: Sized { ... }
    fn then<F, Fut>(self, f: F) -> Sequential<Self, Then<F, Fut::Output>>
       where F: Fn(Self::Output) -> Fut + Send + Sync,
             Fut: Future + Send + Sync,
             Fut::Output: Send + Sync,
             Self: Sized { ... }
    fn chain<T>(self, op: T) -> Sequential<Self, T>
       where T: Op<Input = Self::Output>,
             Self: Sized { ... }
    fn lookup<I, Input>(
        self,
        index: I,
        n: usize,
    ) -> Sequential<Self, Lookup<I, Self::Output, Input>>
       where I: VectorStoreIndex,
             Input: Send + Sync + for<'a> Deserialize<'a>,
             Self::Output: Into<String>,
             Self: Sized { ... }
    fn prompt<P>(self, prompt: P) -> Sequential<Self, Prompt<P, Self::Output>>
       where P: Prompt,
             Self::Output: Into<String>,
             Self: Sized { ... }
}

Required Associated Types§

Required Methods§

Source

fn call(&self, input: Self::Input) -> impl Future<Output = Self::Output> + Send

Provided Methods§

Source

fn batch_call<I>( &self, n: usize, input: I, ) -> impl Future<Output = Vec<Self::Output>> + Send
where I: IntoIterator<Item = Self::Input> + Send, I::IntoIter: Send, Self: Sized,

Execute the current pipeline with the given inputs. n is the number of concurrent inputs that will be processed concurrently.

Source

fn map<F, Input>(self, f: F) -> Sequential<Self, Map<F, Self::Output>>
where F: Fn(Self::Output) -> Input + Send + Sync, Input: Send + Sync, Self: Sized,

Chain a function f to the current op.

§Example
use rig::pipeline::{self, Op};

let chain = pipeline::new()
   .map(|(x, y)| x + y)
   .map(|z| format!("Result: {z}!"));

let result = chain.call((1, 2)).await;
assert_eq!(result, "Result: 3!");
Source

fn then<F, Fut>(self, f: F) -> Sequential<Self, Then<F, Fut::Output>>
where F: Fn(Self::Output) -> Fut + Send + Sync, Fut: Future + Send + Sync, Fut::Output: Send + Sync, Self: Sized,

Same as map but for asynchronous functions

§Example
use rig::pipeline::{self, Op};

let chain = pipeline::new()
    .then(|email: String| async move {
        email.split('@').next().unwrap().to_string()
    })
    .then(|username: String| async move {
        format!("Hello, {}!", username)
    });

let result = chain.call("bob@gmail.com".to_string()).await;
assert_eq!(result, "Hello, bob!");
Source

fn chain<T>(self, op: T) -> Sequential<Self, T>
where T: Op<Input = Self::Output>, Self: Sized,

Chain an arbitrary operation to the current op.

§Example
use rig::pipeline::{self, Op};

struct AddOne;

impl Op for AddOne {
    type Input = i32;
    type Output = i32;

    async fn call(&self, input: Self::Input) -> Self::Output {
        input + 1
    }
}

let chain = pipeline::new()
   .chain(AddOne);

let result = chain.call(1).await;
assert_eq!(result, 2);
Source

fn lookup<I, Input>( self, index: I, n: usize, ) -> Sequential<Self, Lookup<I, Self::Output, Input>>
where I: VectorStoreIndex, Input: Send + Sync + for<'a> Deserialize<'a>, Self::Output: Into<String>, Self: Sized,

Chain a lookup operation to the current chain. The lookup operation expects the current chain to output a query string. The lookup operation will use the query to retrieve the top n documents from the index and return them with the query string.

§Example
use rig::chain::{self, Chain};

let chain = chain::new()
    .lookup(index, 2)
    .chain(|(query, docs): (_, Vec<String>)| async move {
        format!("User query: {}\n\nTop documents:\n{}", query, docs.join("\n"))
    });

let result = chain.call("What is a flurbo?".to_string()).await;
Source

fn prompt<P>(self, prompt: P) -> Sequential<Self, Prompt<P, Self::Output>>
where P: Prompt, Self::Output: Into<String>, Self: Sized,

Chain a prompt operation to the current chain. The prompt operation expects the current chain to output a string. The prompt operation will use the string to prompt the given agent (or any other type that implements the Prompt trait) and return the response.

§Example
use rig::chain::{self, Chain};

let agent = &openai_client.agent("gpt-4").build();

let chain = chain::new()
   .map(|name| format!("Find funny nicknames for the following name: {name}!"))
   .prompt(agent);

let result = chain.call("Alice".to_string()).await;

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: Op> Op for &T

Source§

type Input = <T as Op>::Input

Source§

type Output = <T as Op>::Output

Source§

async fn call(&self, input: Self::Input) -> Self::Output

Implementors§

Source§

impl<F, Input, Fut> Op for Then<F, Input>
where F: Fn(Input) -> Fut + Send + Sync, Input: Send + Sync, Fut: Future + Send, Fut::Output: Send + Sync,

Source§

type Input = Input

Source§

type Output = <Fut as Future>::Output

Source§

impl<F, Input, Output> Op for Map<F, Input>
where F: Fn(Input) -> Output + Send + Sync, Input: Send + Sync, Output: Send + Sync,

Source§

type Input = Input

Source§

type Output = Output

Source§

impl<I, In, T> Op for Lookup<I, In, T>
where I: VectorStoreIndex, In: Into<String> + Send + Sync, T: Send + Sync + for<'a> Deserialize<'a>,

Source§

impl<M, Input, Output> Op for Extract<M, Input, Output>
where M: CompletionModel, Output: JsonSchema + for<'a> Deserialize<'a> + Send + Sync, Input: Into<Message> + Send + Sync,

Source§

impl<Op1, Op2> Op for Parallel<Op1, Op2>
where Op1: Op, Op1::Input: Clone, Op2: Op<Input = Op1::Input>,

Source§

type Input = <Op1 as Op>::Input

Source§

type Output = (<Op1 as Op>::Output, <Op2 as Op>::Output)

Source§

impl<Op1, Op2> Op for AndThen<Op1, Op2>
where Op1: TryOp, Op2: TryOp<Input = Op1::Output, Error = Op1::Error>,

Source§

type Input = <Op1 as TryOp>::Input

Source§

type Output = Result<<Op2 as TryOp>::Output, <Op1 as TryOp>::Error>

Source§

impl<Op1, Op2> Op for MapErr<Op1, Op2>
where Op1: TryOp, Op2: Op<Input = Op1::Error>,

Source§

type Input = <Op1 as TryOp>::Input

Source§

type Output = Result<<Op1 as TryOp>::Output, <Op2 as Op>::Output>

Source§

impl<Op1, Op2> Op for MapOk<Op1, Op2>
where Op1: TryOp, Op2: Op<Input = Op1::Output>,

Source§

type Input = <Op1 as TryOp>::Input

Source§

type Output = Result<<Op2 as Op>::Output, <Op1 as TryOp>::Error>

Source§

impl<Op1, Op2> Op for OrElse<Op1, Op2>
where Op1: TryOp, Op2: TryOp<Input = Op1::Error, Output = Op1::Output>,

Source§

type Input = <Op1 as TryOp>::Input

Source§

type Output = Result<<Op1 as TryOp>::Output, <Op2 as TryOp>::Error>

Source§

impl<Op1, Op2> Op for TrySequential<Op1, Op2>
where Op1: TryOp, Op2: Op<Input = Op1::Output>,

Source§

type Input = <Op1 as TryOp>::Input

Source§

type Output = Result<<Op2 as Op>::Output, <Op1 as TryOp>::Error>

Source§

impl<Op1, Op2> Op for Sequential<Op1, Op2>
where Op1: Op, Op2: Op<Input = Op1::Output>,

Source§

type Input = <Op1 as Op>::Input

Source§

type Output = <Op2 as Op>::Output

Source§

impl<P, In> Op for Prompt<P, In>
where P: Prompt + Send + Sync, In: Into<String> + Send + Sync,

Source§

impl<T> Op for Passthrough<T>
where T: Send + Sync,

Source§

type Input = T

Source§

type Output = T