Published on

Getting started with Langchain & a simple Langchain app

What are Large Language Models?

LLM

Figure 1: Cross Section of a LLM. Image Credit AI Data Drive

Large Language Models (LLMs) are advanced AI systems designed to understand and generate human-like text. These models are trained on vast amounts of data, enabling them to predict the next word in a sentence, generate coherent responses, and perform various language-related tasks with remarkable accuracy. Think of them as supercharged brains that can process vast amounts of text data, learn patterns, and generate coherent responses.

What is Langchain?

A framework for developing applications powered by Large Language Models.

It enables applications that:

  • Are context-aware: connect a language model to sources of context (prompt instructions, few shot examples, content to ground its response in, etc.)

  • Reason: rely on a language model to reason (about how to answer based on provided context, what actions to take, etc.)

LangChain aims to link powerful LLMs to external data sources, enabling the creation of natural language processing (NLP) applications across various industries.

  • Introduced October 2022
  • Used for applications such as text summarization, question answering, chatbots, coding assistants

Need for Langchain

By providing an open-source framework, LangChain streamlines the process of creating natural language processing (NLP) applications across various industries. LangChain addresses the need for simplifying the development of applications using large language models (LLMs) like GPT-3.5 and GPT-4 1. By providing an open-source framework, LangChain streamlines the process of creating natural language processing (NLP) applications across various industries, offering more comprehensive support in different use cases such as developing chatbots, sentiment analysis tools, and other NLP applications2. As NLP technology continues to evolve and grow in importance, platforms like LangChain become increasingly valuable for developers looking to stay ahead in the world of NLP2.

The key needs that LangChain fulfills include:

  1. Connecting LLMs to External Data Sources: LangChain allows developers to link powerful LLMs to external data sources, enabling applications to access recent data without being limited by the models' original training data1.

  2. Simplifying Application Development: By offering a framework with flexible customization options and powerful components, LangChain makes it easier for developers to create a wide variety of applications across different industries, such as text summarization, question answering, and chatbots 31.

  3. Enhancing User Experience: LangChain helps in creating more natural and engaging user experiences by leveraging its framework and components to build chatbots powered by language models that can generate context-aware responses for various purposes like general conversation or support inquiries1.

  4. Providing Reusable Components: The framework offers reusable components like model interaction, data connection and retrieval, chains for linking multiple LLMs, agents for decision-making, prompt templates, indexes, and retrievers, which simplify the development process and allow for efficient creation of advanced NLP applications3.

Overall, LangChain meets the growing demand for advanced NLP applications by providing a user-friendly framework that empowers developers to leverage the capabilities of large language models effectively across diverse use cases and industries1.

Important Langchain Components and Concepts

LLM

Figure 2: LLMs and Langchain. Image Credit AI Data Drive

Chain

A Chain in LangChain is a sequence of steps that can be executed to perform a specific task. Chains allow you to combine multiple components, such as LLMs, tools, and memory, into a reusable workflow. They provide a way to structure the flow of information and decision-making in your application, making it easier to build complex, multi-step AI systems. Imagine a Chain as a series of connected steps, like a production line. Each step in the Chain performs a specific task, and the output of one step becomes the input for the next. This allows you to build complex workflows by chaining together various components, such as language models, data sources, and decision-making algorithms.

Prompt Template

Prompt Templates in LangChain are a way to structure the input prompts that are passed to language models. They allow you to define placeholders for dynamic content, which can be filled in at runtime based on the specific context of the application. This helps ensure consistency and clarity in the prompts used, which is crucial for getting reliable outputs from the language model. Think of a Prompt Template as a fill-in-the-blank form. You can define placeholders in the template, and then fill in those placeholders with dynamic content at runtime. This helps you maintain consistency in the prompts you send to your language models, ensuring they receive the information they need to generate high-quality responses.

LLM Chains

LLM Chains are a specific type of Chain in LangChain that focus on integrating large language models (LLMs) into the workflow. These Chains handle the interaction with the LLM, including passing the appropriate prompts, handling the model's output, and potentially chaining multiple LLMs together to accomplish more complex tasks. Imagine an LLM Chain as a pipeline that takes in a prompt, sends it to the language model, and then processes the model's output to generate the final result.

Sequential Chains

Sequential Chains in LangChain are a type of Chain that execute a series of steps in a linear fashion. Each step in the sequence takes the output of the previous step as its input, allowing you to build multi-step workflows that leverage the capabilities of various components, such as LLMs, tools, and memory. They are like a set of dominoes, where each step in the Chain relies on the output of the previous step. As you push the first domino, the rest of the Chain falls in a predetermined order, with the final output being the result of the entire sequence.

