pub struct PipelineBuilder<E> { /* private fields */ }
Implementations§
Source§impl<E> PipelineBuilder<E>
impl<E> PipelineBuilder<E>
Sourcepub fn map<F, Input, Output>(self, f: F) -> Map<F, Input>
pub fn map<F, Input, Output>(self, f: F) -> Map<F, Input>
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!");
Sourcepub fn then<F, Input, Fut>(self, f: F) -> Then<F, Input>
pub fn then<F, Input, Fut>(self, f: F) -> Then<F, Input>
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!");
Sourcepub fn chain<T>(self, op: T) -> T
pub fn chain<T>(self, op: T) -> T
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);
Sourcepub 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,
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;
Sourcepub fn prompt<P, Input>(self, agent: P) -> Prompt<P, Input>
pub fn prompt<P, Input>(self, agent: P) -> Prompt<P, Input>
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;
Sourcepub 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,
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);