Skip to main content

LLMMathChain

Struct LLMMathChain 

Source
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

Source

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

Uses the built-in prompt, which takes question.

Source

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.

Source

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.

Source

pub fn with_output_key<S: Into<String>>(self, key: S) -> Self

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

Source

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

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 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 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