27 minute read

LangChain Tutorial for Beginners: Build Your First AI App in 30 Minutes

Are you curious about Artificial Intelligence (AI) and how it creates amazing things like smart chatbots or story generators? You’re in the right place! We’re going to dive into LangChain, a super cool tool that helps you build AI apps easily. Think of it like a LEGO set for AI.

This LangChain tutorial for beginners will show you how to get started, even if you’ve never coded an AI app before. Our goal is to build your very first AI app in about 30 minutes, once you have your tools ready. Get ready to make your computer smart!

What is LangChain?

Imagine you have many different powerful AI brains, like ones that write stories, answer questions, or remember things. Connecting these brains to work together can be tricky. This is where LangChain comes in.

LangChain is a special framework that makes it simple to link these different AI pieces together. It helps you build powerful applications powered by large language models (LLMs). It’s like a conductor for an orchestra, making all the different AI instruments play in harmony.

Why is LangChain awesome for beginners?

LangChain takes complicated AI tasks and makes them simple. You don’t need to be an AI expert to start building cool stuff. It provides ready-made tools and building blocks that you can snap together.

This means you can focus on your app’s idea, not on the complex technical details. It’s designed to help you quickly prototype and experiment with AI. You’ll be surprised how fast you can create something fun and functional.

Getting Ready: Your Toolbox for LangChain

Before we jump into building, we need to gather a few essential tools. Don’t worry, these steps are straightforward. Think of it like setting up your workspace before starting a fun project.

Having the right setup will make your LangChain journey smooth. We’ll get everything installed and ready to go.

Getting Your API Keys

To make our AI apps work, they need to “talk” to powerful AI models that live on the internet. These models are like super smart brains, but they charge a small fee for their thoughts. To use them, you need an API key.

An API key is like a secret password that tells the AI model provider who you are. It allows your app to send requests and get responses from their service. We’ll mostly use OpenAI for this tutorial because it’s popular and easy to use.

OpenAI API Key

To get an OpenAI API key, you need to visit the OpenAI website. Create an account if you don’t have one already. Once logged in, look for the “API keys” section in your personal dashboard.

There, you can generate a new secret key. Make sure to copy this key and keep it somewhere safe, as you won’t be able to see it again after you close the window. Remember that using their services might incur small costs, so keep an eye on your usage.

Other LLM APIs

While we’re focusing on OpenAI, LangChain can work with many other LLMs too. You could use models from Google, Hugging Face, or Anthropic, for example. Each of these providers has its own way of generating API keys.

The process is generally similar: sign up, find the API key section, and generate your key. For this LangChain tutorial for beginners, sticking with OpenAI is a great starting point.

Setting Up Your Environment

Your computer needs a special place for our AI tools to live and work. This place is called a “development environment.” We’ll use Python, a popular programming language, as our main tool.

We’ll also set up a “virtual environment,” which keeps our project’s tools separate from other projects. This helps prevent conflicts and keeps things tidy.

Installing Python

Python is like the language our computer speaks to tell the AI what to do. If you don’t have Python installed, head over to the official Python website. Look for the latest stable version (usually 3.9 or newer is good).

Download the installer for your operating system (Windows, macOS, or Linux). Follow the installation steps carefully, and make sure to check the box that says “Add Python to PATH” during installation if you’re on Windows. This step is super important.

Setting Up a Virtual Environment

Once Python is installed, open your terminal or command prompt. Navigate to where you want to save your project. For example, you can create a folder called my_ai_app.

Inside that folder, run these commands:

1
python -m venv myenv

This command creates a new virtual environment named myenv. It’s like building a little isolated room for your project’s tools.

Now, you need to “activate” this environment. This means telling your computer to use the tools inside myenv for this project.

  • On Windows:
    1
    
    .\myenv\Scripts\activate
    
  • On macOS/Linux:
    1
    
    source myenv/bin/activate
    

You’ll see (myenv) appear at the beginning of your command prompt line. This tells you that your virtual environment is active. Now you’re ready to install LangChain!

