My Langchain Adventure: A First-Time User’s Guide

Chirag Jain
5 min readSep 18, 2023

Large language models (LLMs) represent versatile, pre-trained, and fine-tuned models used for an array of tasks, including question answering, text generation, and text summarization. These models are built upon the transformer architecture and are trained on extensive datasets with a substantial number of parameters. The greater the volume of data and parameters, the more enhanced their performance becomes.

When it comes to developing LLMs, one notable aspect is that it doesn’t necessitate expertise in machine learning, nor does it require training examples. What it does demand, however, is proficiency in prompt design.

Prominent examples of LLMs include OpenAI’s GPT, Google’s PaLM, Meta’s LLAMA, and India’s inaugural open-source LLM developed by Tech Mahindra under Project Indus, which boasts a training corpus comprising an impressive 7 billion parameters, GenZ-70B top fine tuned LLM on Hugging Face developed over Meta LLAMA by Indians.

LangChain

Now, let’s shift our focus to LangChain. LangChain was introduced by Harrison Chase in October 2022, serves as the bridge connecting large Language Models to external sources. It operates by amalgamating a set of commands, collectively referred to as a “chain” in LangChain. This innovative framework empowers LLMs to harness resources that would typically be beyond their reach, such as accessing data from Google searches, Twitter, LinkedIn, and more.

Here is a simple application I have created using LangChain.

  1. Import modules from langchain package.
from langchain import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain

2. We will create a prompt template. The variable summary_template contains the input prompt to the LLM.

    summary_template = """
given the information {information} about a person I need you to generate :
1. A brief summary
2. Two random facts about he person
"""

To create a prompt template out of this prompt we will introduce a parameter {information}. This information variable can be changed every time while prompting a LLM.

A prompt is a direct instruction for an AI model, while a prompt template is a structured format with variable placeholders.

3. To create the prompt template using class PromptTemplate we are creating an object summary_prompt_template, while creating this object list of parameters and prompt will pass as input_variables and template arguments respectively. While interacting with the LLM we will populate these parameters as per our requirements.

summary_prompt_template = PromptTemplate(input_var1iables=["information"], template=summary_template)

4. Now, we are creating a LLM object of the ChatOpenAI class where we will introduce our LLM model to interact with and temperature as arguments. Temperature refers to the creativity of the LLM we are using, where 0 means giving generalized answers and 1 refers to max creativity.

llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo")

5. Next, we will amalgamate all the things into a LLMChain, we are creating a chain object of LLMChain class where we are passing information regarding our LLM and the prompt template as an argument to the model.

chain = LLMChain(llm=llm, prompt=summary_prompt_template)

6. After implementing this we will populate our information parameter which is gonna be valid while running the chain and then will print the output from our LLM.

print(chain.run(information=information))

Note:

  1. Create your pipenv and install langchain, openai packages.
  2. Be careful with the interpreter you are using.
  3. Fetch the OpenAI API Key and store is at as a key value pair in .env file (to be created by us in the project). In pycharm, store this api key as an environmental variable also.

Whole code for reference :

from langchain import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain


information = """
Elon Reeve Musk (/ˈiːlɒn/ EE-lon; born June 28, 1971) is a business magnate and investor. Musk is the founder, chairman, CEO and chief technology officer of SpaceX; angel investor, CEO, product architect and former chairman of Tesla, Inc.; owner, chairman and CTO of X Corp.; founder of the Boring Company; co-founder of Neuralink and OpenAI; and president of the Musk Foundation. He is the wealthiest person in the world, with an estimated net worth of US$226 billion as of September 2023, according to the Bloomberg Billionaires Index, and $249 billion according to Forbes, primarily from his ownership stakes in both Tesla and SpaceX.[4][5][6]

Musk was born in Pretoria, South Africa, and briefly attended the University of Pretoria before immigrating to Canada at age 18, acquiring citizenship through his Canadian-born mother. Two years later, he matriculated at Queen's University in Kingston, Ontario. Musk later transferred to the University of Pennsylvania, and received bachelor's degrees in economics and physics there. He moved to California in 1995 to attend Stanford University. However, Musk dropped out after two days and, with his brother Kimbal, co-founded online city guide software company Zip2. The startup was acquired by Compaq for $307 million in 1999, and with $12 million of the money he made, that same year Musk co-founded X.com, a direct bank. X.com merged with Confinity in 2000 to form PayPal.

In 2002, eBay acquired PayPal for $1.5 billion, and that same year, with $100 million of the money he made, Musk founded SpaceX, a spaceflight services company. In 2004, he became an early investor in electric vehicle manufacturer Tesla Motors, Inc. (now Tesla, Inc.). He became its chairman and product architect, assuming the position of CEO in 2008. In 2006, Musk helped create SolarCity, a solar-energy company that was acquired by Tesla in 2016 and became Tesla Energy. In 2013, he proposed a hyperloop high-speed vactrain transportation system. In 2015, he co-founded OpenAI, a nonprofit artificial intelligence research company. The following year, Musk co-founded Neuralink—a neurotechnology company developing brain–computer interfaces—and the Boring Company, a tunnel construction company. In 2022, he acquired Twitter for $44 billion. He subsequently merged the company into newly created X Corp. and rebranded the service as X the following year. In March 2023, he founded xAI, an artificial-intelligence company.

Musk has expressed views that have made him a polarizing figure. He has been criticized for making unscientific and misleading statements, including that of spreading COVID-19 misinformation, and promoting conspiracy theories. His Twitter ownership has been similarly controversial, including laying off a large number of employees, an increase in hate speech on the platform and changes to Twitter Blue verification were criticized. In 2018, the U.S. Securities and Exchange Commission (SEC) sued him for falsely tweeting that he had secured funding for a private takeover of Tesla. To settle the case, Musk stepped down as the chairman of Tesla and paid a $20 million fine.
"""

if __name__ == "__main__":
print("Hello LangChain")

summary_template = """
given the information {information} about a person I need you to generate :
1. A brief summary
2. Two random facts about he person
"""

summary_prompt_template = PromptTemplate(input_var1iables=["information"], template=summary_template)

llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo")

chain = LLMChain(llm=llm, prompt=summary_prompt_template)

print(chain.run(information=information))

Credits : https://www.udemy.com/share/108CTm3@dXipkaeTzObHG2uOT1gtDs6TlJesjpIu-BdxV2fWsi_GIM6yC4bM598gIDuXKqKRpQ==/

--

--