Building LLM Applications with LangChain: A Hands-On Guide
In this blog, we will build a simple LLM-powered application using LangChain components like LLMs, prompts, output parsers, chains, and deploy the app using Streamlit frontend
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 with Ollama Llama3.2
🔹 Using chains to construct a sequential call to prompt, LLM and output parsers
🔹 Deploying our solution as a Streamlit app and monitoring it live using LangSmith
By the end, you’ll have a working LLM-powered application that goes beyond simple API calls and demonstrates LangChain’s value addition.
Note - For those seeking a deeper dive into the underlying code after reading the post, you can find the complete code in this GitHub repo.
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
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, # controls the creativity of generation text
num_predict = 256, # controls the number of output tokens
)
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)
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")