Installing LangChain

With your environment ready, installing LangChain is super easy. It’s just one simple command. This command will download all the necessary LangChain pieces to your virtual environment.

Remember, you should always install packages within your activated virtual environment. This keeps your projects neat and organized.

The Magic Command

Make sure your virtual environment (myenv) is still active. Then, type this into your terminal:

1
pip install langchain langchain_openai

pip is Python’s package installer, and it fetches langchain and langchain_openai from the internet. langchain_openai is a specific part of LangChain that helps it talk to OpenAI models. This command will take a few moments to complete.

Once it’s done, LangChain is successfully installed! You now have the core tools to start building your AI app.

Installing Other Friends

Sometimes, LangChain needs help from other Python tools to do specific tasks. For example, if you want to save your chat history in a database or read from a PDF file. You might install other packages later.

For now, langchain and langchain_openai are all we need for this langchain tutorial for beginners. We’ll keep things simple and add more tools only when necessary.

Your First LLM Call: Talking to AI

Now for the exciting part: making your computer talk to a real AI! This is your first LLM call, and it’s the fundamental step in any LangChain application. We’ll send a simple question to the AI and get an answer back.

This direct interaction shows you how powerful these models are. It’s like having a super-smart assistant at your fingertips.

A Simple Chat

Let’s write a small Python program that asks the AI “What is the capital of France?”. We’ll use the OpenAI model for this.

First, you need to tell your computer your OpenAI API key. It’s best practice to put it in an environment variable, so it’s not directly in your code. This is safer.

To do this, create a file named .env in your project folder and add this line (replace your_openai_api_key_here with your actual key):

1
OPENAI_API_KEY="your_openai_api_key_here"

Then, in your Python script, you’ll load this key.

Code Snippet

Create a new Python file, for example, first_call.py, in your project folder. Put the following code inside it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

# Load environment variables from .env file
load_dotenv()

# Get the OpenAI API key
api_key = os.getenv("OPENAI_API_KEY")

# Make sure the API key is set
if not api_key:
    raise ValueError("OPENAI_API_KEY not found in environment variables.")

# Create a new ChatOpenAI model instance
# We specify the model name and the API key
llm = ChatOpenAI(model="gpt-3.5-turbo", openai_api_key=api_key)

# Ask the LLM a question
response = llm.invoke("What is the capital of France?")

# Print the AI's answer
print(response.content)

To run this code, you’ll also need to install the python-dotenv library to load your .env file:

1
pip install python-dotenv

Then, run your script from the terminal (making sure myenv is active):

1
python first_call.py

You should see something like “The capital of France is Paris.” printed in your terminal. How cool is that!

What Just Happened?

Let’s break down the code you just ran.

  1. load_dotenv(): This line looks for your .env file and loads the OPENAI_API_KEY into your program’s memory.
  2. llm = ChatOpenAI(...): This creates an instance of an OpenAI chat model. We told it which specific model to use (gpt-3.5-turbo) and gave it our secret API key.
  3. llm.invoke("..."): This is where we send our question to the AI. invoke means “to call upon” or “to start.” The AI processes our question and thinks of an answer.
  4. print(response.content): The AI sends back a response object. We want to see the actual text of its answer, which is stored in response.content.

This simple example shows the core interaction with an LLM. You give it input, and it gives you output.

Prompt Templates Basics: Guiding the AI

Asking the AI a simple question is a great start. But what if you want to ask similar questions many times, just changing one small detail? This is where prompt templates basics come in handy. They help you guide the AI with specific instructions and fill in the blanks.

Think of a prompt template like a fill-in-the-blanks letter. You write the general structure, and then you just add the specific details each time you use it. This makes your AI apps much more flexible.

Why Use Templates?

Prompt templates save you a lot of typing and ensure consistency. Instead of writing a full sentence every time, you can have a pre-written structure. For example, if you want to translate words, you can have a template like “Translate the following [word] into [language].”

You just provide the word and the language, and the template builds the full prompt for the AI. This is especially useful when you’re building apps that handle many different user inputs.

