Exercise#6 ToT-v2

Objective

Convert the Creative Writing Workbench code from earlier exercise to use LangChain Execution Language (LCEL)

Creative writing exercise : ToT v1

exercise-6-sequential-chain-v2

NOTE:

  • Use any LLM of your choice
  • You may even use multiple LLMs e.g., different LLM for voting & generation

Hints:

  1. Refer to the final chain graph in solution section to get an idea on final chain flow.

  2. You need to pass multiple inputs to the prompts. Since the input variables will not be propagated automatically from one chain to another you will need to explicitly pass on the variables to the next chain. To do so, you can use a combination of RunnablePassthrough & RunnableParallel as shown in the code below. You may create a chains as sequential chain and then wrap sequential chain and variable passing using a Runnable as shown here.

RunnableParallel(
    variable_to be_passed_to_next_chain=itemgetter('available_variable'), 
    output_variable_name = chain_sequence
)

Note that itemgetter is a shorthand for RunnablePassthrough + Lambda

How to: pass data through

itemgetter

Solution

The solution is built with Google Gemini. The solution is available under the Models folder. In order to use a different model, you need to change the LLM creation code in the solution notebook.

solution-tot-exercise-6

Google Colab

  • Follow instructions in notebook to setup required packages
Open In Colab

Final graph for the chain

  • Lambda represents the RunnablePassthrough using the following shortcut
task_instructions=itemgetter('task_instructions')