Struct PipelineBuilder

Source
pub struct PipelineBuilder<E> { /* private fields */ }

Implementations§

Source§

impl<E> PipelineBuilder<E>

Source

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

Add a function to the current pipeline

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

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

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

pub fn then<F, Input, Fut>(self, f: F) -> Then<F, Input>
where F: Fn(Input) -> Fut + Send + Sync, Input: 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 pipeline = pipeline::new()
    .then(|email: String| async move {
        email.split('@').next().unwrap().to_string()
    })
    .then(|username: String| async move {
        format!("Hello, {}!", username)
    });

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

pub fn chain<T>(self, op: T) -> T
where T: Op, Self: Sized,

Add an arbitrary operation to the current pipeline.

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

struct MyOp;

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

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

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

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

pub fn lookup<I, Input, Output>( self, index: I, n: usize, ) -> Lookup<I, Input, Output>
where I: VectorStoreIndex, Output: Send + Sync + for<'a> Deserialize<'a>, Input: Into<String> + Send + Sync, 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::pipeline::{self, Op};

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

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

pub fn prompt<P, Input>(self, agent: P) -> Prompt<P, Input>
where P: Prompt, Input: Into<String> + Send + Sync, Self: Sized,

Add a prompt operation to the current pipeline/op. The prompt operation expects the current pipeline to output a string. The prompt operation will use the string to prompt the given agent, which must implements the Prompt trait and return the response.

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

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

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

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

pub fn extract<M, Input, Output>( self, extractor: Extractor<M, Output>, ) -> Extract<M, Input, Output>
where M: CompletionModel, Output: JsonSchema + for<'a> Deserialize<'a> + Send + Sync, Input: Into<String> + Send + Sync,

Add an extract operation to the current pipeline/op. The extract operation expects the current pipeline to output a string. The extract operation will use the given extractor to extract information from the string in the form of the type T and return it.

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

#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
struct Sentiment {
    /// The sentiment score of the text (0.0 = negative, 1.0 = positive)
    score: f64,
}

let extractor = &openai_client.extractor::<Sentiment>("gpt-4").build();

let pipeline = pipeline::new()
    .map(|text| format!("Analyze the sentiment of the following text: {text}!"))
    .extract(extractor);

let result: Sentiment = pipeline.call("I love ice cream!".to_string()).await?;
assert!(result.score > 0.5);

Auto Trait Implementations§

§

impl<E> Freeze for PipelineBuilder<E>

§

impl<E> RefUnwindSafe for PipelineBuilder<E>
where E: RefUnwindSafe,

§

impl<E> Send for PipelineBuilder<E>
where E: Send,

§

impl<E> Sync for PipelineBuilder<E>
where E: Sync,

§

impl<E> Unpin for PipelineBuilder<E>
where E: Unpin,

§

impl<E> UnwindSafe for PipelineBuilder<E>
where E: UnwindSafe,

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> 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<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,