Lang Chain Chaining (Simple Sequential Chain)

Lang Chain Chaining (Simple Sequential Chain)

LangChain is a cutting-edge platform that empowers developers to create advanced AI agents. With its comprehensive framework and tools, LangChain enables the development of conversational agents, chatbots, and language-based applications. Leveraging state-of-the-art language models, LangChain equips developers with the capabilities to build intelligent agents that can understand, reason, and interact with users in a human-like manner. Unlock the potential of AI with LangChain and revolutionize the way you build AI-powered applications.

LangChain stands out as the preferred platform for building agents due to its comprehensive framework and advanced capabilities. With LangChain, developers can leverage state-of-the-art language models, customize agent behaviour, and seamlessly integrate with various tools and APIs. The platform's focus on language learning models (LLMs) enables the creation of conversational agents that excel in understanding and interacting with users. Choose LangChain to unlock the full potential of AI and build intelligent agents that deliver exceptional user experiences.

Use Cases: LangChain can be used to build personal assistants, question and answering systems, chatbots, and applications for querying tabular data, interacting with APIs, extraction, summarization, and evaluation of generative models. Provide examples and explain how LangChain can be applied in each use case.

Getting Started: Head on to the [docs]https://python.langchain.com/docs

Examples: Include some practical examples of using LangChain to build applications. You can refer to the LangChain [cookbook]https://github.com/langchain-ai/langchain/blob/master/cookbook, which provides example code for building applications with LangChain. Mention that the cookbook focuses on more applied and end-to-end examples than the main documentation.

Additional Resources: There are many but if you are a developer, go check this out [ChatGptForLangchain]https://github.com/langchain-ai/chat-langchain
We will try to create a dedicate a special post to this soon.

So lets dive in to create something with Langchain.

Writing & Understanding the Code

Introduction: As we see ChatGpt being so popular, ever imagied how we can build and use it for our custom use business needs. Langchain allows us capabilities to use the power of OpenAI LLMs and other open-source, fine-tuned LLMs for customised behaviours. In LangChain, a chain refers to a sequence of language learning models (LLMs) that are used to process and generate text. These LLMs are trained on large amounts of data and can understand and generate human-like text. The chain in LangChain consists of multiple LLMs that work together to perform various tasks, such as natural language understanding, language generation, and dialogue management. By chaining together these models, LangChain enables the development of powerful conversational agents and chatbots that can engage in meaningful and contextually relevant conversations with users. Sequential chain allows you to connect multiple chains and execute them in a specific order.

Pre-requisites:

All you need is a Jupyter notebook and langchain installed on your environment. Checkout the [Installation Instructions]https://js.langchain.com/docs/get_started/installation

Code Setup: This includes importing the necessary libraries (os, openai, dotenv), loading the OpenAI API key from the environment using load_dotenv, and initializing the OpenAI language model using the OpenAI class from langchain.llms.

import os
import openai
from dotenv import load_dotenv
from langchain.llms import OpenAI
from langchain import PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain

load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")

Prompt Templates: The code snippet that defines the two prompt templates using the PromptTemplate class from langchain.

prompt1 = PromptTemplate(
   input_variables=["concept"],
   template="""
           You are an expert with coding and programming.
           Explain me the concept of {concept} in a couple of lines.""",
)
prompt2 = PromptTemplate(
   input_variables=["concept_name"],
   template="""
           Explain me the description of {concept_name} like the user is 5 years old""",
)

Chains Creation: Describing the process of creating the individual chains and combining them into a sequential chain. The LLMChain class is used to create each chain, with the OpenAI language model and prompt template as parameters. Show the code snippet that creates the two chains and combines them into a SimpleSequentialChain using the chains parameter.

chain1 = LLMChain(llm=OpenAI(model_name="text-davinci-003"), prompt=prompt1)
chain2 = LLMChain(llm=OpenAI(model_name="text-davinci-003"), prompt= prompt2)

chain_Final = SimpleSequentialChain ( chains=[chain1, chain2] , verbose=True)

Running the Chain: Run the sequential chain using the run method. Describe that the input for the chain is provided as an argument to the run method. Show the code snippet that runs the sequential chain with the input "Sum of two numbers with an example of 2+2" and stores the output in a variable. Give it a try ;)

explaination = chain_Final.run("Sum of two numbers with example of 2+2")
print(explaination)

Conclusion: Summarize the purpose and functionality of the sequential chain. Highlight the benefits of using sequential chains in programming and how they can be applied to solve various problems.