🌱 0️⃣ What We Are Building
We are building a Science Teacher AI chatbot backend.
This AI:
- answers science questions
- explains like teacher
- remembers chat
- ignores non-science
Example:
Student: What is gravity?
AI: Gravity is a force…
Student: explain again
AI: As I explained earlier…
👉 AI remembers context.
🧠 1️⃣ How System Works
Flow:
Student → API → Filter → LangChain → OpenAI → Answer
↑
Memory
Steps:
- Student sends question
- Express API receives
- Science filter checks
- LangChain adds memory
- OpenAI generates answer
- API returns response
🧰 2️⃣ Technologies
- Node.js → backend
- Express → API
- LangChain → LLM framework
- OpenAI → AI brain
- BufferMemory → chat memory
- dotenv → API key
- pnpm → package manager
📦 3️⃣ Create Project
mkdir science-teacher-bot
cd science-teacher-bot
pnpm init
📦 4️⃣ Install Dependencies
pnpm add express cors dotenv langchain @langchain/openai nodemon
📁 5️⃣ Folder Structure
science-teacher-bot/
│
├── src/
│ ├── memory.mjs
│ ├── llm.mjs
│ ├── filter.mjs
│ ├── route.mjs
│ └── server.mjs
│
├── .env
└── package.json
🔐 6️⃣ OpenAI Key
Create .env
OPENAI_API_KEY=your_key_here
PORT=3000
💾 7️⃣ memory.mjs
import { BufferMemory } from "langchain/memory";
export const memory = new BufferMemory({
returnMessages: true,
memoryKey: "chat_history",
});
What it does
- stores chat history
- remembers messages
- gives context to AI
🧠 8️⃣ llm.mjs
import "dotenv/config";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";
import { memory } from "./memory.mjs";
const llm = new ChatOpenAI({
model: "gpt-4o-mini",
temperature: 0.3,
});
const template = `
You are a science teacher.
Answer only science questions.
Explain in simple way.
Conversation:
{chat_history}
Student: {input}
Teacher:
`;
export const chatChain = new ConversationChain({
llm,
memory,
prompt: template,
});
What it does
- creates AI model
- defines teacher behavior
- connects memory
- creates chatbot
🔬 9️⃣ filter.mjs
export function isScience(text) {
const words = [
"physics","chemistry","biology",
"atom","cell","energy","force",
"gravity","plant","reaction",
"photosynthesis","molecule"
];
return words.some(w =>
text.toLowerCase().includes(w)
);
}
What it does
- checks if question is science
- blocks other topics
🌐 1️⃣0️⃣ route.mjs
import express from "express";
import { chatChain } from "./llm.mjs";
import { isScience } from "./filter.mjs";
export const router = express.Router();
router.post("/", async (req, res) => {
const { text } = req.body;
if (!text) {
return res.json({
error: "Question required",
});
}
if (!isScience(text)) {
return res.json({
answer: "I only answer science questions.",
});
}
const response = await chatChain.predict({
input: text,
});
res.json({ answer: response });
});
What it does
- receives question
- validates text
- checks science
- calls AI
- sends answer
🚀 1️⃣1️⃣ server.mjs
import express from "express";
import cors from "cors";
import "dotenv/config";
import { router } from "./route.mjs";
const app = express();
app.use(cors());
app.use(express.json());
app.use("/ask", router);
app.get("/", (req, res) => {
res.send("Science Teacher AI running");
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log("Server running on", PORT);
});
What it does
- creates server
- enables JSON
- mounts API route
- starts backend
▶️ 1️⃣2️⃣ Run Project
Add script in package.json
"scripts": {
"dev": "node src/server.mjs"
}
Run:
pnpm dev
🧪 1️⃣3️⃣ Test API
POST → http://localhost:3000/ask
Body:
{
"text": "What is photosynthesis?"
}
Response:
{
"answer": "Photosynthesis is the process..."
}
🧠 1️⃣4️⃣ Memory Demo
Ask:
- What is gravity
- explain again
AI remembers context.
⚠️ 1️⃣5️⃣ Important Notes
Current memory:
- shared for all users
- resets on restart
Real apps use:
- DB memory
- session ID
- vector store
🏆 1️⃣6️⃣ What You Built (Hero Level)
You created:
✅ LLM backend
✅ LangChain integration
✅ Memory chatbot
✅ Science filter
✅ Express API
✅ Teacher AI
This is real AI app architecture.
🎓 1️⃣7️⃣ How to Explain to Students
We built an AI teacher using OpenAI.
Express receives student questions.
LangChain connects AI and memory.
Memory stores conversation.
Filter ensures science-only answers.


SOCIAL SHARE CARD GENERATOR