Prompt chaining is a foundational concept in building advanced workflows using large language models (LLMs).
LangChain leverages from_template to design structured input/output workflows for each step, making it easy to handle complex chatbot operations.
Prompt chaining is a foundational concept in building advanced workflows using large language models (LLMs). It involves linking multiple prompts in a logical sequence, where the output of one prompt serves as the input for the next. This modular approach is powerful for solving complex tasks like multistep text processing, summarization, question-answering and more.
LangChain is a versatile framework designed to simplify the creation of such workflows. It provides tools to manage LLMs such as IBM® Granite™ models or OpenAI’s GPT (generative pre-trained transformer) models, define custom prompts, and connect them into reusable chains. By abstracting the complexity of managing prompts, LangChain allows developers to focus on solving problems rather than orchestrating interactions with LLMs.
In this tutorial, we will:
LangChain provides a powerful framework for building modular workflows in chatbot applications. By combining structured prompts, dynamic chaining, and advanced LLM integration, it allows developers to create scalable, adaptive pipelines that leverage RAG techniques and deliver structured outputs like JSON. Here's how LangChain handles prompt chaining effectively:
Prompt abstraction: LangChain leverages from_template to design structured input/output workflows for each step, making it easy to handle complex chatbot operations.
LLM integration: The framework seamlessly integrates with various LLMs, such as, IBM Granite, OpenAI and Hugging Face, enabling fine-tuning for customized tasks.
Chain management: LangChain's SequentialChain and SimpleSequentialChain enable modular workflows for chatbot pipelines, while stroutputparser ensures structured outputs such as JSON.
Dynamic workflows: Using tools such as ConditionalChain and systemmessage templates, LangChain supports adaptive workflows, aligning with the principles of RAG (retrieval-augmented generation) for dynamic content generation.
By the end of this tutorial, you’ll have a solid understanding of how to use LangChain to build modular and extensible workflows for a wide range of applications.
Prompt chaining allows you to design workflows where outputs from one step are passed to the next. Different types of chaining support diverse workflows, ranging from simple sequential tasks to more complex, dynamic processes. Here’s a brief look at the types of prompt chaining:
Sequential chaining: The most straightforward type of chaining, where the output of one prompt is directly passed as input to the next. This option is ideal for tasks with a linear progression.[1]
Branching chaining: In branching chaining, a single output is split into multiple parallel workflows. Each branch processes the output independently. [2]
Iterative chaining: Iterative chaining involves repeatedly running a prompt or chain until a specified condition is met. This option is useful for refining outputs.[3]
Hierarchical chaining: This type breaks down a large task into smaller subtasks, which are executed hierarchically. Lower-level outputs feed higher-level tasks. [4]
Conditional chaining: Conditional chaining dynamically chooses the next step based on the output of a prior prompt. It enables decision-making within workflows.
Multimodal chaining: Multimodal chaining integrates prompts that handle different data types (for example, text, images or audio). It is suitable for applications combining multiple modalities. [2]
Dynamic chaining: Dynamic chaining adapts the workflow based on real-time outputs or changing conditions. It adds flexibility to prompt chaining. [5]
Recursive chaining: In recursive chaining, large inputs are divided into smaller chunks for individual processing, and the results are then combined. It is useful for handling lengthy documents or datasets. [6]
Reverse chaining: Reverse chaining starts with an expected output and works backward to determine the necessary inputs or steps to achieve it. It is great for problem-solving and debugging. [5]
Each type of chaining caters to unique use cases, making it essential to choose the right one based on the task's complexity and requirements.
In this workflow, we process customer feedback with chat models and prompt engineering to build a scalable text-processing pipeline. The following stages of the tutorial demonstrate sequential, branching and iterative chaining techniques powered by generative AI.
Extracting keywords (sequential chaining)
Generating a sentiment summary (branching chaining)
Refining the sentiment summary (iterative chaining)
Final output
This approach combines sequential, branching, and iterative chaining in Python with chat models and prompt engineering. It ensures robust processing of customer feedback, leveraging generative AI for keyword extraction, sentiment analysis, and refinement.
You need an IBM Cloud® account to create a watsonx.ai™ project.
While you can choose from several tools, this tutorial walks you through how to set up an IBM account to use a Jupyter Notebook.
This step opens a notebook environment where you can copy the code from this tutorial. Alternatively, you can download this notebook to your local system and upload it to your watsonx.ai project as an asset. To view more Granite tutorials, check out the IBM Granite™ Community. This tutorial is also available on GitHub.
We need libraries to work with langchain framework and watsonxLLM. Let's first install the required packages.
Note: If you are using an old version of `pip`, you can use the command pip install --upgrade pip` to upgrade it. This step helps you with easy installation of the latest packages, which might not be compatible with an older version. But if you are already using the latest version or recently upgraded your packages, then you can skip this command.
This code block imports essential Python libraries and tools to build and manage an LLM application by using LangChain and IBM Watson LLM.
The os module is used to access environment variables, such as project credentials or API keys.
WatsonxLLM is a module from langchain_ibm that integrates IBM Watson LLM for generating outputs from generative AI models.
PromptTemplate helps create reusable templates for prompts, ensuring input structure and flexibility in prompt engineering.
LLMChain builds individual task chains while
SequencialChain links multiple steps into a single workflow and `getpass` safely retrieves sensitive information (for example, API keys) without exposing it on the screen.
This code sets up credentials for accessing the IBM Watson Machine Learning (WML) API and ensures that the PROJECT_ID is correctly configured.
This code initializes the IBM Watson LLM for use in the application:
This step prepares the Watson LLM for generating responses in the workflow.
This code defines prompt templates for three stages of the text-processing workflow:
These PromptTemplate instances enable reusable and structured prompt engineering for the LLM application.
This code defines LLM chains that connect the prompts with the initialized IBM Watson LLM, assigning unique output keys for each stage:
These LLMChain instances enable modular task execution, facilitating a step-by-step LLM application workflow.
This code combines the previously defined chains into a sequential workflow, enabling a step-by-step process for text input. The SequentialChain links the keyword_chain, sentiment_chain, and refine_chain in a defined order, ensuring that the output of one chain serves as the input for the next. The workflow is configured to accept text as its initial input, with the final output, a refined sentiment summary, stored under the key "refined_summary". This setup allows for streamlined and efficient execution of the LLM application, ensuring a coherent and modular processing pipeline.
In this code block, we will run the entire workflow. First, we have a multiline feedback string defined as feedback_text, containing both positive and negative user comments about an app. The workflow.run method processes the feedback through the sequential chains (keyword extraction, sentiment analysis, and refinement) by using the provided input. And the refined sentiment summary is printed directly as the final result.
OUTPUT
Refined Sentiment Summary:
The user's sentiment is predominantly negative due to recurring app crashes and slow customer support response times, despite appreciation for the app's features and occasional helpful customer support. To enhance user satisfaction, the development team should focus on resolving app crashes and expediting customer support responses.
The refined sentiment summary is a concise and clear evaluation of the feedback. It highlights the user's appreciation for the app's features but expresses frustration over frequent crashes and slow customer support, reflecting the workflow's ability to distill critical insights effectively.
Selecting the appropriate chaining type for LLM application involves evaluating key factors to ensure efficiency and coherence:
Task complexity: Use runnable workflows for tasks with multiple steps. Few-shot examples or chatprompttemplate can help structure complex tasks requiring different prompts.
Dependency: If outputs from one step are placeholders for the next prompt, use sequential chaining. Output parsers ensure smooth transition of outputs into structured inputs.
Adaptability: For dynamic workflows, such as those involving langchain agents, iterative chaining allows real-time adjustments of parameters and next prompts.
Data modality: Choose workflows compatible with varied data types. Use embedding methods for text and vector data or LangChain Expression Language for flexible operations.
By considering these factors, you can build a robust and adaptable LLM application with coherent chaining workflows.
Prompt chaining is a versatile technique for building sophisticated natural language processing (NLP) workflows. In this tutorial, we explored various chaining types and demonstrated a generic example integrating multiple chaining approaches. By experimenting with these methods, you can unlock the full potential of language models for real-world applications.
[1] Roegiest, A., & Chitta, R. (2024). Answering Questions in Stages: Prompt Chaining for Contract QA.
[2] Ge, J., Luo, H., Qian, S., Gan, Y., Fu, J., & Zhang, S. (2023). Chain of Thought Prompt Tuning in Vision Language Models.
[3] Sun, S., Yuan, R., Cao, Z., Li, W., & Liu, P. (2024). Prompt Chaining or Stepwise Prompt? Refinement in Text Summarization. , 7551-7558. https://arxiv.org/abs/2406.00507.
[4] Huang, Q., Zhu, J., Li, Z., Xing, Z., Wang, C., & Xu, X. (2023). PCR-Chain: Partial Code Reuse Assisted by Hierarchical Chaining of Prompts on Frozen Copilot. 2023 IEEE/ACM 45th International Conference on Software Engineering: Companion Proceedings (ICSE-Companion), 1-5. https://ieeexplore.ieee.org/document/10172550.
[5] Wu, T., Jiang, E., Donsbach, A., Gray, J., Molina, A., Terry, M., & Cai, C. (2022). PromptChainer: Chaining Large Language Model Prompts through Visual Programming. CHI Conference on Human Factors in Computing Systems Extended Abstracts. https://dl.acm.org/doi/10.1145/3491101.3519729.
[6] Trautmann, D. (2023). Large Language Model Prompt Chaining for Long Legal Document Classification. ArXiv, abs/2308.04138. https://arxiv.org/abs/2308.04138.
Train, validate, tune and deploy generative AI, foundation models and machine learning capabilities with ease and build AI applications in a fraction of the time with a fraction of the data.
Redefine how you work with AI for business. IBM Consulting™ is working with global clients and partners to co-create what’s next in AI. Our diverse, global team of more than 20,000 AI experts can help you quickly and confidently design and scale cutting edge AI solutions and automation across your business.
IBM’s artificial intelligence solutions help you build the future of your business. These include: IBM watsonx, our AI and data platform and portfolio of AI-powered assistants; IBM Granite, our family of open-sourced, high-performing and cost-efficient models trained on trusted enterprise data; IBM Consulting, our AI services to redesign workflows; and our hybrid cloud offerings that enable AI-ready infrastructure to better scale AI.