Beginner's Guide for LangChain

LangChain is a Basically a Open Source Python Package to build applications powered by Large Language Models such as ChatGPT.

To install LangChain, in the terminal, run:

pip install langchain

To use LangChain with OpenAI, you need to use the langchain-openai package.
To use LangChain with gemini, you need to use the langchain-google-genai.

But for this article we use OpenAI.

In the terminal, run:

pip install langchain-openai

Once both the packages are installed, let’s create a simple LLM chain.

Imagine getting the power of ChatGPT in a single line of code.

from langchain_openai import ChatOpenAI
llm = ChatOpenAI(api_key="<Insert your Open AI API key here>”)

To use the LLM, this one line of code is enough:

llm.invoke("What is the first law of thermodynamics?")

The output is:

AIMessage(content='The first law of thermodynamics, also known as the law of energy conservation, states that energy cannot be created or destroyed in an isolated system. It can only change forms or be transferred from one object to another. This law is also known as the principle of conservation of energy.', response_metadata={'finish_reason': 'stop', 'logprobs': None})

Suppose we want to provide some standard information along with the input question, we can use a prompt template.

from langchain_core.prompts import ChatPromptTemplate

For example, a prompt template would be:

[

(“system”,”You are an expert Physics teacher.”),

(”user”,” {input}”)

]

The prompt template consists of two parts: the system message and the user message. The system message is used to provide a persona to the LLM or standard instructions for the LLM. The user message consists of the user input.

Create the prompt using the prompt template as below:

prompt = ChatPromptTemplate.from_messages([
("system","You are an expert Physics teacher."),
("user"," {input}")
])

So, now we have an LLM and a prompt created from a prompt template.

We can create a pipeline consisting of the prompt and the LLM in a simple chain:

chain = prompt | llm

To run this chain, use

chain.invoke({"input":"What is the first law of thermodynamics?"})

The response is:

AIMessage(content='The first law of thermodynamics, also known as the law of energy conservation, states that energy cannot be created or destroyed in an isolated system. It can only change forms or be transferred from one object to another. In other words, the total energy of a closed system remains constant over time. This principle is often expressed mathematically as ΔU = Q - W, where ΔU is the change in internal energy of the system, Q is the heat added to the system, and W is the work done by the system.', response_metadata={'finish_reason': 'stop', 'logprobs': None})

To make the output simple and appear as a string, we can use the below code:

from langchain_core.output_parsers import StrOutputParser
output_parser = StrOutputParser()

We can link the output_parser as below:

chain = prompt | llm | output_parser

and invoke the chain with the same question input

chain.invoke({"input":"What is the first law of thermodynamics?"})

The output is now:

'The first law of thermodynamics, also known as the law of energy conservation, states that energy cannot be created or destroyed in an isolated system. It can only change forms or be transferred from one part of the system to another. In other words, the total energy of a closed system remains constant over time. This principle is a fundamental concept in thermodynamics and plays a key role in understanding energy transformations in various physical processes.'

Thus, we have set up a basic LLM Chain using LangChain.

If you liked it , do share it with someone who may need it ! ! 😀

10
Subscribe to my newsletter

Read articles from Anuj Kumar Upadhyay directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anuj Kumar Upadhyay
Anuj Kumar Upadhyay

I am a developer from India. I am passionate to contribute to the tech community through my writing. Currently i am in my Graduation in Computer Application.