7

Introduction to LangChain: LangChain Basics

7

Today we start something new! We start a series of videos on Langchain. I wanted to create more actionable content with more continuity in the long term to make sure people can learn and utilize that learning in their job. My goal is that this content on Langchain will help people apply those concepts to build great Large Language Model applications. This is the first video of many, and we cover some of the LangChain basics. Let me know if you like it!


What is LangChain

When you have a typical large language model, the way to interact with it is to ask a question and you get an answer. But it's pretty isolated from the rest of the world. You cannot really do much more than that.

So, if you want to build applications, you may want to connect your large language model to data. You may want to give it access to tools. You may want to give it memory access. And you may want to automate the process of prompt engineering. LangChain allows you to connect all those different aspects to build software applications.

LangChain basics

So let's demonstrate some capabilities of LangChain. Let's pip install it along the OpenAI python package:

! pip install langchain openai

We set up the OpenAI API key as part of the environment variables

import os

os.environ['OPENAI_API_KEY'] = ...

LLMs

We can import the OpenAI’s GPT-3 model and ask it questions

from langchain.llms import OpenAI

llm = OpenAI()

llm.predict('How are you?')

> "\n\nI'm doing well, thank you. How about you?"

We can also import the underlying model behind ChatGPT

from langchain.chat_models import ChatOpenAI

chat_model = ChatOpenAI()

chat_model.predict('How are you?')

> "As an AI, I don't have feelings, but I'm here to help you with any questions or tasks you have. How can I assist you today?"

The problem with raw LLMs is that they don’t remember the history of the conversations

chat_model.predict('What was my previous question?')

> "I'm sorry, but I am an AI language model and I don't have access to your previous questions."

Chains

We can create a chain that is going to help us augment the LLM

from langchain.chains import ConversationChain

chain = ConversationChain(
    llm=chat_model,
    verbose=True
)

chain.run('How are you today?')

As part of the prompt, we see now that there is a bit more to my simple prompt. We have a system prompt, the history of the conversation, and the human’s question. The system prompt allows us to give the LLM more context on what needs to be done. Let’s see if it remembers:

chain.run('What was my previous question?')

Prompt templates

With LangChain, we can automate a lot of the prompt engineering and for that, we can use prompt templates. Let’s create a prompt template

from langchain.prompts import PromptTemplate

template = """
Return all the subcategories of the following category

{category}
"""

prompt = PromptTemplate(
    input_variables=['category'],
    template=template
)

‘Category’ is an input variable that will be used once we run the chain. Let’s input it into a chain. We use LLMChain, which is the simplest chain we can use

from langchain.chains import LLMChain

chain = LLMChain(
    llm=chat_model,
    prompt=prompt,
    verbose=True
)

chain.run('Machine Learning')

We can also break down the prompts into the system and human prompts. This is helpful when we build chatbots

from langchain.prompts import (
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
    ChatPromptTemplate
)

system_template = """
You are a helpful assistant who generate comma separated lists.
A user will only pass a category and you should generate subcategories of that category in a comma separated list.
ONLY return comma separated and nothing more!
"""

human_template = '{category}'

system_message = SystemMessagePromptTemplate.from_template(
    system_template
)

human_message = HumanMessagePromptTemplate.from_template(
    human_template
)

Ans we can combine the 2 prompts into one:

prompt = ChatPromptTemplate.from_messages([
    sytem_message, human_message   
])

chain = LLMChain(
    llm=chat_model,
    prompt=prompt,
    verbose=True
)


chain.run('Machine Learning')

This gives us more control of the LLM’s output, but we can go further by using an output parser.

Output parser

Let’s overwrite the base output parser and generate Python lists from the LLM’s response:

from langchain.schema import BaseOutputParser

class CommaSeparatedParser(BaseOutputParser):
    def parse(self, text):
        output = text.strip().split(',')
        output = [o.strip() for o in output]
        return output
    
chain = LLMChain(
    llm=chat_model,
    prompt=prompt,
    output_parser=CommaSeparatedParser(),
    verbose=True
)

chain.run('Machine Learning')

We can also feed the chain with multiple inputs:

input_list = [
    {'category': 'food'},
    {'category': 'country'},
    {'category': 'colors'}
]

response = chain.apply(input_list)
response[2]['text']

Simple Sequence

We can also compose chains. Here we are creating a pipeline of chains, where the output of one chain is used as input in the next one. We create 2 chains: a title chain and a synopsis to automate the process of writing a play. First, we have a title chain:

title_template = """
You are a writer. Given a subject, 
your job is to return a fun title for a play.

Subject: {subject}
Title:"""

title_chain = LLMChain.from_string(
    llm=chat_model,
    template=title_template
)

title_chain.run('Machine Learning')

> '"The Algorithmic Adventure: A Machine Learning Marvel"'

And a synopsis chain:

synopsis_template = """
You are a writer. 
Given a title, write a synopsis for a play.

Title: {title}
Synopsis:"""

synopsis_chain = LLMChain.from_string(
    llm=chat_model,
    template=synopsis_template
)

title = "The Algorithmic Adventure: A Machine Learning Marvel"

synopsis_chain.run(title)

Let’s now combine those 2 chains by passing them as a list to the simple sequential chain:

from langchain.chains import SimpleSequentialChain

chain = SimpleSequentialChain(
    chains=[title_chain, synopsis_chain],
    verbose=True
)

chain.run('Machine Learning')

Potentially an interesting play!

This concludes our introduction to LangChain basics. I hope you had some fun. We will continue our introduction to Langchain in the next video. See you there!

7 Comments
The AiEdge Newsletter
The AiEdge Newsletter
Authors
Damien Benveniste