use crate::frame::{FormatAndExecuteError, Frame};
use crate::options::Opt;
use crate::options::Options;
use crate::output::Output;
use crate::prompt::{Prompt, StringTemplateError};
use crate::traits::Executor;
use crate::{chains::sequential, prompt, Parameters};
use serde::Deserialize;
use serde::Serialize;
#[derive(derive_builder::Builder, Debug, Clone, Serialize, Deserialize)]
pub struct Step {
pub(crate) prompt: prompt::PromptTemplate,
pub(crate) options: Options,
}
impl Step {
pub fn for_prompt_template(prompt: prompt::PromptTemplate) -> Self {
Self {
prompt,
options: Options::empty().clone(),
}
}
pub fn for_prompt_with_streaming(prompt: prompt::PromptTemplate) -> Self {
let mut options = Options::builder();
options.add_option(Opt::Stream(true));
let options = options.build();
Self { prompt, options }
}
pub fn for_prompt_and_options(prompt: prompt::PromptTemplate, options: Options) -> Self {
Self { prompt, options }
}
pub fn prompt(&self) -> &prompt::PromptTemplate {
&self.prompt
}
pub fn options(&self) -> &Options {
&self.options
}
pub fn to_chain(self) -> sequential::Chain
where
Self: Sized,
{
crate::chains::sequential::Chain::of_one(self)
}
pub fn format(&self, parameters: &Parameters) -> Result<Prompt, StringTemplateError> {
self.prompt.format(parameters)
}
pub async fn run<E>(
&self,
parameters: &Parameters,
executor: &E,
) -> Result<Output, FormatAndExecuteError>
where
Self: Sized,
E: Executor,
{
Frame::new(executor, self)
.format_and_execute(parameters)
.await
}
}