Making a Simple Template

Let’s create a template that asks the AI to tell a short story about a specific animal. We want to change the animal each time we use the template.

LangChain provides a ChatPromptTemplate that’s perfect for this. It helps you mix fixed text with placeholders.

Code Snippet

Let’s create a new Python file, prompt_template_example.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

# Load environment variables
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    raise ValueError("OPENAI_API_KEY not found in environment variables.")

llm = ChatOpenAI(model="gpt-3.5-turbo", openai_api_key=api_key)

# Create a prompt template
# The curly braces {} denote a placeholder
story_template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant that writes short, simple stories."),
        ("user", "Write a very short story about a {animal} that learns to fly."),
    ]
)

# Fill the template with a specific animal
# This creates a "prompt" ready to be sent to the LLM
animal_story_prompt = story_template.invoke({"animal": "penguin"})

# Now, send the generated prompt to the LLM
response = llm.invoke(animal_story_prompt)

# Print the AI's story
print(response.content)

Run this file from your terminal:

1
python prompt_template_example.py

You should see a short story about a penguin learning to fly! If you change {"animal": "penguin"} to {"animal": "elephant"}, you’ll get a story about an elephant.

The system message tells the AI its role or personality. The user message is the instruction or question from you.

Creating Simple Chains: Linking Steps

Now that you know how to talk to an AI and use templates, let’s learn about creating simple chains. Chains are like workflows for your AI app. They let you connect different steps together, so one step’s output becomes another step’s input.

This is where LangChain really shines! It helps you build complex AI applications by breaking them down into smaller, manageable pieces.

What are Chains?

Imagine you want to:

  1. Ask the AI to come up with some ideas for a story.
  2. Then, take those ideas and ask the AI to write the actual story.

You could do this in two separate steps, sending two different prompts. But with a chain, you can connect these steps automatically. LangChain’s LCEL (LangChain Expression Language) makes creating simple chains very intuitive.

A chain makes your AI app more powerful and automated. It’s like building an assembly line for your AI tasks.

Building Your First Simple Chain

We’ll create a chain that takes an animal name, uses a template to generate a story idea, and then uses another template to write a story based on that idea. This chain will have two main steps: a prompt and an LLM.

The | (pipe) symbol in LangChain is used to connect different components in a chain. It means “take the output of the left side and send it as input to the right side.”

Code Snippet

Let’s create simple_chain_example.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# Load environment variables
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    raise ValueError("OPENAI_API_KEY not found in environment variables.")

llm = ChatOpenAI(model="gpt-3.5-turbo", openai_api_key=api_key)
parser = StrOutputParser() # This helps get the plain text out of the LLM's response

# Define the first prompt template for coming up with an idea
idea_template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a creative assistant. Come up with a unique story idea."),
        ("user", "Suggest a short, fun story idea about a {creature}."),
    ]
)

# Define the second prompt template for writing the story
story_writer_template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a skilled storyteller. Write a very short story based on the following idea."),
        ("user", "Here is the story idea: {idea}\n\nWrite the story."),
    ]
)

# Build the chain!
# 1. Take the initial input (e.g., {"creature": "dragon"})
# 2. Pass it to the idea_template to generate a story idea prompt
# 3. Pass the generated prompt to the LLM to get the actual idea
# 4. Parse the LLM's idea response into a simple string
# 5. Take that idea string and put it into the story_writer_template
# 6. Pass the full story prompt to the LLM to write the story
# 7. Finally, parse the story response into a simple string

creative_story_chain = (
    idea_template
    | llm
    | parser
    | {"idea": parser}  # This step takes the parsed idea and maps it to the "idea" input for the next template
    | story_writer_template
    | llm
    | parser
)

# Invoke the chain with an input for the first template
result = creative_story_chain.invoke({"creature": "dragon"})

print(result)

You’ll need langchain_core for StrOutputParser. Install it:

1
pip install langchain-core

Then run the script:

1
python simple_chain_example.py

