One-Cease Framework Constructing Purposes with LLMs


Massive Language Fashions (LLMs) have been gaining reputation for the previous few years. And with the entry of Open AIs ChatGPT, there was a large reputation achieve within the Trade in direction of these LLMs. These Massive Language Fashions are being labored upon to create totally different purposes from Query Answering Chatbots to Textual content Mills to Conversational Bots and way more. Many new APIs are being created and every has its personal means of coaching and testing our personal information to it. That is the place LangChain suits in, a Python Framework/Library for growing purposes powered with these Language Fashions. On this article, we will probably be going to be constructing purposes with LLMs.

Studying Goals

  • To grasp the LangChain Framework
  • To create purposes with LangChain
  • Understanding the Parts concerned in it
  • Constructing Chains by means of LangChain for Language Fashions
  • Understanding Brokers and Prompts in LangChain

This text was revealed as part of the Knowledge Science Blogathon.

What’s LangChain? Why is it mandatory?

LangChain is a Python Library for constructing purposes powered by Massive Language Fashions. It not solely takes care of connecting to totally different Massive Language Fashions by means of APIs however even makes these LLMs join to a knowledge supply and even makes them conscious of their setting and work together with it. So how does it slot in? The factor is these Massive Language Fashions on their very own in isolation is probably not that highly effective. Thus with LangChain, we are able to join them with exterior information sources and computation, which is able to drastically assist them to give you good solutions.

LangChain thus makes it doable for us to make the Language Fashions connect with our very personal database and create purposes round them, permitting them to reference the information. With LangChain, not solely you possibly can create Language Fashions to create Query Reply Bots primarily based in your information offered, however even make the Language Fashions take sure actions primarily based on questions thus making them Knowledge-Conscious and Agentic. We will probably be trying into these actions additional down within the article.

LangChain Framework accommodates Parts that represent LLM wrappers, that are wrappers for common language mannequin APIs,  from Open AI, Hugging Face, and many others. It even consists of the Immediate-Templates for creating our personal Prompts. LangChain, the title itself has the phrase Chain, thus making it doable to chain a number of Parts collectively and eventually Brokers, which we’ve talked about earlier than, that permit the mannequin to work together with exterior information and computations.

Putting in LangChain

Like all different Python libraries, LangChain could be put in by means of Python’s pip command. The command for that is:

pip set up -qU langchain

It will obtain and set up the newest steady model of langchain Framework in Python. The langchain Framework comes with many LLM wrappers, ChatBot Wrappers, Chat Schemas, and Immediate-Templates

Other than LangChain Packages, we have to even set up the next packages, which we will probably be engaged on on this article

pip set up ai21
pip set up -qU huggingface_hub
pip set up -qU openai

It will set up the cuddling face hub, the place we can work with hugging face APIs. OpenAI is put in, thus we are able to work with GPT fashions, and another LLM mannequin we’ve put in is the ai21, which is the Studio AI21 Language Mannequin. We will probably be working with these 3 fashions, writing examples, and understanding how LangChain suits in.

Constructing Purposes with LLMs Wrapper

LangChain offers wrappers to totally different fashions from LLMs to Chat Fashions to Textual content Embedding Fashions. Massive Language Fashions are those that take textual content for the enter and the output returned is a textual content string. On this part, we’ll check out the LangChain wrappers i.e. the usual interface it offers for various Massive Language Fashions like Hugging Face, Open AI, Studio AI21, and many others.

Let’s begin through the use of the Open AI mannequin in LangChain. The respective code will probably be

# importing Open AI Wrapper from LangChain
from langchain.llms import OpenAI

# present your API KEY right here
os.environ["OPENAI_API_KEY"] = 'Your OpenAI API KEY'

# initializing OpenAI LLM
llm = OpenAI(model_name="text-ada-001")

# question
question = 'Inform me a joke'

# mannequin output

Right here we import the OpenAI Wrapper from LangChain. Then we create an Occasion of it named llm and supply enter, the mannequin title we need to use. Now to this llm variable, we straight move the question to get an output. Working this code has given the output:

We see that the mannequin offers out a joke primarily based on the question offered. Now let’s do this with the Hugging Face Mannequin.

# importing Hugging Face Wrapper from LangChain
from langchain import HuggingFaceHub

# present your API KEY right here
os.environ['HUGGINGFACEHUB_API_TOKEN'] = 'Your Hugging Face API Token'

