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§
Provided Methods§
Sourcefn batch_call<I>(
&self,
n: usize,
input: I,
) -> impl Future<Output = Vec<Self::Output>> + Send
fn batch_call<I>( &self, n: usize, input: I, ) -> impl Future<Output = Vec<Self::Output>> + Send
Execute the current pipeline with the given inputs. n
is the number of concurrent
inputs that will be processed concurrently.
Sourcefn map<F, Input>(self, f: F) -> Sequential<Self, Map<F, Self::Output>>
fn map<F, Input>(self, f: F) -> Sequential<Self, Map<F, Self::Output>>
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!");
Sourcefn then<F, Fut>(self, f: F) -> Sequential<Self, Then<F, Fut::Output>>
fn then<F, Fut>(self, f: F) -> Sequential<Self, Then<F, Fut::Output>>
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!");
Sourcefn chain<T>(self, op: T) -> Sequential<Self, T>
fn chain<T>(self, op: T) -> Sequential<Self, T>
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);
Sourcefn 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 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;
Sourcefn prompt<P>(self, prompt: P) -> Sequential<Self, Prompt<P, Self::Output>>
fn prompt<P>(self, prompt: P) -> Sequential<Self, Prompt<P, Self::Output>>
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.