An introduction to Langchain

This lesson will teach you about Langchain, an open-source framework for building AI applications.

What is Langchain?

LangChain is designed to accelerate the development of LLM applications.

Langchain enables software developers to build LLM applications quickly, integrating with external components and data sources.

Developers can build Langchain applications with Python or JavaScript/TypeScript.

Langchain supports multiple LLMs and allows you to swap one for another with a single parameter change.

Meaning you can quickly test multiple LLMs for suitability and utilize different models for different use cases.

Langchain provides out-of-the-box integrations with APIs and databases including Neo4j.

Langchain’s flexibility allows you to test different LLM providers and models with minimal code changes.

How does it work?

LangChain applications bridge users and LLMs, communicating back and forth with the LLM through Chains.

The key components of a Langchain application are:

  • Model Interaction (Model I/O): Components that manage the interaction with the language model, overseeing tasks like feeding inputs and extracting outputs.

  • Data Connection and Retrieval: Retrieval components can access, transform, and store data, allowing for efficient queries and retrieval.

  • Chains: Chains are reusable components that determine the best way to fulfill an instruction based on a prompt.

  • Agents: Agents orchestrate commands directed at LLMs and other tools, enabling them to perform specific tasks or solve designated problems.

  • Memory: Allow applications to retain context, for example, remembering the previous messages in a conversation.

Click to reveal an example Langchain application

This example program uses Langchain to build a chatbot that answers questions about Neo4j Cypher queries.

The program interacts with an OpenAI LLM, uses a prompt template to instruct the LLM on how to act, and uses a memory component to retain context.

After completing this module, you will understand what this program does and how it works.

python
from langchain_openai import ChatOpenAI
from langchain.prompts.prompt import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain.agents import AgentExecutor, create_react_agent
from langchain.tools import Tool
from langchain import hub

chat_llm = ChatOpenAI(
    openai_api_key="sk-..."
)

memory = ConversationBufferMemory(memory_key="chat_history", input_key="input", return_messages=True)

prompt = PromptTemplate(template="""You are a Neo4j expert having a conversation about how to create Cypher queries

Chat History: {chat_history}
Question: {input}
""", input_variables=["chat_history", "input"])

chat_chain = LLMChain(
    llm=chat_llm,
    prompt=prompt,
    memory=memory)

tools = [
    Tool.from_function(
        name="ChatOpenAI",
        description="Talk about Neo4j Cypher queries",
        func=chat_chain.run,
        return_direct=True,
    )
]

agent_prompt = hub.pull("hwchase17/react")
agent = create_react_agent(chat_llm, tools, agent_prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, handle_parse_errors=True)

while True:
    q = input("> ")
    print(agent_executor.invoke({"input": q}))

In the next lesson, you will set up your development environment and use Langchain to query an LLM.

Check Your Understanding

Programming Languages

What programming languages can you build a Langchain application in?

Select all that apply.

  • ✓ JavaScript

  • ✓ Python

  • ✓ TypeScript

Hint

Langchain offers libraries written in multiple programming languages.

Solution

The correct answers are Python, JavaScript and TypeScript.

Lesson Summary

In this lesson, you learned about Langchain, an open-source framework for building AI applications.

In the next lesson, you will learn how to set up your development environment and use Langchain to query an LLM.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?