Chains and Steps
Chains and Steps are used to create multi-step prompts that can interact with the AI model in stages.
Overview
Chains in PromptL allow you to break complex workflows into smaller, manageable steps. Each step generates a response, which can then be used in subsequent steps. This approach improves the model’s ability to perform complex tasks and provides greater control over dynamic conversations.
With Chains, you can:
- Process tasks incrementally to guide the model step-by-step.
- Store and reuse intermediate results dynamically.
- Customize step-specific configurations for more efficient execution.
- Isolate steps to minimize context overhead or confusion.
Syntax
Define steps in your prompt using the <step>
tag. The engine pauses after each step, waits for the model’s response, and adds it as an assistant message before continuing.
Basic Syntax
Step with Custom Configuration
Override the default configuration by adding attributes to the <step>
tag:
Advanced Features
Storing Step Responses
You can store the text response of a step in a variable using the as
attribute. This allows you to reuse the response in later steps or logic blocks.
Storing Raw Messages
Some providers will return additional metadata along with the response. To store the entire message object, instead of just the text response (e.g., role, content, and additional metadata), use the raw
attribute:
The raw response will return an object with the full message details, which contains the role
, content
, and other metadata provided by the model.
The content
attribute will always be defined as an array of content objects, which can include text, images, or other types of content.
Isolating Steps
Use the isolated
attribute to prevent a step from inheriting context from previous steps. This can reduce unnecessary costs or confusion for the model.
In this example, the final step does not need to conside the full texts used in previous steps, so isolating the first two steps can help reduce context overhead, resulting in cheaper and more efficient processing.
Real-World Use Cases
Multi-Step Workflow
Chains are ideal for breaking down tasks like:
- Analyzing data.
- Generating intermediate results.
- Combining results for a final output.
Decision Trees
Use logic to adapt workflows based on intermediate results:
Implementation
To execute chains, use the Chain
class. The chain evaluates the prompt step-by-step, waiting for the model’s response at each step.
To run a step, execute the step
method of the chain instance. The first time step
is called, it should not include any arguments. Subsequent calls must always pass the model response message from the previous step.
Example: Using the Chain Class
Debugging Chains
- Log Intermediate Steps:
- Use the
raw
attribute to inspect full responses for debugging.
- Use the
- Handle Errors Gracefully:
- Implement fallback logic for unexpected responses or failures.
- Test Edge Cases:
- Ensure your chains handle empty inputs, invalid configurations, or incomplete data.
Summary
Chains and Steps provide powerful tools for breaking complex tasks into manageable parts. With features like custom configurations, variable storage, and step isolation, you can design robust, dynamic workflows tailored to any use case.