1. Sequential chains
Let's take our LCEL chains to the next level with sequential chains!
2. Sequential problems
Some problems can only be solved sequentially.
Consider a chatbot used to create a travel itinerary.
3. Sequential problems
We need to tell the chatbot our destination,
4. Sequential problems
receive suggestions on what to see on our trip,
5. Sequential problems
and tell the model which activities to select
6. Sequential problems
to compile the itinerary.
7. Sequential problems
This is a sequential problem, as it requires more than one user input: one to specify the destination, and another to select the activities. Let's code this out!
8. Sequential chains
In sequential chains, the output from one chain becomes the input to another.
We'll create two prompt templates: one to generate suggestions for activities from the input destination, and another to create an itinerary for one day of activities from the model's top three suggestions.
We define our model, and begin our sequential chain. We start by defining a dictionary that passes our destination prompt template to the LLM and parses the output to a string, all using LCEL's pipe. This gets assigned to the "activities" key, which is important, as this is the input variable to the second prompt template. We pipe the first chain into the second prompt template, then into the LLM, and again, parse to a string. We also wrap the sequential chain in parentheses so we can split this code across multiple lines.
To summarize: the destination_prompt is passed to the LLM to generate the activity suggestions, and the output is parsed to a string and assigned to "activities". This is passed to the second activities_prompt, which is passed to the LLM to generate the itinerary, which is parsed as a string.
9. Sequential chains
Let's invoke the chain, passing Rome as our input destination.
The model considered that we only had one day to explore, and wove in it's top suggestions of the Colosseum and Vatican City.
10. Let's practice!
Time to give this a go!