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
impl MapRerankDocuments
Sourcepub fn new<M: Into<Box<dyn Chain>>>(map_chain: M) -> Self
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:.
Sourcepub fn with_input_key<S: Into<String>>(self, key: S) -> Self
pub fn with_input_key<S: Into<String>>(self, key: S) -> Self
Input key holding the documents (default input_documents).
Sourcepub fn with_output_key<S: Into<String>>(self, key: S) -> Self
pub fn with_output_key<S: Into<String>>(self, key: S) -> Self
Key of the answer in execute’s output (default output).
Sourcepub fn with_document_variable_name<S: Into<String>>(self, name: S) -> Self
pub fn with_document_variable_name<S: Into<String>>(self, name: S) -> Self
Prompt variable each document’s text goes in (default context).
Sourcepub fn with_max_concurrency(self, max_concurrency: usize) -> Self
pub fn with_max_concurrency(self, max_concurrency: usize) -> Self
How many LLM calls run at the same time (default 5, minimum 1).
Sourcepub fn with_return_intermediate_steps(self, enabled: bool) -> Self
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.
Sourcepub async fn rank(
&self,
input_variables: &PromptArgs,
) -> Result<(Vec<RankedAnswer>, usize, Option<TokenUsage>), ChainError>
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.
Trait Implementations§
Source§impl Chain for MapRerankDocuments
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,
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,
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,
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 morefn get_input_keys(&self) -> Vec<String>
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,
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,
Chain and receive just the generation result as a String.
The input is a set of variables passed as a PromptArgs hashmap. Read moreSource§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,
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,
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 moreSource§fn call_stream<'a>(
&'a self,
input_variables: PromptArgs,
) -> ChainEventStream<'a>
fn call_stream<'a>( &'a self, input_variables: PromptArgs, ) -> ChainEventStream<'a>
Auto Trait Implementations§
impl !RefUnwindSafe for MapRerankDocuments
impl !UnwindSafe for MapRerankDocuments
impl Freeze for MapRerankDocuments
impl Send for MapRerankDocuments
impl Sync for MapRerankDocuments
impl Unpin for MapRerankDocuments
impl UnsafeUnpin for MapRerankDocuments
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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