You will get a complete story about a dragon, with the AI first coming up with an idea and then writing the story! This is a powerful demonstration of creating simple chains.

Putting it All Together: Simple Chain Example

Let’s break down what creative_story_chain does:

  • idea_template: This is the start. It takes our input {"creature": "dragon"}.
  • | llm: The output of idea_template (a prompt) is sent to our ChatOpenAI model. The LLM generates a story idea.
  • | parser: The LLM’s raw response is then parsed into a clean string (just the text of the idea).
  • | {"idea": parser}: This is a crucial step! It takes the plain string (our generated story idea) and maps it to a new dictionary {"idea": "our generated story idea"}. This is because the story_writer_template expects an input named idea.
  • | story_writer_template: This new dictionary (with the story idea) is now fed into our story_writer_template. It generates a new prompt to write the actual story.
  • | llm: This second prompt is sent to the LLM again, which now writes the full story.
  • | parser: Finally, the full story response is parsed into a clean string.

This entire sequence happens with one invoke call on creative_story_chain. This is the beauty of LangChain’s chains!

Building Your First AI App: The 30-Minute Challenge

Alright, it’s time for the langchain tutorial for beginners challenge! We’ll build a complete, small AI application that generates simple stories based on your input. We’ll use everything we’ve learned so far. Our goal is to have a working story generator in roughly 30 minutes.

This app will demonstrate how to combine prompts and LLMs into a functional tool. You’ll be amazed at what you can create in such a short time.

The Idea: A Simple Story Generator

Our AI app will ask you for a character and a setting. Then, it will use these inputs to create a short, fun story. For example, if you say “a brave knight” and “a magical forest,” the AI will write a story about a brave knight’s adventure in a magical forest.

This app is simple enough for a beginner but powerful enough to show LangChain’s capabilities. It’s a great running first example of a practical AI application.

Step 1: Plan Your Story

Before coding, let’s think about the prompts we’ll need. We need to tell the AI its role and what information we’ll provide.

  • System Message: “You are a creative storyteller who writes short, happy stories.”
  • User Message: “Write a short story about a {character} in a {setting}.”

This single prompt template will be enough for our simple story generator. We’ll combine it directly with the LLM.

Step 2: Get Your Prompt Template Ready

We’ll use ChatPromptTemplate.from_messages just like we did before. This makes it easy to define roles for the AI.

The placeholders {character} and {setting} will allow us to customize the story each time. This setup ensures our app is flexible and interactive.

Step 3: Build the Chain

Our chain will be very straightforward for this langchain tutorial for beginners app. It will have three main parts:

  1. The ChatPromptTemplate to build the specific story request.
  2. The ChatOpenAI LLM to process the request and generate the story.
  3. The StrOutputParser to get the clean text output.

Connecting these components with the | pipe symbol creates our story-generating workflow.

Step 4: Run Your App!

Now, let’s put it all into a single Python file.

Code Snippet

Create a file named story_app.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from datetime import datetime

# --- Setup: This part is similar to what we did before ---
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    raise ValueError("OPENAI_API_KEY not found in environment variables.")

# Initialize the LLM
llm = ChatOpenAI(model="gpt-3.5-turbo", openai_api_key=api_key, temperature=0.7) # Added temperature for more creative output

# Define the prompt template for our story generator
story_template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a creative storyteller who writes short, happy stories for children. Keep them simple and engaging."),
        ("user", "Write a short story about a {character} who lives in a {setting}. Make sure the story has a happy ending."),
    ]
)

# Define the output parser to get plain text
parser = StrOutputParser()

# Build the chain
# Input -> Prompt Template -> LLM -> Output Parser
story_chain = story_template | llm | parser

# --- Interactive part of the app ---
print("Welcome to the AI Story Generator!")
print("Let's create a fun story together.")

while True:
    character_input = input("\nEnter a main character (e.g., 'a brave squirrel', 'a little robot'): ")
    if character_input.lower() == 'exit':
        break

    setting_input = input("Enter a magical setting (e.g., 'a enchanted forest', 'a candy land'): ")
    if setting_input.lower() == 'exit':
        break

    print(f"\nGenerating a story about {character_input} in {setting_input}...")
    start_time = datetime.now()

    # Invoke the chain with user inputs
    try:
        story = story_chain.invoke({"character": character_input, "setting": setting_input})
        end_time = datetime.now()
        time_taken = (end_time - start_time).total_seconds()
        print("\n--- Your Generated Story ---")
        print(story)
        print(f"\n(Story generated in {time_taken:.2f} seconds)")
    except Exception as e:
        print(f"\nOops! Something went wrong: {e}")
        print("Please check your API key and internet connection.")

    print("\n---")
    print("Type 'exit' for character or setting to quit.")

print("\nThank you for using the AI Story Generator! Goodbye!")

Let’s Test it Out!

Save the file as story_app.py and run it from your activated virtual environment:

1
python story_app.py

The program will ask you for a character and a setting. Type your inputs and press Enter. In a few moments, the AI will generate a unique story for you! You can keep generating stories until you type ‘exit’.

Congratulations! You’ve just built your first interactive AI application using LangChain. This running first example truly shows the power and simplicity of the framework for beginners.

Basic Debugging: When Things Go Wrong

Even the best programmers encounter errors. It’s a natural part of coding! When building your langchain tutorial for beginners apps, you might hit some bumps. Basic debugging is about figuring out why your code isn’t working as expected.

Don’t get discouraged if your app doesn’t work perfectly the first time. Debugging is like being a detective, finding clues to solve the mystery.

Common Errors and How to Fix Them

Here are some common issues you might face with LangChain and LLMs:

  • ValueError: OPENAI_API_KEY not found...:
    • Reason: Your OPENAI_API_KEY is not loaded correctly.
    • Fix: Double-check your .env file name and location (it should be in the same folder as your Python script). Make sure the key itself is correct and that load_dotenv() is called at the beginning of your script.
  • AuthenticationError (from OpenAI):
    • Reason: Your API key is incorrect, expired, or has insufficient permissions.
    • Fix: Go to your OpenAI dashboard, check your API keys, and make sure your account has active billing. Generate a new key if needed.
  • RateLimitError (from OpenAI):
    • Reason: You’ve sent too many requests too quickly, or you’ve exceeded your usage limits.
    • Fix: Wait a few seconds before trying again. For more serious limits, check your OpenAI account’s usage dashboard. You might need to upgrade your plan for higher limits.
  • NameError (e.g., name 'ChatOpenAI' is not defined):
    • Reason: You forgot to import a necessary class or function.
    • Fix: Look at the top of your script. Did you include from langchain_openai import ChatOpenAI?
  • Wrong output from LLM / LLM not following instructions:
    • Reason: Your prompt might not be clear enough, or the temperature setting is too high/low.
    • Fix: Refine your prompt template. Be very specific with your instructions. Try adjusting the temperature parameter when initializing ChatOpenAI (e.g., temperature=0.7 for more creativity, temperature=0.1 for more factual/less creative).
  • Missing Python packages:
    • Reason: You forgot to install a library like python-dotenv or langchain-core.
    • Fix: The error message will usually tell you which package is missing (e.g., ModuleNotFoundError: No module named 'dotenv'). Use pip install [package-name] to install it.

Using verbose=True

LangChain has a built-in helper for basic debugging called verbose=True. When you add verbose=True to certain LangChain components (like an LLM or a chain), it prints out extra information about what’s happening behind the scenes.

This includes the exact prompt being sent to the LLM and the raw response received back. It’s incredibly useful for understanding why your AI is behaving the way it is.

To use it, simply add verbose=True when you initialize your LLM or chain:

1
2
3
4
5
6
7
8
9
# With LLM
llm = ChatOpenAI(model="gpt-3.5-turbo", openai_api_key=api_key, temperature=0.7, verbose=True)

# With Chain
story_chain = story_template | llm | parser
# If you want to make the whole chain verbose:
# from langchain.chains import LLMChain # (This is an older way, but shows verbose)
# from langchain_core.runnables import RunnableSequence # For LCEL, usually set verbose on components
# You can often get similar verbose behavior from setting debug flags

For LCEL chains (like story_template | llm | parser), setting verbose=True on the llm component is often sufficient to see the prompts and responses. For more advanced debugging, you can use LangChain’s tracing tools (like LangSmith), but that’s a topic for a more advanced langchain tutorial for beginners beyond this one.

Deploying a Simple App

You’ve built a cool AI app! Now, how do you show it to your friends or let others use it? This is where deploying simple app comes in. Deployment means making your application available for others to access, usually over the internet.

For our langchain tutorial for beginners app, we won’t do a full-blown deployment to a server. Instead, we’ll look at easy ways to turn your Python script into a simple web interface.

What Does “Deploy” Mean?

When you run python story_app.py on your computer, only you can use it. Deploying means taking your code and putting it on a public server. This server runs your app, and anyone with the correct web address (URL) can use it through their web browser.

It sounds complex, but for simple apps, there are beginner-friendly tools that make it much easier. You don’t need to be a web developer to share your AI creations.

Simple Ways to Share

Two popular Python libraries make it easy to create simple web interfaces for your apps: Gradio and Streamlit. They handle the web parts, so you can focus on your LangChain code.

We’ll briefly look at how you might use them.

Using Gradio

Gradio is fantastic for quickly creating web UIs for machine learning models. You define your input and output components, and Gradio builds the interface for you.

Here’s how you could adapt our story_app.py for Gradio:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import gradio as gr # You'll need to install gradio

# --- Setup: Same as before ---
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    raise ValueError("OPENAI_API_KEY not found in environment variables.")

llm = ChatOpenAI(model="gpt-3.5-turbo", openai_api_key=api_key, temperature=0.7)

story_template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a creative storyteller who writes short, happy stories for children. Keep them simple and engaging."),
        ("user", "Write a short story about a {character} who lives in a {setting}. Make sure the story has a happy ending."),
    ]
)
parser = StrOutputParser()
story_chain = story_template | llm | parser

# --- Gradio specific part ---
def generate_story_gradio(character, setting):
    """Function to be called by Gradio UI."""
    try:
        story = story_chain.invoke({"character": character, "setting": setting})
        return story
    except Exception as e:
        return f"Oops! Something went wrong: {e}\nPlease check your API key and internet connection."

# Create the Gradio interface
iface = gr.Interface(
    fn=generate_story_gradio,
    inputs=[
        gr.Textbox(label="Main Character (e.g., 'a brave squirrel')"),
        gr.Textbox(label="Magical Setting (e.g., 'an enchanted forest')")
    ],
    outputs="textbox",
    title="AI Story Generator with LangChain",
    description="Enter a character and a setting, and let AI write a happy story!",
    theme="soft"
)

# Launch the Gradio app
iface.launch()

To run this, first install Gradio:

1
pip install gradio

Then run the script. It will open a web page in your browser where you can interact with your app. You can even share a public link provided by Gradio for a short time! This makes deploying simple app very accessible.

Using Streamlit

Streamlit is another excellent tool for creating data apps and dashboards with Python. It allows you to turn data scripts into interactive web apps in minutes.

Here’s a snippet showing how our story_app.py could look with Streamlit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import streamlit as st # You'll need to install streamlit

# --- Setup: Same as before ---
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    st.error("OPENAI_API_KEY not found in environment variables. Please set it in your .env file.")
    st.stop() # Stop the app if API key is missing

llm = ChatOpenAI(model="gpt-3.5-turbo", openai_api_key=api_key, temperature=0.7)

story_template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a creative storyteller who writes short, happy stories for children. Keep them simple and engaging."),
        ("user", "Write a short story about a {character} who lives in a {setting}. Make sure the story has a happy ending."),
    ]
)
parser = StrOutputParser()
story_chain = story_template | llm | parser

