Learnitweb

Simple GenAI App Using Ollama

This tutorial shows how to build a local Generative AI web application using:

  • LangChain for prompt chaining
  • Ollama to run open-source LLMs locally
  • Streamlit for the web UI
  • Google Gemma (2B) or any other Ollama-supported model

1. Prerequisites

Before starting, make sure you have:

  • Python 3.9+
  • Ollama installed and running
  • At least one model downloaded (for example, Gemma)

Download the model once using:

ollama run gemma:2b

Once downloaded, it will be reused automatically.

2. requirements.txt (Complete)

Create a file named requirements.txt with the following content:

streamlit
langchain
langchain-community
langchain-core

Install dependencies:

pip install -r requirements.txt

3. .env file

LANGSMITH_TRACING=true
LANGSMITH_ENDPOINT=https://api.smith.langchain.com
LANGCHAIN_API_KEY=<put your langchain here>
LANGSMITH_PROJECT=testProject

These properties help you to track the LangChain application.

4. app.py

Below is the complete and runnable app.py.

from dotenv import load_dotenv
load_dotenv()
import streamlit as st

from langchain_community.llms import Ollama
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser


# --------------------------------------------------
# 1. Streamlit UI Configuration
# --------------------------------------------------
st.set_page_config(page_title="LangChain + Ollama Demo", layout="centered")

st.title("Generative AI App using LangChain & Ollama (Open Source)")
st.write(
    "This application uses a **local open-source LLM** running via Ollama. "
    "No OpenAI API key is required."
)


# --------------------------------------------------
# 2. User Input
# --------------------------------------------------
user_question = st.text_input(
    "What question do you have in mind?",
    placeholder="e.g. What is Generative AI?"
)


# --------------------------------------------------
# 3. Prompt Template
# --------------------------------------------------
prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are a helpful AI assistant. "
            "Answer the user's question clearly and concisely."
        ),
        (
            "human",
            "{question}"
        )
    ]
)


# --------------------------------------------------
# 4. Initialize LLM (Ollama)
# --------------------------------------------------
# IMPORTANT:
# The model name must already be downloaded via:
# ollama run gemma:2b
llm = Ollama(
    model="llama3",
    temperature=0.7
)


# --------------------------------------------------
# 5. Output Parser
# --------------------------------------------------
output_parser = StrOutputParser()


# --------------------------------------------------
# 6. Create LangChain Chain
# --------------------------------------------------
chain = prompt | llm | output_parser


# --------------------------------------------------
# 7. Run the Chain on User Input
# --------------------------------------------------
if user_question:
    with st.spinner("Generating response..."):
        response = chain.invoke(
            {"question": user_question}
        )

    st.subheader("AI Response")
    st.write(response)

5. Running the Application

Run Streamlit

streamlit run app.py

If successful, you will see:

  • A browser window opens automatically
  • A title: “Generative AI App using LangChain & Ollama”
  • A text input box for questions

6. Switching Models (Optional)

You can switch models by changing one line:

llm = Ollama(model="llama3")

or

llm = Ollama(model="llama2")

Just make sure the model is downloaded first:

ollama run llama3