Skip to content

learnwithparam/sentiment-nlp-agents

Repository files navigation

Sentiment NLP Agents

learnwithparam.com

Build a multi-tool NLP agent that classifies sentiment, extracts entities and keywords, summarizes content, and detects emotion. Each task runs through a focused LLM prompt, orchestrated by a small agent graph that streams per-task progress over Server-Sent Events.

Start learning at learnwithparam.com. Regional pricing available with discounts of up to 60%.

What You'll Learn

  • Design a multi-task NLP agent with one focused prompt per task
  • Orchestrate sequential tool calls with an optional LangGraph StateGraph
  • Stream per-task progress back to the client with Server-Sent Events
  • Parse structured JSON safely from flexible LLM responses
  • Shape clean Pydantic response models from heterogeneous task outputs

Tech Stack (FastAPI, SSE, LangGraph-optional)

  • FastAPI - High-performance async Python web framework
  • Server-Sent Events - Real-time per-task progress streaming
  • LangGraph (optional) - Small StateGraph for the agent pipeline, with a plain-async fallback
  • LLM Provider Pattern - OpenRouter default, plus Fireworks, Gemini, OpenAI
  • Pydantic - Validation for request body and task-specific result shapes
  • Docker - Containerized development

Getting Started

Prerequisites

  • Python 3.11+
  • uv (installed automatically by make setup)
  • An API key from any supported LLM provider (OpenRouter has a free tier)

Quick Start

# One command to set up and run
make dev

# Or step by step:
make setup          # Create .env and install dependencies
# Edit .env with your API key
make run            # Start the FastAPI server

With Docker

make build          # Build the Docker image
make up             # Start the container
make logs           # View logs
make down           # Stop the container

API Documentation

Once running, open http://localhost:8000/docs for the interactive Swagger UI.

Try It

curl -X POST http://localhost:8000/sentiment/analyze \
  -H "Content-Type: application/json" \
  -d '{
    "text": "I absolutely love this new coffee shop! The barista was amazing.",
    "tasks": ["sentiment", "entities", "summary"]
  }'

For streaming per-task progress:

curl -N -X POST http://localhost:8000/sentiment/analyze/stream \
  -H "Content-Type: application/json" \
  -d '{"text":"Great product, terrible support.","tasks":["sentiment","emotion","summary"]}'

Challenges

Work through these incrementally to build the full application:

  1. The First Verdict - Wire a single classify_sentiment prompt end to end
  2. The Structured Result - Make the model emit strict JSON and parse it safely
  3. The Task Registry - Add entity, keyword, summary, and emotion tasks behind one interface
  4. The Agent Graph - Sequence the task calls through a LangGraph StateGraph
  5. The Streaming UI - Push per-task progress events over SSE
  6. The Selective Caller - Let the client pick a subset of tasks per request
  7. The Resilient Pipeline - Keep going when one task fails, surface errors per task
  8. The Router Upgrade - Route long text to summary-first, short text to sentiment-first (bonus)

Makefile Targets

make help           Show all available commands
make setup          Initial setup (create .env, install deps)
make dev            Setup and run (one command!)
make run            Start FastAPI server
make build          Build Docker image
make up             Start container
make down           Stop container
make clean          Remove venv and cache

Learn more

About

Sentiment NLP Agents

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors