pub struct ConstitutionalChain { /* private fields */ }Expand description
Runs a chain, then for each principle asks the model to critique the answer and, when the critique finds a problem, to revise it. Each revision is what the next principle critiques.
let chain = ConstitutionalChain::new(
qa_chain,
llm,
vec![ConstitutionalPrinciple::get("illegal").unwrap()],
);
let answer = chain.invoke(prompt_args! { "question" => "..." }).await?;Implementations§
Source§impl ConstitutionalChain
impl ConstitutionalChain
Sourcepub fn new<L: Into<Box<dyn LLM>>>(
chain: LLMChain,
llm: L,
principles: Vec<ConstitutionalPrinciple>,
) -> Self
pub fn new<L: Into<Box<dyn LLM>>>( chain: LLMChain, llm: L, principles: Vec<ConstitutionalPrinciple>, ) -> Self
chain answers first; llm writes the critiques and revisions with the built-in
few-shot prompts.
Sourcepub fn with_critique_chain<C: Into<Box<dyn Chain>>>(self, chain: C) -> Self
pub fn with_critique_chain<C: Into<Box<dyn Chain>>>(self, chain: C) -> Self
Replaces the critique step. It gets input_prompt, output_from_model and
critique_request; an output containing “No critique needed” skips the revision.
Sourcepub fn with_revision_chain<C: Into<Box<dyn Chain>>>(self, chain: C) -> Self
pub fn with_revision_chain<C: Into<Box<dyn Chain>>>(self, chain: C) -> Self
Replaces the revision step. It gets the critique step’s inputs plus critique and
revision_request.
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 final answer in execute’s output (default output).
Sourcepub fn with_return_intermediate_steps(self, enabled: bool) -> Self
pub fn with_return_intermediate_steps(self, enabled: bool) -> Self
Also return the first answer and every CritiqueRevision from
execute, under CONSTITUTIONAL_INITIAL_OUTPUT_KEY and
CONSTITUTIONAL_STEPS_KEY.
Sourcepub async fn critique_and_revise(
&self,
input_variables: PromptArgs,
) -> Result<ConstitutionalOutput, ChainError>
pub async fn critique_and_revise( &self, input_variables: PromptArgs, ) -> Result<ConstitutionalOutput, ChainError>
Runs the chain and every principle, returning all the steps.
Trait Implementations§
Source§impl Chain for ConstitutionalChain
impl Chain for ConstitutionalChain
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 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 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 ConstitutionalChain
impl !UnwindSafe for ConstitutionalChain
impl Freeze for ConstitutionalChain
impl Send for ConstitutionalChain
impl Sync for ConstitutionalChain
impl Unpin for ConstitutionalChain
impl UnsafeUnpin for ConstitutionalChain
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