What's the difference between SimpleSequentialChain & SequentialChain ?

  1. Complexity: SimpleSequentialChain: This is a simpler and more straightforward implementation of a sequential chain, where the output of one step is directly fed as the input to the next step. SequentialChain: This is a more complex and flexible implementation of a sequential chain, which allows for more advanced features and customization.
  2. Input/Output Handling: SimpleSequentialChain: The input to the first step and the output of the final step are directly passed to and from the chain. SequentialChain: The input and output of each step can be mapped and transformed using custom functions, allowing for more complex data processing. With, SimpleSequentialChains, one can view only the final output, instead of all the outputs including the intermediate ones.
  3. Memory Management: SimpleSequentialChain: Does not have built-in memory management capabilities. SequentialChain: Supports the use of a memory object, which can be used to store and retrieve data between steps in the chain.
  4. Callbacks: SimpleSequentialChain: Supports a basic set of callbacks, such as on_chain_start and on_chain_end. SequentialChain: Provides a more extensive set of callbacks, allowing for more granular control and monitoring of the chain's execution.

Conversation Memory Buffer

The Conversation Memory Buffer in LangChain is a component that stores the history of a conversation, allowing the language model to maintain context and reference previous interactions. This is particularly useful for building chatbots, virtual assistants, and other conversational AI applications, where the model needs to understand the full context of the conversation to provide relevant and coherent responses. It is like a scrapbook that keeps track of all the previous interactions in a conversation. Whenever a new message is added, the Conversation Memory Buffer updates the scrapbook, allowing the language model to reference past context and maintain a coherent dialogue.

The LangChain Ecosystem

Imagine LangChain as a toolbox filled with various components that work together to build powerful AI applications. Each component plays a specific role, like the different tools in a toolbox, and when combined, they allow you to tackle complex tasks with ease.

Models

These are the language models themselves, like GPT-3 or ChatGPT, that provide the core AI capabilities. They're the "engines" that power your applications.

Prompts

Prompts are like the "instructions" you give to the language models. They're the way you communicate your needs and guide the models to generate the desired outputs.

Indexes

Indexes are the "filing cabinets" that store and organize the data your application needs to access, making it easy to retrieve relevant information.

Memory

Memory is the "notepad" that keeps track of the context and history of your conversations or workflows, allowing the language models to maintain coherence and continuity.

Agents

Agents are the "decision-makers" that can intelligently choose which tools to use and how to use them to accomplish a given task, making your applications more autonomous and adaptable.

Bringing it All Together

Imagine you're building a virtual assistant application. The language model would be the "brain" that generates the responses, the prompts would be the "instructions" you give it, the indexes would be the "knowledge base" it can draw from, the memory would be the "context" it remembers, the chains would be the "workflows" it follows, and the agents would be the "decision-makers" that choose the best course of action.


Developing a simple Langchain Application 👨🏽‍💻

Requirements & Setup:

  1. Python > 3.8.1

  2. Create and activate a virtual environment

terminal
virtualenv myenv
(OR)
python -m venv myenv

myenv\Scripts\activate

Code

langchain_example.py
from dotenv import load_dotenv
import os
from langchain_community.llms import OpenAI
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SequentialChain, SimpleSequentialChain
from langchain.memory import ConversationBufferMemory
import streamlit as st

load_dotenv()

# Read the value of OPENAI_API_KEY from the .env file
openai_api_key = os.getenv('OPENAI_API_KEY')

#Set the title for the page
st.title('A simple Langchain Application')

#Set an input box
input_text = st.text_input('Enter a topic...')

#I/P Prompt

topic_prompt = PromptTemplate(
    input_variables=['topic'],
    template='Write an essay on {topic}.'
)

details_prompt = PromptTemplate(input_variables=['context'], template='Identify and enumerate the individuals involved (who), locations mentioned (where) and reasons or motivations outlined (why) within the given essay: {context}')

coordinates_prompt = PromptTemplate(input_variables=['details'], template='Generate the coordinates of the locations in {details}')

#LLM

llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key)

#Memory

context_memory = ConversationBufferMemory(input_key='topic', memory_key='chat_history')
details_memory = ConversationBufferMemory(input_key='context', memory_key='chat_history')
coordinates_memory = ConversationBufferMemory(input_key='details', memory_key='details_history')

#chain
topic_chain = LLMChain(llm=llm, prompt=topic_prompt, verbose=True, output_key='context', memory=context_memory)
details_chain = LLMChain(llm=llm, prompt=details_prompt, verbose=True, output_key='details', memory=details_memory)
coordinates_chain = LLMChain(llm=llm, prompt=coordinates_prompt, verbose=True, output_key='coordinates', memory=coordinates_memory)

main_chain = SequentialChain(chains=[topic_chain, details_chain, coordinates_chain], verbose=False, input_variables=['topic'], output_variables=['context','details','coordinates'])

if input_text:
    st.write(main_chain({'topic':input_text}))

    with st.expander('Details'):
        st.info(details_memory.buffer)
    with st.expander('Coordinates'):
        st.info(coordinates_memory.buffer)

Output

output
Video 1: Output of the Langchain App

Footnotes

  1. https://www.ibm.com/topics/langchain 2 3 4 5

  2. https://www.youtube.com/watch?v=_FpT1cwcSLg&list=PLZoTAELRMXVORE4VF7WQ_fAl0L1Gljtar&index=2 2

  3. https://python.langchain.com/docs/get_started/introduction 2