🦜️🔗LangChain-01: Introduction

Exploring the Features and Benefits of LangChain

Sweety Tripathi
𝐀𝐈 𝐦𝐨𝐧𝐤𝐬.𝐢𝐨
7 min readJun 15, 2023

--

[Image by Author]

LangChain offers a unified interface that caters to various use cases, providing a standardized framework. Suppose you have developed a customized workflow and wish to make a similar transition, but this time utilizing a language model from Hugging Face instead of OpenAI. In that case, LangChain simplifies this process by making it as straightforward as adjusting a few variables. It is a wrapper for utilizing Large Language Models like GPT-4,BLOOM, Flan-t5 and connecting them to many tools (such as vector stores, database warehouses, dataloader)

In this Article I will demonstrate how to start with LangChain.

Why LangChain ?

  1. Simplify Development: LangChain offers Python library that helps developers with the necessary tools for working seamlessly to build application without much change in code. This saves time and effort for developers who would otherwise have to handle complex NLP tasks manually.
  2. Multiple Tool Integrations :

☁️ Cloud Storage: LangChain integrates with Amazon, Google, and Microsoft Azure cloud storage systems, allowing users to interact with files and data stored in these platforms.

📈 Few-Shot Learning: LangChain provides support for prompt generation in few-shot learning scenarios, allowing the development of models that can learn from limited examples.

📚 Language Models: LangChain incorporates language models from OpenAI, Anthropic, and Hugging Face, providing powerful natural language processing capabilities.

And much more like Database Support, Web Search, Google Drive Integration and so on…

Some of the popular tools that integrate with LangChain

3. Versatility for Multiple Use Cases: LangChain’s flexibility allows it to be employed in diverse scenarios, adapting to the specific needs of the application. It opens up possibilities for utilizing LLMs in various domains, including customer service, knowledge management, and more.

4. Open Source : LangChain was launched in October 2022 as an open source project by Harrison Chase, while working at machine learning startup Robust Intelligence.

How does LangChain Works?

LangChain provides a end-to-end pipeline from Input → Model selection → Prompt Creation → Output Generation. Below are main components of Langchain:

🦜️🔗 Models

Lang-chain allows you to swap easily between models. It enables access to a range of pre-trained LLMs from various providers . Below flowchart shows different models types and its provider that are used in LangChain.

Types of models and different LLM providers[Image by Author]

Example:

Open AI

By integrating the “OPEN_AI_KEY” one can connect directly with OpenAI By default Model is text-davinci-003. One can change model/engine name by changing model_name as parameter.

from langchain.llms import OpenAI

llm_openai = OpenAI(openai_api_key=OPEN_AI_KEY)
OR
llm_openai = OpenAI(openai_api_key=OPEN_AI_KEY,model_name= "text-davinci-003")

llm_openai("Lionel Messi, played for which club?")
## Lionel Messi has played for FC Barcelona since 2003.

HuggingFace Hub

By integrating the “HUGGINGFACEHUB_API_TOKEN” one can connect directly with huggingface hub and download any model available in Hub.I am using google/flan-t5-large model.

from langchain import HuggingFaceHub
import os

os.environ["HUGGINGFACEHUB_API_TOKEN"]='YOUR_KEY'
llm_hugging=HuggingFaceHub(repo_id="google/flan-t5-large")
llm_hugging("Lionel Messi, played for which club?")
#fc barcelona

🦜️🔗 Prompts

Prompt Prompt Everywhere…!!! The concept of garbage in, garbage out (GIGO) suggests that flawed or nonsensical input data leads to nonsensical output. Similarly, for prompts, prompt engineering is crucial in creating meaningful and effective results.

Prompt is an instruction or discussion topic a user provides for the AI model to respond to. The prompt can be a question, statement, or any other stimulus intended to spark creativity, reflection, or engagement.

Lang-chain provides a standard interface for string prompt templates and message PromptTemplate

# Note : Prompt template are task agnostic, this means that prompt templates can be easily customized and modified to suit different tasks and scenarios. By using variables and placeholders within the prompt templates, LangChain allows for dynamic input and customization based on the specific task at hand.This flexibility not only saves time but also simplifies the development process by providing a standardized approach for generating prompts in a wide range of applications.

How to create Prompt templates?

  1. Zero shot Example

PromptTemplate class can take any number of input variables, and can be formatted to generate a prompt.

from langchain.prompts import PromptTemplate

prompt = PromptTemplate(
input_variables=["text"],
template="Translate given text in french {text}?",
)
print(prompt.format(text="Hi how are you"))
#Output : Translate given text in french Hi how are you?

2. Few Shot Example

Langchain provides FewShotPromptTemplate class to pass few examples to help the language model generate a better response.

from langchain import PromptTemplate, FewShotPromptTemplate

# First, create the list of few shot examples.
examples = [
{"word": "happy", "antonym": "sad"},
{"word": "tall", "antonym": "short"},
]

# Next, we specify the template to format the examples we have provided.
# We use the `PromptTemplate` class for this.
example_formatter_template = """Word: {word}
Antonym: {antonym}
"""

example_prompt = PromptTemplate(
input_variables=["word", "antonym"],
template=example_formatter_template,
)

