Skip to content

AsyncFlowStep

This module the AsyncFlowStep class, which can execute a language model, record execution times, and optionally invoke callbacks on the results. The async implementation allows async flowsteps to be executed in parallel if multiple flowsteps have all the required inputs available.

AsyncFlowStep

Bases: AsyncBaseFlowStep

Represents a specific async step in an async Flow.

An AsyncFlowStep calls a language model using a prompt template, records the run time, and optionally invokes callback functions on the results. Async Flowsteps can be run in parallel in an AsyncFlow if all the required inputs are available.

Parameters:

Name Type Description Default
name str

The name of the flow step.

required
llm BaseLLM

The language model to be used in the flow step.

required
prompt_template PromptTemplate

Template for the prompt to be used with the language model.

required
callbacks Union[list[AsyncBaseCallback], None]

Callbacks to be invoked when running the flow

None

Attributes:

Name Type Description
llm BaseLLM

The language model to be used in the flow step.

prompt_template PromptTemplate

Template for the prompt to be used with the language model.

required_keys set[str]

The keys required for the flow step to run.

Source code in llmflows/flows/async_flowstep.py
class AsyncFlowStep(AsyncBaseFlowStep):
    """
    Represents a specific async step in an async Flow.

    An AsyncFlowStep calls a language model using a prompt template, records the
    run time, and optionally invokes callback functions on the results.
    Async Flowsteps can be run in parallel in an AsyncFlow if all the required
    inputs are available.

    Args:
        name (str): The name of the flow step.
        llm (BaseLLM): The language model to be used in the flow step.
        prompt_template (PromptTemplate): Template for the prompt to be used with the 
            language model.
        callbacks Union[list[AsyncBaseCallback], None]: Callbacks to be invoked
            when running the flow

    Attributes:
        llm (BaseLLM): The language model to be used in the flow step.
        prompt_template (PromptTemplate): Template for the prompt to be used with the 
            language model.
        required_keys (set[str]): The keys required for the flow step to run.
    """

    def __init__(
        self,
        name: str,
        llm: BaseLLM,
        prompt_template: PromptTemplate,
        output_key: str,
        callbacks: Union[list[AsyncBaseCallback], None] = None,
    ):
        super().__init__(name, output_key, callbacks)
        self.llm = llm
        self.prompt_template = prompt_template
        self.required_keys = prompt_template.variables

    async def generate(self, inputs: dict[str, Any]) -> tuple[Any, Any, Any]:
        prompt = self.prompt_template.get_prompt(**inputs)
        text_result, call_data, model_config = await self.llm.generate_async(prompt)

        call_data["prompt_template"] = self.prompt_template.prompt
        call_data["prompt"] = prompt

        return text_result, call_data, model_config