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 and store the history in Neo4j.
After completing this module, you will understand what this program does and how it works.
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_react_agent
from langchain.tools import Tool
from langchain import hub
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain.schema import StrOutputParser
from langchain_community.chat_message_histories import Neo4jChatMessageHistory
from langchain_community.graphs import Neo4jGraph
from uuid import uuid4
SESSION_ID = str(uuid4())
print(f"Session ID: {SESSION_ID}")
llm = ChatOpenAI(openai_api_key="sk-...")
graph = Neo4jGraph(
url="bolt://localhost:7687",
username="neo4j",
password="pleaseletmein"
)
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a Neo4j expert having a conversation about how to create Cypher queries",
),
("human", "{input}"),
]
)
cypher_chat = prompt | llm | StrOutputParser()
def get_memory(session_id):
return Neo4jChatMessageHistory(session_id=session_id, graph=graph)
tools = [
Tool.from_function(
name="Cypher Support",
description="For when you need to talk about Cypher queries.",
func=cypher_chat.invoke,
)
]
agent_prompt = hub.pull("hwchase17/react-chat")
agent = create_react_agent(llm, tools, agent_prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
cypher_agent = RunnableWithMessageHistory(
agent_executor,
get_memory,
input_messages_key="input",
history_messages_key="chat_history",
)
while True:
q = input("> ")
response = cypher_agent.invoke(
{
"input": q
},
{"configurable": {"session_id": SESSION_ID}},
)
print(response["output"])
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.