Skip to main content

MapReduceDocuments

Struct MapReduceDocuments 

Source
pub struct MapReduceDocuments { /* private fields */ }
Expand description

Combines documents in two phases: a map chain runs on every document independently (concurrently), then a reduce chain combines the mapped outputs.

Good for documents too large to fit in one prompt together, when each one can be processed on its own (summaries, extracting relevant passages).

Inputs: input_documents (a Vec<Document>) plus whatever the prompts use (e.g. question). The map chain gets each document’s text as context. The reduce chain gets the mapped outputs as input_documents (keeping each source document’s metadata), so a StuffDocument works as the reduce chain.

ⓘ
let chain = MapReduceDocuments::load_summarize(OpenAI::default());
let summary = chain
    .invoke(prompt_args! { "input_documents" => docs })
    .await?;

Implementations§

Source§

impl MapReduceDocuments

Source

pub fn new<M: Into<Box<dyn Chain>>, R: Into<Box<dyn Chain>>>( map_chain: M, reduce_chain: R, ) -> Self

map_chain runs once per document; reduce_chain combines the results.

Source

pub fn with_input_key<S: Into<String>>(self, key: S) -> Self

Input key holding the documents (default input_documents).

Source

pub fn with_document_variable_name<S: Into<String>>(self, name: S) -> Self

Prompt variable the map chain receives each document’s text as (default context).

Source

pub fn with_reduce_input_key<S: Into<String>>(self, key: S) -> Self

Input key the reduce chain receives the mapped documents in (default input_documents).

Source

pub fn with_max_concurrency(self, max_concurrency: usize) -> Self

How many map calls run at the same time (default 5, minimum 1).

Source§

impl MapReduceDocuments

Source

pub fn load_qa<L: Into<Box<dyn LLM>>>(llm: L) -> Self

Extracts the passages relevant to question from each document, then answers from those. Inputs: input_documents, question.

Source

pub fn load_summarize<L: Into<Box<dyn LLM>>>(llm: L) -> Self

Summarizes each document, then summarizes the summaries. Input: input_documents.

Trait Implementations§

Source§

impl Chain for MapReduceDocuments

Source§

fn call_stream<'a>( &'a self, input_variables: PromptArgs, ) -> ChainEventStream<'a>

Runs the map phase quietly and streams the reduce step.

Source§

fn call<'life0, 'async_trait>( &'life0 self, input_variables: PromptArgs, ) -> Pin<Box<dyn Future<Output = Result<GenerateResult, ChainError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Call the Chain and receive as output the result of the generation process along with additional information like token consumption. The input is a set of variables passed as a PromptArgs hashmap. Read more
Source§

fn get_input_keys(&self) -> Vec<String>

Source§

fn invoke<'life0, 'async_trait>( &'life0 self, input_variables: PromptArgs, ) -> Pin<Box<dyn Future<Output = Result<String, ChainError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Invoke the Chain and receive just the generation result as a String. The input is a set of variables passed as a PromptArgs hashmap. Read more
Source§

fn execute<'life0, 'async_trait>( &'life0 self, input_variables: PromptArgs, ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, Value>, ChainError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Execute the Chain and return the result of the generation process along with additional information like token consumption formatted as a HashMap. The input is a set of variables passed as a PromptArgs hashmap. The key for the generated output is specified by the get_output_keys method (default key is output). Read more
Source§

fn stream<'life0, 'async_trait>( &'life0 self, _input_variables: PromptArgs, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamData, ChainError>> + Send>>, ChainError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stream the Chain and get an asynchronous stream of chain generations. The input is a set of variables passed as a PromptArgs hashmap. If the chain have memroy, the tream method will not be able to automaticaly set the memroy, bocause it will not know if the how to extract the output message out of the stram Read more
Source§

fn get_output_keys(&self) -> Vec<String>

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> LangchainIntoOpenAI<U> for T
where U: OpenAIFromLangchain<T>,

Source§

fn into_openai(self) -> U

Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T, U> OpenAiIntoLangchain<U> for T
where U: LangchainFromOpenAI<T>,

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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, U> TryLangchainIntoOpenAI<U> for T

Source§

impl<T, U> TryOpenAiIntoLangchain<U> for T

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