pub struct LLMMathChain { /* private fields */ }Expand description
Answers math questions by asking the model for an expression and evaluating it with the built-in calculator, so the arithmetic is exact instead of guessed. Nothing the model writes is executed as code.
The model’s expression goes in a fenced block (any language tag, so ```python
works too). An Answer: ... reply is returned as is.
ⓘ
let chain = LLMMathChain::new(OpenAI::default());
let answer = chain
.invoke(prompt_args! { "question" => "What is 43 * 10000 / 7324.3?" })
.await?; // "58.7086820584"Implementations§
Source§impl LLMMathChain
impl LLMMathChain
Sourcepub fn new<L: Into<Box<dyn LLM>>>(llm: L) -> Self
pub fn new<L: Into<Box<dyn LLM>>>(llm: L) -> Self
Uses the built-in prompt, which takes question.
Sourcepub fn from_chain<C: Into<Box<dyn Chain>>>(chain: C) -> Self
pub fn from_chain<C: Into<Box<dyn Chain>>>(chain: C) -> Self
Uses your own chain to write the expression. Its output must hold the expression in
a fenced block, or an Answer: line.
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 with the question (default question). The prompt still gets it as
question.
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 answer).
Sourcepub async fn solve(
&self,
input_variables: PromptArgs,
) -> Result<MathAnswer, ChainError>
pub async fn solve( &self, input_variables: PromptArgs, ) -> Result<MathAnswer, ChainError>
Asks the model for an expression and evaluates it.
Trait Implementations§
Source§impl Chain for LLMMathChain
impl Chain for LLMMathChain
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,
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 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,
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 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,
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 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,
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 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 LLMMathChain
impl !UnwindSafe for LLMMathChain
impl Freeze for LLMMathChain
impl Send for LLMMathChain
impl Sync for LLMMathChain
impl Unpin for LLMMathChain
impl UnsafeUnpin for LLMMathChain
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
Mutably borrows from an owned value. Read more
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> ⓘ
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 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> ⓘ
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