Building LLM Applications with LangChain: A Hands-On Guide
In this blog, we build a simple LLM-powered application using LangChain, covering key components like LLMs, prompts, output parsers, and chaining, while also demonstrating how to deploy the app using
👋 Hey! This is Manisha Arora from PrepVector. Welcome to the Tech Growth Series, a newsletter that aims to bridge the gap between academic knowledge and practical aspects of data science. My goal is to simplify complicated data concepts, share my perspectives on the latest trends, and share my learnings from building and leading data teams.
In Part 1, we explored the LangChain framework—how it simplifies building LLM-powered applications by providing modular components like chains, retrieval strategies, embeddings, and vector stores.
Now, it’s time to move beyond concepts and build something real. In this post, we’ll walk through a hands-on tutorial to create an LLM-powered application using LangChain. We’ll cover:
🔹 Setting up LangChain and integrating an LLM
🔹 Using chains to structure interactions
🔹 Adding memory, retrieval, and output parsing for a more robust app
By the end, you’ll have a working LLM-powered application that goes beyond simple API calls and leverages LangChain’s full potential.
Let’s dive in! 🚀
About the Authors:
Arun Subramanian: Arun is an Associate Principal of Analytics & Insights at Amazon Ads, where he leads development and deployment of innovative insights to optimize advertising performance at scale. He has over 12 years of experience and is skilled in crafting strategic analytics roadmap, nurturing talent, collaborating with cross-functional teams, and communicating complex insights to diverse stakeholders.
Manisha Arora: Manisha is a Data Science Lead at Google Ads, where she leads the Measurement & Incrementality vertical across Search, YouTube, and Shopping. She has 12 years experience in enabling data-driven decision making for product growth.
Building blocks of our LLM application
Now that we learnt about the LangChain architecture, let’s put that to use to build a simple LLM application. For our first app, we are going to use the below building blocks.
LLMs:
LangChain provides interfaces for interacting with various LLMs, both from different providers (e.g., OpenAI, Cohere) and open-source models (e.g., Ollama, HuggingFace), Most popular LLMs are usually part of integration packages. For our app, we are going to use Llama 3.2 via Ollama. Below is a sample code
from langchain_ollama import ChatOllama
llm = ChatOllama(
model = "llama3.2",
temperature = 0.8,
num_predict = 256,
)
Prompts:
LangChain includes tools for creating and managing prompts, which are the inputs provided to language models. For our app, we are going to use ChatPromptTemplate. Prompts are part of the core package. Here is a sample code
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate([
("system", "You are a helpful AI bot. Your name is {name}."),
("human", "Hello, how are you doing?"),
("ai", "I'm doing well, thanks!"),
("human", "{user_input}"),
])
prompt_value = template.invoke(
{
"name": "Bob",
"user_input": "What is your name?"
}
)
Output Parsers:
Output parsers help to structure and format the output from LLMs. Common output parsers are part of the core package. For our app, we will use StrOutputParser to convert the LLM's output into a string.
Chains:
Chains are the core building blocks of LangChain applications. They represent sequences of operations that process data, often involving LLM calls. For our app, we are going to chain together the 3 components in sequence - llm, prompt and output parser. Below is a sample code
from langchain_core.output_parsers import StrOutputParser
output_parser=StrOutputParser()
chain=prompt|llm|output_parser
response=chain.invoke({"input":"Can you tell me about Langsmith?"})
print(response)
Monitoring and Evaluation
Enabling tracing via LangSmith allows you to closely monitor and evaluate your application. You can access the “Tracking projects” page in https://smith.langchain.com/ to learn details about how many LLM calls your app made, what were the different human prompts and LLM responses, latency for each call, costs incurred, etc. Below is a sample screenshot from the calls made from our app.
Bringing everything together in Streamlit
Streamlit is an open-source Python framework for data scientists and AI/ML engineers to deliver dynamic data apps with only a few lines of code. For our demo application, we are going to build a simple Streamlit UI to intake user input and output LLM response. Below is a sample code used to build our app.
## Streamlit framework
st.title("Langchain Demo With Llama 3.2 Model")
st.subheader("Chat with the model")
input_text=st.text_input("Enter your question here:")
if input_text:
with st.spinner("Generating answer..."):
answer = chain.invoke({"question":input_text})
st.subheader("Answer:")
st.write(answer)
You can find the complete code in the app.py file from this GitHub repo. Finally, you can launch your Streamlit app by running streamlit run app.py. Below is a sample screenshot of your launched app
Conclusion
This hands-on introduction provides a basic example of how LangChain can be used to build a production grade LLM application. By abstracting away the complexities of LLM interaction and providing tools for prompt management and chaining, LangChain simplifies the development process and enables developers to create powerful and interactive applications powered by language models. If you found this interesting, look forward to our next article where we will implement a LLM Retrieval Augmented Generation (RAG) application utilizing more components from the LangChain framework.
Frequently Asked Questions
1. What is Ollama? How can I run LLMs using it?
Ollama is a lightweight, extensible framework for building and running language models on the local machine. It provides a simple API for creating, running, and managing models, as well as a library of pre-built models that can be easily used in a variety of applications. To use local LLMs using Ollama, download the app from
https://ollama.com/. Once you have Ollama installed, download the LLM model of your choice to local. You can find different models available in Ollama from their GitHub repo - https://github.com/ollama/ollama
# list Ollama models in your local machine
ollama list
# download llama3.2 model to local machine
Ollama pull llama3.2
2. How to sign up on LangChain and get an API key?
Go to https://www.langchain.com/. Sign up for a new account. Go to settings page and generate a new API key. Site would automatically guide you to copy all the required environment variables. Paste them into a .env file and ensure that you add it to .gitignore.
3. What if I want to use OpenAI instead of Ollama ?
Biggest advantage of LangChain is the ability to easily switch between different LLM model providers. To use OpenAI instead of Ollama, update your code with the below snippet. Ensure you have the OpenAI API Key in your .env file.
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o")
References:
https://python.langchain.com/docs/introduction/
Check out my upcoming courses:
Master Product Sense and AB Testing, and learn to use statistical methods to drive product growth. I focus on inculcating a problem-solving mindset, and application of data-driven strategies, including A/B Testing, ML, and Causal Inference, to drive product growth.
AI/ML Projects for Data Professionals
Gain hands-on experience and build a portfolio of industry AI/ML projects. Scope ML Projects, get stakeholder buy-in, and execute the workflow from data exploration to model deployment. You will learn to use coding best practices to solve end-to-end AI/ML Projects to showcase to the employer or clients.
Not sure which course aligns with your goals? Send me a message on LinkedIn with your background and aspirations, and I'll help you find the best fit for your journey.