pub struct RefineDocuments { /* private fields */ }Expand description
Combines documents by running a chain on the first one, then refining that answer with each following document in turn.
Good when the documents don’t fit in one prompt and order matters, or when you want the answer to build up as the model reads. It makes one LLM call per document, sequentially.
Inputs: input_documents (a Vec<Document>) plus whatever the prompts use (e.g.
question). The initial chain gets each document as context; the refine chain also
gets the previous answer as existing_answer.
let chain = RefineDocuments::load_summarize(OpenAI::default());
let summary = chain
.invoke(prompt_args! { "input_documents" => docs })
.await?;Implementations§
Source§impl RefineDocuments
impl RefineDocuments
Sourcepub fn new<C1: Into<Box<dyn Chain>>, C2: Into<Box<dyn Chain>>>(
initial_chain: C1,
refine_chain: C2,
) -> Self
pub fn new<C1: Into<Box<dyn Chain>>, C2: Into<Box<dyn Chain>>>( initial_chain: C1, refine_chain: C2, ) -> Self
initial_chain answers from the first document; refine_chain takes
existing_answer and the next document and returns an improved answer.
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_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 is passed as (default context).
Sourcepub fn with_initial_response_name<S: Into<String>>(self, name: S) -> Self
pub fn with_initial_response_name<S: Into<String>>(self, name: S) -> Self
Prompt variable the previous answer is passed as (default existing_answer).
Source§impl RefineDocuments
impl RefineDocuments
Sourcepub fn load_qa<L: Into<Box<dyn LLM>>>(llm: L) -> Self
pub fn load_qa<L: Into<Box<dyn LLM>>>(llm: L) -> Self
Answers question from the first document, then refines the answer with each of the
others. Inputs: input_documents, question.
Sourcepub fn load_summarize<L: Into<Box<dyn LLM>>>(llm: L) -> Self
pub fn load_summarize<L: Into<Box<dyn LLM>>>(llm: L) -> Self
Summarizes the first document, then refines the summary with each of the others.
Input: input_documents.
Trait Implementations§
Source§impl Chain for RefineDocuments
impl Chain for RefineDocuments
Source§fn call_stream<'a>(
&'a self,
input_variables: PromptArgs,
) -> ChainEventStream<'a>
fn call_stream<'a>( &'a self, input_variables: PromptArgs, ) -> ChainEventStream<'a>
Runs the intermediate steps quietly and streams the final refinement.
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>
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 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,
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 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 morefn get_output_keys(&self) -> Vec<String>
Auto Trait Implementations§
impl !RefUnwindSafe for RefineDocuments
impl !UnwindSafe for RefineDocuments
impl Freeze for RefineDocuments
impl Send for RefineDocuments
impl Sync for RefineDocuments
impl Unpin for RefineDocuments
impl UnsafeUnpin for RefineDocuments
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