This project demonstrates the Prompt Chaining workflow pattern for Large Language Models (LLMs) using Spring AI. The pattern decomposes complex tasks into a sequence of steps, where each LLM call processes the output of the previous one.
The prompt chaining pattern is particularly useful when:
- Complex tasks can be broken down into simpler, sequential steps
- Each step's output needs to be validated or transformed
- The process requires maintaining a clear chain of transformations
This implementation shows a four-step workflow for processing numerical data in text:
- Extract numerical values and metrics
- Standardize to percentage format
- Sort in descending order
- Format as markdown table
- Java 17 or higher
- Spring Boot 3.4.1
- Spring AI 1.0.0-M5
- Ollama (for LLM integration)
-
Install and start Ollama following the instructions at ollama.ai
-
Build the project:
./mvnw clean install
-
Run the application:
./mvnw spring-boot:run
The example processes a Q3 performance report through the chain of prompts. Here's the sample input:
Q3 Performance Summary:
Our customer satisfaction score rose to 92 points this quarter.
Revenue grew by 45% compared to last year.
Market share is now at 23% in our primary market.
Customer churn decreased to 5% from 8%.
New user acquisition cost is $43 per user.
Product adoption rate increased to 78%.
Employee satisfaction is at 87 points.
Operating margin improved to 34%.
The workflow processes this through four steps:
-
Extract Values: Pulls out numerical values and their metrics
92: customer satisfaction 45%: revenue growth 23%: market share 5%: customer churn 43: user acquisition cost 78%: product adoption 87: employee satisfaction 34%: operating margin
-
Standardize Format: Converts values to percentages where applicable
92%: customer satisfaction 45%: revenue growth 23%: market share 5%: customer churn 78%: product adoption 87%: employee satisfaction 34%: operating margin
-
Sort: Orders values in descending order
92%: customer satisfaction 87%: employee satisfaction 78%: product adoption 45%: revenue growth 34%: operating margin 23%: market share 5%: customer churn
-
Format: Creates a markdown table
| Metric | Value | |:--|--:| | Customer Satisfaction | 92% | | Employee Satisfaction | 87% | | Product Adoption | 78% | | Revenue Growth | 45% | | Operating Margin | 34% | | Market Share | 23% | | Customer Churn | 5% |
The workflow is implemented in two main classes:
-
ChainWorkflow.java
: Contains the core logic for the prompt chaining pattern, including:- System prompts for each transformation step
- Chain execution logic
- Gate validation between steps
-
Application.java
: Provides the Spring Boot setup and example usage:- Sample input data
- Spring AI configuration
- Command-line runner for demonstration
Each step in the chain acts as a gate that validates and transforms the output before proceeding to the next step, ensuring the process stays on track.
This implementation is based on the prompt chaining pattern described in Anthropic's research paper Building Effective Agents.