llm_chain/frame.rs
1//! A frame is the combination of a `Step` and an `Executor`. It wraps common behavior used by different chain types.
2//!
3//! This module provides the `Frame` struct, which is a core component in the process of creating customized chains.
4//! The `Frame` struct is designed to simplify the process of interacting with `Step` and `Executor` traits, allowing
5//! developers to focus on implementing the desired functionality without worrying about the boilerplate code.
6//!
7//! The `Frame` struct is generic over the `Step` and `Executor` types, ensuring that it can work with any
8//! combination of types that implement the required traits.
9
10use crate::output::Output;
11use crate::step::Step;
12use crate::traits;
13use crate::traits::ExecutorError;
14use crate::Parameters;
15
16/// The `Frame` struct represents a combination of a `Step` and an `Executor`.
17///
18/// It is designed to provide a simple interface for working with different chain types and handling common
19/// behavior for formatting and executing steps.
20pub struct Frame<'l, E>
21where
22 E: traits::Executor,
23{
24 executor: &'l E,
25 step: &'l Step,
26}
27
28impl<'l, E> Frame<'l, E>
29where
30 E: traits::Executor,
31{
32 /// Constructs a new `Frame` with the given `Executor` and `Step`.
33 ///
34 /// The `new` function takes two references to an `Executor` and a `Step`, respectively, and returns
35 /// a new `Frame` instance.
36 pub fn new(executor: &'l E, step: &'l Step) -> Self {
37 Self { executor, step }
38 }
39
40 /// Formats the step with the provided parameters and executes it using the associated executor.
41 ///
42 /// This function takes a reference to a `Parameters` struct, formats the step with the provided parameters,
43 /// and executes it using the associated executor. The result of the execution is returned as `E::Output`.
44 pub async fn format_and_execute(
45 &self,
46 parameters: &Parameters,
47 ) -> Result<Output, FormatAndExecuteError> {
48 let prompt = self.step.format(parameters)?;
49 Ok(self.executor.execute(self.step.options(), &prompt).await?)
50 }
51}
52
53#[derive(Debug, thiserror::Error)]
54/// An error that occurs when formatting and prompt template for an LLM
55pub enum FormatAndExecuteError {
56 #[error("Error formatting: {0}")]
57 Format(#[from] crate::prompt::StringTemplateError),
58 #[error("Error executing: {0}")]
59 Execute(#[from] ExecutorError),
60}