# --- Streamlit specific part ---
st.set_page_config(page_title="AI Story Generator", page_icon="📖")
st.title("📖 AI Story Generator with LangChain")
st.write("Enter a character and a setting, and let AI write a happy story for you!")

with st.form("story_form"):
    character_input = st.text_input("Main Character", "a curious cat")
    setting_input = st.text_input("Magical Setting", "a sparkling garden")
    submitted = st.form_submit_button("Generate Story")

    if submitted:
        if not character_input or not setting_input:
            st.warning("Please enter both a character and a setting!")
        else:
            with st.spinner("Generating your story..."):
                try:
                    story = story_chain.invoke({"character": character_input, "setting": setting_input})
                    st.success("Here is your story!")
                    st.markdown(f"**Character:** {character_input}")
                    st.markdown(f"**Setting:** {setting_input}")
                    st.write(story)
                except Exception as e:
                    st.error(f"Oops! Something went wrong: {e}\nPlease check your API key and internet connection.")

st.sidebar.markdown("---")
st.sidebar.info("This app uses LangChain and OpenAI to generate stories.")

To run this, first install Streamlit:

1
pip install streamlit

Then, from your terminal in the project folder, run:

1
streamlit run your_streamlit_app.py

Streamlit will also open a tab in your web browser. Both Gradio and Streamlit simplify deploying simple app significantly for beginners.

What’s Next for Deployment?

For more robust applications that need to handle many users or be always online, you would look into cloud platforms like AWS, Google Cloud, or Azure. These platforms offer services like virtual machines or serverless functions to host your applications. However, for a langchain tutorial for beginners, Gradio and Streamlit are perfect for getting your feet wet.

Beyond the Basics: What’s Next?

You’ve completed this langchain tutorial for beginners and built your first AI app! That’s a huge accomplishment. But this is just the beginning of what you can do with LangChain. The framework is incredibly powerful and offers many more advanced features.

Here are some ideas and concepts you can explore next:

  • Memory: Make your AI apps remember past conversations. LangChain offers various memory types to store chat history, allowing your AI to have more natural, continuous conversations.
  • Agents: Teach your AI to use tools! Agents allow your LLM to decide which tools to use (like searching the web, calling a calculator, or running code) to achieve a goal. This makes AI incredibly versatile.
  • Retrieval Augmented Generation (RAG): Connect your LLM to external knowledge bases. This means your AI can answer questions using information from specific documents, databases, or websites, not just its general training data. Imagine an AI that can answer questions about your company’s internal documents! You can learn more about this in our upcoming post on [Building a Q&A Bot with RAG].
  • Document Loaders & Embeddings: Learn how to load different types of documents (PDFs, text files, web pages) into your AI app and convert them into “embeddings” for searching and retrieval. This is a core component of RAG.
  • Callbacks: Understand how to monitor and log the different steps in your LangChain chain. This is great for debugging and understanding how your AI thinks.
  • Custom Tools & Chains: Build your own custom tools for agents or design more complex chains to perform multi-step reasoning tasks.
  • Different LLMs: Experiment with other large language models like Google’s Gemini, Anthropic’s Claude, or open-source models available through Hugging Face. LangChain’s modular design makes it easy to swap them out.
  • Evaluation: How do you know if your AI app is working well? Learn techniques to evaluate the performance and accuracy of your LangChain applications.

Keep experimenting, building, and learning. The world of AI is constantly evolving, and LangChain provides an excellent foundation for you to be a part of it.

Conclusion

You’ve done it! You started with What is LangChain, went through installing LangChain and setting up environment, made your first LLM call, understood prompt templates basics, and mastered creating simple chains. Most importantly, you built your first AI app, a story generator, demonstrating a fantastic running first example.

You even got a peek into basic debugging and deploying simple app concepts. This langchain tutorial for beginners has equipped you with fundamental skills to start your AI development journey. The path ahead is full of exciting possibilities.

Keep exploring, keep building, and have fun making your computers smarter! The AI future is yours to shape.

Leave a comment