# initialize Hugging Face LLM
flan_t5_model = HuggingFaceHub(

query1 = "Who was the primary particular person to go to House?"
query2 = "What's 2 + 2 equals to?"

generate = flan_t5_model.generate([query1, query2])

We now have adopted the identical course of that we’ve adopted with the OpenAI mannequin. Right here we’re working with the flan-t5 mannequin and have set the temperature to 1e-1. The one distinction is that right here we use the generate() methodology of the LLM mannequin to move the queries. This generate() methodology is used while you need to move a number of queries on the similar time. These are handed within the type of a listing, which we’ve accomplished so within the above. To get the mannequin output, we then name the generations operate. The ensuing output we get after we run the code is


We see 2 solutions from the mannequin. The primary is Yuri Gagarin, who was certainly the primary particular person to enter the area and the second reply is 4, which is correct. Lastly, we’ll take a look at one other LLM wrapper, this time for the AI21 Studio which offers us with the API entry to Jurrasic – 2 LLM. Let’s take a look at the code:

# importing AI21 Studio Wrapper from LangChain
from langchain.llms import AI21

# present your API KEY right here
os.environ['AI21_API_KEY'] = 'Your API KEY'

# initializing OpenAI LLM
llm = AI21()

# question
question = 'Inform me a joke on Vehicles'

# mannequin output

Once more, we see that the general code implementation is sort of just like the OpenAI instance that we’ve seen earlier. The one distinction is the API Key and the LLM wrapper that’s being imported which is the AI21. Right here we’ve given a question to inform a joke on automobiles, let’s see the way it performs


The AI21 actually did a fab job with the reply it offered. So on this part, we’ve discovered, the best way to use totally different LLM wrappers from LangChain for working with totally different Language Fashions. Other than these 3 wrappers that we’ve seen, LangChain offers many extra wrappers to many different Language Fashions on the market.

Immediate Templates

Prompts are a key half when growing purposes with Massive Language Fashions. Initially, one must retrain your entire mannequin or need to work with fully a special mannequin to do totally different duties, like one mannequin for Translation, and one mannequin for Summarization. However the entry of Immediate Templates has modified all of it. With Immediate templates, we are able to make the Language Mannequin do something from Translation to Query Answering, to Textual content Era/Summarization on totally different information.

On this article, we’ll have a look into Immediate Templates in langchain with the OpenAI fashions. Let’s start by making a template after which giving it to LangChain’s PromptTemplate class

from langchain import PromptTemplate

# making a Immediate Template
template = """The next is a dialog between a time traveler and a 
historian. The time traveler is from the longer term and tends to make humorous 
comparisons between previous and future occasions:

Historian: {question}

Time Traveler: """

# assigning the template to the PromptTemplate Class
immediate = PromptTemplate(

Step one is to write down a template. Right here we’ve written a template stating that we wish the AI to behave like a humorous Time Traveller, that’s we’ve set a context for the AI, and the way it must be appearing. Then we offer the question contemplating we’re a historian. The question is current in {} as a result of we wish that half to get replaced by the query we need to ask. Then this template is given to the PromptTemplate class from LangChain. Right here we move the template to the template variable, and we then inform the enter variable as “question” (the enter variable is the place the place our questions sit).

Let’s attempt to create a query and observer the Immediate that will probably be generated.

# printing an instance Immediate
print(immediate.format(question='Are there flying automobiles?'))

Right here within the output, we see that {question} is changed by the query that we’ve given to the “question” variable that was given to the format() operate of the Immediate Occasion. So the PromptTemplate does the job of formatting our question earlier than we are able to ship it to the mannequin. Now, we’ve efficiently created a Immediate Template. We’ll now check this template with an OpenAI mannequin. The next would be the code:

# creating the llm wrapper
llm = OpenAI(model_name="text-davinci-003",temperature=1)

# mannequin's output to the question
        question='Are there flying automobiles?')

Right here once more, we’re working with the LLM wrapper from langchain for OpenAI Language Mannequin. We’re then giving the Immediate on to the llm, identical to how we’ve handed at first code instance. It will then print the output generated by the mannequin, which is:


Ha! No means, though I did see a farmer the opposite day utilizing a robotic scarecrow to shoo away birds from his cornfield. Does that rely?

Seeing the output, we are able to say that the Language mannequin did certainly act like a Time Traveller. Immediate Templates could be supplied with a number of queries. Let’s attempt to create a template to supply a number of inputs to the Language Mannequin

multi_query_template = """Reply the next questions one after the other.


long_prompt = PromptTemplate(

qs = [
    "Which IPL team won the IPL in the 2016 season?",
    "How many Kilometers is 20 Miles?",
    "How many legs does a lemon leaf have?"


The above depicts the code for a number of question templates. Right here within the Immediate Template, we’ve written at the beginning, that the questions should be answered one after the other. Then we offer this template to the PromptTemplate class. Retailer all of the questions in a listing referred to as qs. Identical to earlier than, we move this qs checklist to the question variable within the immediate.format() operate and provides it to the mannequin. Now let’s verify the output produced by the Language Mannequin


We see that the OpenAI Language Mannequin has given all the appropriate output solutions one after the other. Thus far, what we’ve accomplished with Immediate Template is only a glimpse. We will do way more utilizing the Immediate Parts from the LangChain Framework. One instance is we are able to carry out Few Shot Studying by offering some examples within the context of the Immediate Template itself.

LangChain – Chaining Parts Collectively

The LangChain, the title itself has the phrase Chain in it. On this part, we’ll take a look at the Chain Module of LangChain. When growing purposes, utilizing Language Fashions in isolation is ok, i.e. for small purposes, however when creating complicated ones, it’s higher to chain LLMs, i.e. both chaining two related LLMs or two totally different ones. LangChain offers a Normal Interface for chaining totally different Language Fashions collectively. Chains can be utilized to attach totally different Parts like LLMs and even Prompts collectively to carry out a specific motion


Let’s begin by taking a easy instance of chaining an OpenAI Language Mannequin with a Immediate.

from langchain.llms import OpenAI
from langchain import PromptTemplate, LLMChain

# making a Immediate Template
template = """The next is a dialog between a human and an AI 
Assistant. Regardless of the human asks to clarify, the AI assistant
explains it with humour and by taking banana for example

Human: {question}

AI: """

# assigning the template to the PromptTemplate Class
immediate = PromptTemplate(

# question
question = "Clarify Machine Studying?"

# creating an llm chain
llm = OpenAI(model_name="text-davinci-003",temperature=1)
llm_chain = LLMChain(immediate=immediate, llm=llm)

# mannequin output
  • Create a template, saying that each query requested by the consumer is answered by the AI in a humorous means and taking an instance of a banana.
  • Then this template handed to the PromptTemplate() operate thus making a Immediate Template out of it.
  • Then with the OpenAI LLM wrapper, we initialize the OpenAI mannequin.
  • We now have imported the LLMChain from LangChain. LLMChain is without doubt one of the Easiest Chains offered by LangChain. To this, we move the Immediate Template and the Language Mannequin
  • Lastly, we move the question on to the llm_chain’s run() operate

What LLMChain does is, first it sends the enter question to the primary component within the chain, i.e. to the Immediate, i.e. the PromptTemplate. Right here the enter will get formatted to a specific Immediate. This formatted Immediate is then handed to the following component within the chain, i.e. the language mannequin. So a series could be thought of like a Pipeline. Let’s see the output generated.

Output: Machine Studying is sort of a banana. You retain giving it information and it slowly develops the talents it must make extra clever choices. It’s like a banana rising from just a little inexperienced fruit to a totally ripe and scrumptious one. With Machine Studying, the extra information you give it, the smarter it will get!

Right here we do get a solution to the query we’ve requested. The reply generated is each humorous and even accommodates the banana instance in it. So we would get a query now, can two chains be chained collectively? Nicely, the reply is totally YES. Within the subsequent instance, we will probably be doing precisely the identical. We’ll create two chains with two fashions, then we’ll chain them each collectively. The code for that is

from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain

# creating the primary template
first_template = """Given the IPL Staff Title, inform the 12 months through which they first
received the trophy.


team_template = PromptTemplate(input_variables=["team_name"], template=first_template)

# creating the team_chain that holds the 12 months informatino
team_chain = LLMChain(llm=llm, immediate=team_template)

# creating the second Template
second_template = """Given the 12 months, title the Highest Scoring Batsman within the IPL for that 12 months.
{12 months}

batsman_template = PromptTemplate(input_variables=["year"], template=second_template)

# creating the batsman_chain that holds the bastman info
batsman_chain = LLMChain(llm=llm, immediate=batsman_template)

# combining two LLMChains
final_chain = SimpleSequentialChain(chains=[team_chain, batsman_chain], verbose=True)

# checking the chain output
final_output ="Sunrisers Hyderabad")
  • Firstly, we’ve created two templates, the template one asks the mannequin for the 12 months through which a specific Cricket Staff received, and the second template, given the 12 months, tells the highest-scoring batsman
  • After creating the templates, then we create the PromptTemplates for each of them
  • Then we create our first chain, the team_chain, which accommodates our OpenAI Language Mannequin, which we’ve outlined within the first code and the primary template. The enter to our first template is the TEAM NAME
  • Then we create the second chain. This chain takes the identical mannequin however the template given is the second template. This chain takes within the YEAR and offers the highest-scoring IPL batsman in that 12 months.
  • Lastly, we then mix these two chains with the SimpleSequentialChain() operate and retailer it within the final_chain variable. Right here we move the chains within the type of a listing. It’s vital to make it possible for the chains are saved within the checklist in the identical order that they must be. We even set the verbose to True, so we are able to higher perceive the output

Now we’ve run the code by giving Sunrisers Hyderabad for the enter to our ultimate chain. The output returned was:

Building Applications with LLMs

The output produced is certainly True. So how did it do it? Firstly, the enter Sunrisers Hyderabad is fed to the primary chain i.e. the team_chain. The output of the team_chain is in line 1. So it returned the output. i.e. the 12 months 2016. The team_chain generates an output, which serves because the enter for the second chain, the batsman_chain. The batsman_chain takes the enter of the 12 months, particularly 2016, and produces the title of the highest-scoring IPL batsman in that 12 months. The second line shows the output of this chain. So that is how chaining/mixture of two or extra chains work particularly.

Once more, we’ve simply checked out solely a few of the Chaining ideas in LangChain. Builders can work with the Chains to create numerous purposes. They will additionally categorize the chains themselves, though this matter is past the scope of this text.

Brokers in LangChain

Regardless of their immense energy, Massive Language Fashions typically lack primary functionalities reminiscent of logic and calculation. They will battle with easy calculations that even small calculator packages can deal with extra successfully.

Brokers, then again, have entry to instruments and toolkits that allow them to carry out particular actions. For example, the Python Agent makes use of the PythonREPLTool to execute Python instructions. The Massive Language Mannequin offers directions to the agent on what code to run.


from langchain.brokers.agent_toolkits import create_python_agent
from import PythonREPLTool
from langchain.python import PythonREPL
from langchain.llms.openai import OpenAI

# creating Python agent
agent_executor = create_python_agent(
    llm=OpenAI(temperature=0, max_tokens=1000),
)"What's 1.12 raised to the facility 1.19?")

Right here to create the Python agent, we use the create_python_agent() object from the langchain. To this, we move our OpenAI Language Mannequin. And the software we’ll work is the PythonREPLTool() which is able to operating Python code. To get an in depth output, we set the verbose to True. Lastly, we run the mannequin with the enter to seek out the facility of 1.12 raised to 1.19. The output generated is

Python | Building Applications with LLMs

On this course of, the language mannequin generates Python code, which is then executed by the agent utilizing the PythonREPLTool(). The ensuing reply is returned by the language mannequin. Brokers transcend code execution and also can search Google for solutions when the language mannequin fails. These highly effective parts within the LangChain allow the creation of complicated fashions with excessive accuracy.


LangChain is a newly developed  Python Framework for constructing purposes with highly effective Language Fashions. LangChain proved a typical interface to work with a number of language fashions. The Parts in LangChain just like the Prompts, Chains, and brokers could be labored with to generate highly effective purposes. We now have discovered the best way to modify Prompts to make the Language Fashions produce totally different outputs. And in addition regarded into the best way to chain totally different language fashions too. Even labored with Brokers, that had been able to operating Python Code, which Massive Language Fashions can’t.

A few of the key takeaways from this text embrace:

  • The assist for various fashions in LangChain makes it simple to work with totally different Language Fashions.
  • Create brokers with LangChain to carry out duties that straightforward Language Fashions can’t
  • A number of queries could be fed into the Language Fashions by altering the Immediate Template.
  • Mix A number of Language Fashions along with Chain Parts to get correct solutions.
  • With LangChain, the time to vary from one Language Mannequin to a different is drastically decreased.

The media proven on this article will not be owned by Analytics Vidhya and is used on the Writer’s discretion.

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles