Skip to main content

MapRerankDocuments

Struct MapRerankDocuments 

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

Answers from every document separately, asking the model to score each answer, and returns the best one.

Good for questions whose answer sits in one document (a FAQ entry, a product page), where mixing documents would blur it. It makes one LLM call per document, concurrently.

Inputs: input_documents (a Vec<Document>) plus whatever the prompt uses (e.g. question). The chain gets each document’s text as context and must end its output with Score: <0-100>, like the prompt of load_qa asks.

Outputs without a score rank below every scored one; if no output has a score the chain fails. Ties keep document order.

ⓘ
let chain = MapRerankDocuments::load_qa(OpenAI::default());
let answer = chain
    .invoke(prompt_args! { "input_documents" => docs, "question" => "Who wrote it?" })
    .await?;

Implementations§

Source§

impl MapRerankDocuments

Source

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

map_chain runs once per document and must answer with a trailing Score:.

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_output_key<S: Into<String>>(self, key: S) -> Self

Key of the answer in execute’s output (default output).

Source

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

Prompt variable each document’s text goes in (default context).

Source

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

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

Source

pub fn with_return_intermediate_steps(self, enabled: bool) -> Self

Also return every document’s RankedAnswer (in document order) from execute, under MAP_RERANK_INTERMEDIATE_STEPS_KEY.

Source

pub async fn rank( &self, input_variables: &PromptArgs, ) -> Result<(Vec<RankedAnswer>, usize, Option<TokenUsage>), ChainError>

Answers from every document and returns the answers in document order, the index of the best one, and the usage of all calls.

Source§

impl MapRerankDocuments

Source

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

Answers question from each document with a self-reported score and returns the best-scored answer. Inputs: input_documents, question.

Trait Implementations§

Source§

impl Chain for MapRerankDocuments

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,

Returns the best answer under the output key, its score under MAP_RERANK_SCORE_KEY, and optionally every answer under MAP_RERANK_INTERMEDIATE_STEPS_KEY.

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 get_output_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 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 call_stream<'a>( &'a self, input_variables: PromptArgs, ) -> ChainEventStream<'a>

Like call, but streams the LLM output as LLMEvents: Text chunks, then one Done with the complete result. Read more

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