Build an LLM Web App in Python from Scratch: Part 1 (Local CLI)

Ever thought about sprinkling some AI magic into your web app? This is Part 1 of our journey from a basic command-line tool to a full-blown AI web app. Today, we're building a "Human-in-the-Loop" (HITL) chatbot. Think of it as an AI that politely asks for your "OK" before it says anything. We'll use a tiny, super-simple tool called PocketFlow – it's just 100 lines of code! So, You Want to Add AI to Your Web App? Adding AI (especially those smart Large Language Models or LLMs) to web apps can make them super powerful. Imagine smart chatbots, instant content creation, or even coding help. But hold on, it's not just "plug and play." You'll bump into questions like: Where does the AI brain actually live? In the user's browser? On your server? How do you handle AI tasks that need multiple steps? How can users tell the AI what to do or give feedback? What's the deal with remembering conversation history? Most importantly: How do you stop the AI from saying something totally weird or wrong? This tutorial series is your guide to building LLM web apps in Python, and we'll tackle these questions head-on! Here's our 4-part adventure: Part 1 (That's Today!): We'll build a basic Human-in-the-Loop (HITL) chatbot that runs in your computer's command line. We'll use PocketFlow to nail down the core logic, no fancy UI to distract us. Part 2: Let's get this HITL bot on the web! We'll build a user interface using Streamlit (or maybe Flask), and learn how to manage user interactions smoothly. Part 3: Time for real-time action! We'll upgrade our web app with WebSockets (using FastAPI) for instant messages and a slicker chat feel. Part 4: What about AI tasks that take a while? We'll set up background processing and use Server-Sent Events (SSE) with FastAPI. This lets us show users live progress updates, making our app feel truly pro. To make this journey smooth, we'll use PocketFlow. It's not just for simple AI calls; PocketFlow helps you build complex AI workflows that you can actually control. And because it's so small (seriously, just 100 lines for the core framework), you'll always know what's going on. No black boxes! The secret sauce to control and quality? A pattern called Human-in-the-Loop (HITL). With PocketFlow, HITL is easy. It means your AI is like a helpful assistant: it drafts stuff, but you (the human co-pilot) get the final say. Approve, edit, or reject – you're in control before anything goes live. Total quality control, phew! You can try out the code for the command-line HITL bot discussed in this part at: PocketFlow Command-Line HITL Example. Your First HITL App: The "Are You Sure?" Bot Let's build the most basic HITL app: a chatbot where you approve every single AI response. Imagine it's an AI trying to tell jokes. AI can be funny, but sometimes its jokes might be... well, not funny, or maybe even a little weird or inappropriate. That's where you come in! The Basic Idea (It's Super Simple!) Think of a tiny script. The AI suggests a joke, and you, the human, give it a thumbs-up or thumbs-down before it's told. That's HITL in action! If the AI suggests, "Why did the chicken cross the playground? To get to the other slide!" and you think it's a winner, you say "yes!" If it suggests something that makes no sense, or isn't right for your audience, you say "nope!" user_topic_request = "Tell me a joke about cats." ai_suggested_joke = call_llm_for_joke(user_topic_request) # You get to approve! approval = input(f"AI suggests: '{ai_suggested_joke}'. Tell this joke? (y/n): ") if approval.lower() == 'y': print(f"To Audience: {ai_suggested_joke}") else: print("Human said: Nope! That joke isn't hitting the mark.") That's the core! AI suggests a joke, human (input()) approves, then it's (maybe) told. This simple check is crucial for joke quality and appropriateness. Everything else we build is about making this core idea robust for more complex apps. Visually, it's a little loop, especially for our joke bot: Let's See It Go! You: Tell me a programming joke. AI suggests: 'Why do programmers prefer dark mode? Because light attracts bugs!' Approve? (y/n): y To Audience: Why do programmers prefer dark mode? Because light attracts bugs! You: Tell me a joke about vegetables. AI suggests: 'Why did the tomato turn red? Because it saw the salad dressing! But also, all vegetables are boring.' Approve? (y/n): n Human said: Nope! That joke isn't hitting the mark. (And the comment about vegetables is a bit much!) Regenerating... (Imagine we have logic for this) AI suggests: 'What do you call a sad strawberry? A blueberry!' Approve? (y/n): y To Audience: What do you call a sad strawberry? A blueberry! See? The human caught that slightly off-kilter joke and the unnecessary comment. That's HITL making sure the AI comedian doesn't bomb too hard! Why Just Plain Python Gets Tangled Fast When you try to build something more than a quic

Jun 2, 2025 - 00:20
 0
Build an LLM Web App in Python from Scratch: Part 1 (Local CLI)

Ever thought about sprinkling some AI magic into your web app? This is Part 1 of our journey from a basic command-line tool to a full-blown AI web app. Today, we're building a "Human-in-the-Loop" (HITL) chatbot. Think of it as an AI that politely asks for your "OK" before it says anything. We'll use a tiny, super-simple tool called PocketFlow – it's just 100 lines of code!

So, You Want to Add AI to Your Web App?

Adding AI (especially those smart Large Language Models or LLMs) to web apps can make them super powerful. Imagine smart chatbots, instant content creation, or even coding help. But hold on, it's not just "plug and play." You'll bump into questions like:

  • Where does the AI brain actually live? In the user's browser? On your server?
  • How do you handle AI tasks that need multiple steps?
  • How can users tell the AI what to do or give feedback?
  • What's the deal with remembering conversation history?
  • Most importantly: How do you stop the AI from saying something totally weird or wrong?

This tutorial series is your guide to building LLM web apps in Python, and we'll tackle these questions head-on! Here's our 4-part adventure:

  • Part 1 (That's Today!): We'll build a basic Human-in-the-Loop (HITL) chatbot that runs in your computer's command line. We'll use PocketFlow to nail down the core logic, no fancy UI to distract us.
  • Part 2: Let's get this HITL bot on the web! We'll build a user interface using Streamlit (or maybe Flask), and learn how to manage user interactions smoothly.
  • Part 3: Time for real-time action! We'll upgrade our web app with WebSockets (using FastAPI) for instant messages and a slicker chat feel.
  • Part 4: What about AI tasks that take a while? We'll set up background processing and use Server-Sent Events (SSE) with FastAPI. This lets us show users live progress updates, making our app feel truly pro.

To make this journey smooth, we'll use PocketFlow. It's not just for simple AI calls; PocketFlow helps you build complex AI workflows that you can actually control. And because it's so small (seriously, just 100 lines for the core framework), you'll always know what's going on. No black boxes!

The secret sauce to control and quality? A pattern called Human-in-the-Loop (HITL). With PocketFlow, HITL is easy. It means your AI is like a helpful assistant: it drafts stuff, but you (the human co-pilot) get the final say. Approve, edit, or reject – you're in control before anything goes live. Total quality control, phew!

You can try out the code for the command-line HITL bot discussed in this part at: PocketFlow Command-Line HITL Example.

Your First HITL App: The "Are You Sure?" Bot

Let's build the most basic HITL app: a chatbot where you approve every single AI response. Imagine it's an AI trying to tell jokes. AI can be funny, but sometimes its jokes might be... well, not funny, or maybe even a little weird or inappropriate. That's where you come in!

The Basic Idea (It's Super Simple!)

Think of a tiny script. The AI suggests a joke, and you, the human, give it a thumbs-up or thumbs-down before it's told. That's HITL in action! If the AI suggests, "Why did the chicken cross the playground? To get to the other slide!" and you think it's a winner, you say "yes!" If it suggests something that makes no sense, or isn't right for your audience, you say "nope!"

user_topic_request = "Tell me a joke about cats."
ai_suggested_joke = call_llm_for_joke(user_topic_request)

# You get to approve!
approval = input(f"AI suggests: '{ai_suggested_joke}'. Tell this joke? (y/n): ")

if approval.lower() == 'y':
    print(f"To Audience: {ai_suggested_joke}")
else:
    print("Human said: Nope! That joke isn't hitting the mark.")

That's the core! AI suggests a joke, human (input()) approves, then it's (maybe) told. This simple check is crucial for joke quality and appropriateness. Everything else we build is about making this core idea robust for more complex apps. Visually, it's a little loop, especially for our joke bot:

Image description

Let's See It Go!

You: Tell me a programming joke.
AI suggests: 'Why do programmers prefer dark mode? Because light attracts bugs!'
Approve? (y/n): y
To Audience: Why do programmers prefer dark mode? Because light attracts bugs!

You: Tell me a joke about vegetables.
AI suggests: 'Why did the tomato turn red? Because it saw the salad dressing! But also, all vegetables are boring.'
Approve? (y/n): n
Human said: Nope! That joke isn't hitting the mark. (And the comment about vegetables is a bit much!)
Regenerating... (Imagine we have logic for this)
AI suggests: 'What do you call a sad strawberry? A blueberry!'
Approve? (y/n): y
To Audience: What do you call a sad strawberry? A blueberry!

See? The human caught that slightly off-kilter joke and the unnecessary comment. That's HITL making sure the AI comedian doesn't bomb too hard!

Why Just Plain Python Gets Tangled Fast

When you try to build something more than a quick demo with plain Python while loops and if statements, things can turn into a bowl of spaghetti code REAL quick. The main headaches are: