Mastering Sequential Prompts in Langchain: An Introduction to Automated Prompt Engineering

Qrious Kamal
4 min readMar 19, 2023

--

The process of Prompt Engineering has already entered into a new era of automation. You are going to witness that in this video. It starts with langchain’s Chains and Agents.

What is a Chain in Langchain Python Library:

First, let’s define what is a Chain. In the context of Langchain, a chain is a series of actions, that is triggered by your starting prompt. That actions and their outputs need to move between systems, language models and even reach out to internet and get data. That task is taken can by the Agents. More about agents in the next video. In other words, the response to one prompt becomes the input for the next prompt in the sequence. This creates a chain of prompts that are closely related to each other, resulting in more accurate and relevant output. Never heard of langchain, got you covered, take a look at this video

Why Chain and Agents important?

So why are Chains and Agents important in Langchain and automated prompt engineering? The answer lies in the nature of our work or task at hand. Any task we want to complete will involve working with outputs that span multiple other application, devices, and even those located out of our sight. Once you understand this, then automation becomes the only way to get the work done. Else you have to move from one system to another, and take each task and do. Automation is made possible by the LLMChain, SequentialChain and SimpleSequentialChain classes in Langchain Library

You will need the following 7 Steps:

  1. API connectivity to one of the LLMs
  2. Access to a terminal/ command prompt that has Python + Langchain library Installed
  3. Understand the ecosystem around Langchain
  4. Create your Prompt Templates
  5. Write the individual chain elements using LangChain own utility chains
  6. Use LLMChain, SequentialChain and SimpleSequentialChain classes to create the chains of activity
  7. Execute the chain by sending in the prompts.

The code for this Post:

https://github.com/insightbuilder/python_de_learners_data/blob/main/code_script_notebooks/projects/langChain_exploration/Utils_langChain.ipynb

How sequential chain in Langchain Works

Sequential prompts in Langchain generate output by connecting with LLMs like Text-Davinci-002. Lets think of a scenario. We want the ChatGPT or any LLM to take a Problem statement

  • Categorise problem statement and then
  • Provide the pseudo code for the problem statement

A problem statement will be provided to the Sequential chain created in LangChain. The chain will first categorise the problem statement and then move on to provide the psuedocode for the problem statement. Both activity done with a “Single Prompt”. We can do any number of activities… Since the explanation is best given with the example, take a look at the below video, in which we will explore the concept of Chains and its classes in Langchain library.

How the prompt output becomes the input for the next step using Python.

We need to answer how Python helps in automating multiple systems. To implement automation in Langchain, knowledge of these multiple systems needs to be gained. Whether it is database, API endpoints, Cloud services storage or local file system. Python makes it easy to gain the knowledge, and then implement it as programs that spans all these systems, applications and the products. It has a large number of libraries and tools that make it easy to work with natural language data. Langchain spans across these libraries, tools, systems using the framework of Agents, Chains and Prompts and automates. Want to brush up your python libraries, here is playlist with important python libraries for your rapid recollection.

What are sequential Chains? Why they are important?

The Utility Chains that are already built into Langchain can connect with internet using LLMRequests, do math with LLMMath, do code with PALChain and a lot more. Sequentialchain class in Langchain connects thes langchain objects into a series of tools, that can be manipulated using the large language models. The decision inside the Chains, Agents in Langchain Library is a powerful technique that can help create actionable output. Testing it out in the Python environment could be a great way of getting hang of this library. With the help of Python and the API connectivity to LLMs, implementing chains has never been easier.

Deep Dive into Langchain

Dive deep into this tech today. Langchain library came into existance in Oct’22 and it is growing rapidly. The series of videos I have made on LLMs, ChatGPT automation, Fine Tuning will bring you into the world of Prompt Engineering automation in no time. In conclusion, So if you’re interested in LLMS, Prompt Engineering and automation, be sure to check out other videos subscribe to my channel at https://www.youtube.com/channel/UCRkoxQy1AuX8dT8WYnw0w-w

--

--

Qrious Kamal

Name says it. Ever qrious and searching for getting thing done in the easiest possible route. And then document the challenges.