๐ ๊ณผํ ๊ต์ฌ Chatbot โ ์ ์ฒด ์ค๋ช (๋ชจ๋)
Source: Dev.to
๐ฑ 0๏ธโฃ ์ฐ๋ฆฌ๊ฐ ๋ง๋ค๊ณ ์๋ ๊ฒ
์ฐ๋ฆฌ๋ ๊ณผํ ๊ต์ฌ AI ์ฑ๋ด ๋ฐฑ์๋๋ฅผ ๋ง๋ค๊ณ ์์ต๋๋ค.
์ด AI:
- ๊ณผํ ์ง๋ฌธ์ ๋ต๋ณํฉ๋๋ค
- ๊ต์ฌ์ฒ๋ผ ์ค๋ช ํฉ๋๋ค
- ์ฑํ ๊ธฐ๋ก์ ๊ธฐ์ตํฉ๋๋ค
- ๊ณผํ์ด ์๋ ์ง๋ฌธ์ ๋ฌด์ํฉ๋๋ค
์์
Student: What is gravity?
AI: Gravity is a forceโฆ
Student: Explain again
AI: As I explained earlierโฆ
๐ AI๋ ๋งฅ๋ฝ์ ๊ธฐ์ตํฉ๋๋ค.
๐ง 1๏ธโฃ ์์คํ ์๋ ๋ฐฉ์
Flow
Student โ API โ Filter โ LangChain โ OpenAI โ Answer
โ
Memory
Steps
- ํ์์ด ์ง๋ฌธ์ ๋ณด๋ ๋๋ค.
- Express API๊ฐ ์ด๋ฅผ ๋ฐ์ต๋๋ค.
- ๊ณผํ ํํฐ๊ฐ ๋ด์ฉ์ ํ์ธํฉ๋๋ค.
- LangChain์ด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ถ๊ฐํฉ๋๋ค.
- OpenAI๊ฐ ๋ต๋ณ์ ์์ฑํฉ๋๋ค.
- API๊ฐ ์๋ต์ ๋ฐํํฉ๋๋ค.
๐งฐ 2๏ธโฃ Technologies
- Node.js โ ๋ฐฑ์๋ ๋ฐํ์
- Express โ API ํ๋ ์์ํฌ
- LangChain โ LLM ์ค์ผ์คํธ๋ ์ด์
- OpenAI โ AI ๋ชจ๋ธ ์ ๊ณต์ ์ฒด
- BufferMemory โ ์ฑํ ๋ฉ๋ชจ๋ฆฌ ์ฒ๋ฆฌ
- dotenv โ ํ๊ฒฝ ๋ณ์
- pnpm โ ํจํค์ง ๋งค๋์
๐ฆ 3๏ธโฃ ํ๋ก์ ํธ ๋ง๋ค๊ธฐ
mkdir science-teacher-bot
cd science-teacher-bot
pnpm init
๐ฆ 4๏ธโฃ ์์กด์ฑ ์ค์น
pnpm add express cors dotenv langchain @langchain/openai nodemon
๐ 5๏ธโฃ ํด๋ ๊ตฌ์กฐ
science-teacher-bot/
โ
โโโ src/
โ โโโ memory.mjs
โ โโโ llm.mjs
โ โโโ filter.mjs
โ โโโ route.mjs
โ โโโ server.mjs
โ
โโโ .env
โโโ package.json
๐ 6๏ธโฃ OpenAI ํค
.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
- ์ฑํ ๊ธฐ๋ก์ ์ ์ฅํฉ๋๋ค
- ์ด์ ๋ฉ์์ง๋ฅผ ๊ธฐ์ตํฉ๋๋ค
- 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 a simple way.
Conversation:
{chat_history}
Student: {input}
Teacher:
`;
export const chatChain = new ConversationChain({
llm,
memory,
prompt: template,
});
๋ฌด์์ ํ๋๊ฐ
- AI ๋ชจ๋ธ์ ์์ฑํฉ๋๋ค
- ๊ต์ฌ ํ๋์ ์ ์ํฉ๋๋ค
- ๋ฉ๋ชจ๋ฆฌ ์ปดํฌ๋ํธ๋ฅผ ์ฐ๊ฒฐํฉ๋๋ค
- ์ฑ๋ด ์ฒด์ธ์ ๊ตฌ์ถํฉ๋๋ค
๐ฌ 9๏ธโฃ filter.mjs
export function isScience(text) {
const keywords = [
"physics", "chemistry", "biology",
"atom", "cell", "energy", "force",
"gravity", "plant", "reaction",
"photosynthesis", "molecule"
];
return keywords.some(word =>
text.toLowerCase().includes(word)
);
}
What it does
- ์ง๋ฌธ์ด ๊ณผํ๊ณผ ๊ด๋ จ๋ ๊ฒ์ธ์ง ํ์ธํฉ๋๋ค
- ๋น๊ณผํ ์ฃผ์ ๋ฅผ ์ฐจ๋จํฉ๋๋ค
๐ 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 });
});
๋ฌด์์ ํ๋๊ฐ
- ์ง๋ฌธ์ ๋ฐ๋๋ค
- ์ ๋ ฅ์ ๊ฒ์ฆํ๋ค
- ๊ณผํ ์ง๋ฌธ์ธ์ง ํ์ธํ๋ค
- AI ์ฒด์ธ์ ํธ์ถํ๋ค
- ๋ต๋ณ์ ๋ฐํํ๋ค
๐ 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);
});
๋ฌด์์ ํ๋๊ฐ
- Express ์๋ฒ๋ฅผ ์์ฑํฉ๋๋ค
- CORS ๋ฐ JSON ํ์ฑ์ ํ์ฑํํฉ๋๋ค
/askAPI ๋ผ์ฐํธ๋ฅผ ๋ง์ดํธํฉ๋๋ค- ๋ฐฑ์๋๋ฅผ ์์ํฉ๋๋ค
โถ๏ธ 1๏ธโฃ2๏ธโฃ ํ๋ก์ ํธ ์คํ
package.json์ ์คํฌ๋ฆฝํธ๋ฅผ ์ถ๊ฐํฉ๋๋ค:
{
"scripts": {
"dev": "node src/server.mjs"
}
}
์๋ฒ๋ฅผ ์์ํฉ๋๋ค:
pnpm dev
๐งช 1๏ธโฃ3๏ธโฃ API ํ ์คํธ
์๋ํฌ์ธํธ: POST http://localhost:3000/ask
์์ฒญ ๋ณธ๋ฌธ (JSON):
{
"text": "What is photosynthesis?"
}
AI์ ๋ต๋ณ์ด ํฌํจ๋ JSON ์๋ต์ ๋ฐ์์ผ ํฉ๋๋ค.
{
"text": "What is photosynthesis?"
}
์๋ต:
{
"answer": "Photosynthesis is the process..."
}
๐ง 1๏ธโฃ4๏ธโฃ ๋ฉ๋ชจ๋ฆฌ ๋ฐ๋ชจ
์ง๋ฌธ:
- ์ค๋ ฅ์ ๋ฌด์์ธ๊ฐ์
- ๋ค์ ์ค๋ช ํด ์ฃผ์ธ์
AI๋ ์ปจํ ์คํธ๋ฅผ ๊ธฐ์ตํฉ๋๋ค.
โ ๏ธ 1๏ธโฃ5๏ธโฃ ์ค์ํ ๋ฉ๋ชจ
ํ์ฌ ๋ฉ๋ชจ๋ฆฌ:
- ๋ชจ๋ ์ฌ์ฉ์์ ๊ณต์
- ์ฌ์์ ์ ์ด๊ธฐํ
์ค์ ์ฑ์์๋:
- DB ๋ฉ๋ชจ๋ฆฌ
- ์ธ์ ID
- ๋ฒกํฐ ์คํ ์ด
๐ 1๏ธโฃ6๏ธโฃ ๋น์ ์ด ๋ง๋ ๊ฒ (ํ์ด๋ก ๋ ๋ฒจ)
- โ LLM ๋ฐฑ์๋
- โ LangChain ํตํฉ
- โ ๋ฉ๋ชจ๋ฆฌ ์ฑ๋ด
- โ ๊ณผํ ํํฐ
- โ Express API
- โ Teacher AI
์ด๊ฒ์ ์ค์ AI ์ฑ ์ํคํ ์ฒ์ ๋๋ค.
๐ 1๏ธโฃ7๏ธโฃ How to Explain to Students
- Express๋ ํ์ ์ง๋ฌธ์ ๋ฐ์ต๋๋ค.
- LangChain์ AI์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐ๊ฒฐํฉ๋๋ค.
- Memory๋ ๋ํ๋ฅผ ์ ์ฅํฉ๋๋ค.
- Filter๋ ๊ณผํ ๊ด๋ จ ๋ต๋ณ๋ง ๋ณด์ฅํฉ๋๋ค.