# Finally, we create the `FewShotPromptTemplate` object.
few_shot_prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix="Give the antonym of every input\n",
suffix="Word: {input}\nAntonym: ",
input_variables=["input"],
example_separator="\n",
)

print(few_shot_prompt.format(input="big"))
# -> Give the antonym of every input
# ->
# -> Word: happy
# -> Antonym: sad
# ->
# -> Word: tall
# -> Antonym: short
# ->
# -> Word: big
# -> Antonym:

3. Serialize prompt template

Langchain provide whole ecosystem of prompts at your end

🧑‍💻Load prompt template from LangChainHub, which contains a collection of useful prompts you can use in your project

🧑‍💻Load prompt template from your local system , it will automatically infer the file format through the file extension name.

#Save prompt file at local system
prompt_template.save("local_prompt.json")

#Load prompt file from local system
from langchain.prompts import load_prompt
loaded_prompt = load_prompt("local_prompt.json")

🦜️🔗Chains

Chain is “Hero🦸‍♂️” component in Langchain that stands out as a powerful tool that combines multiple models and prompts, allowing them to work together seamlessly within a single application.

from langchain.chains import LLMChain
chain = LLMChain(llm=llm_openai, prompt=prompt)

chain.run("Hi how are you")

🦜️🔗Memory

Chains and Agents are stateless, meaning that they treat each incoming query independently, after executing that particular task it forgets the previous conversational unlike in Chatbots where keeping track of conversation is highly important.

For Example : Lets add memory to a question/answering chatbot system that has multiple inputs.

from langchain.memory import ConversationBufferMemory
from langchain import OpenAI, LLMChain, PromptTemplate

template = """You are a chatbot having a conversation with a human.

{chat_history}
Human: {human_input}
Chatbot:"""

prompt = PromptTemplate(
input_variables=["chat_history", "human_input"],
template=template
)
memory = ConversationBufferMemory(memory_key="chat_history")

Chain all together — Model, Prompt, Memory

llm_chain = LLMChain(
llm=OpenAI(openai_api_key=OPEN_AI_KEY),
prompt=prompt,
verbose=True,
memory=memory,
)

Lets have a conversation with chatbot:

# Run below chain sequentially 
llm_chain.predict(human_input="Hi! I'm planning a trip to London on July 2023. Can you provide me with some information and recommendations?")
llm_chain.predict(human_input="I'm mainly interested in sightseeing spots and popular attractions.")
llm_chain.predict(human_input="That's great! I also need information about transportation within the city. ")
llm_chain.predict(human_input="Ohh I forgot the date ,When I was going to london could you plz tell me?")

The LLM can clearly remember the history of the conversation. Let’s take a look at how this conversation history is stored by the ConversationBufferMemory

🦜️🔗Indexes

Flowchart -How Index-Retriever system works[Image by Author]

There are 4 Main Steps for making Index-Retriever system using LangChain. It provides below wrapper

  • Document Loaders: The first step is to load the data into textloader
  • Text Splitters: Next split the documents into chunks.
  • VectorStores: Then select which embeddings want to use and create reate the vectorstore to use as the index.
  • Retrievers: Expose this index in a retriever interface by creating chain and takes in a query (a string) and returns a list of documents.

I will explain in detail how to build Langchain Index-Retriever system using Pinecone for Question-Answering Task in my next Article.

🦜️🔗Agents

Agents play a role when there is a requirement for direct interaction with the user and not a predetermined chain of systems. An agent has access to various tools. Depending on the user input, the agent can then decide which, if any, of these tools to call.

3 Important things while building Agent chain

🕵️Agent: Agents expose an interface that takes in user input along with tool to perform an action.

⚓Tool: These are the actions an agent can take.

⚒️Toolkit: These are groups of tools designed for a specific use case.

For example, creating a calculator tool from the existing load_tools :

Top Alternatives To LangChain

Image Source [WEB]

Cons of LangChain

  1. LangChain may have a learning curve, requiring time and effort to understand its functionalities. (probably need to do a lot more R&D!🤯)
  2. LangChain’s extensive capabilities may feel complex for straightforward tasks.
  3. Working with large language models and integrating tools can be computationally expensive.
  4. LangChain may not support all platforms or services.
  5. Regular maintenance and updates are required to ensure optimal performance and security.

Final thoughts

I find LangChain to be a game-changer in the field of natural language processing. Its user-friendly approach and flexible architecture make it stand out. But on the same time proper testing and validation are crucial to ensure the scalability, reliability and infrastructure support for production deployment.

Thank you for reading!🤗I hope that you found this article both informative and enjoyable to read.

Fore more information like this follow me on LinkedIn

References:

  1. https://python.langchain.com/en/latest/
  2. https://www.pinecone.io/learn/langchain/
  3. https://en.wikipedia.org/wiki/LangChain

Follow our Social Accounts- Facebook/Instagram/Linkedin/Twitter

Join AImonks Youtube Channel to get interesting videos.

--

--

Sweety Tripathi
𝐀𝐈 𝐦𝐨𝐧𝐤𝐬.𝐢𝐨

Data scientist with a interest in NLP/GenAI and a love for dance, exploring the intersection of art and technology.