Ver3.0 ๐ค ์ฑ๋ด ๊ธฐ๋ณธ ์์ค์ฝ๋ ์์ ์ ๋ณต: AI ๋ํ ์์ง ๋ง๋ค๊ธฐ

๐ค ์ฑ๋ด ๊ธฐ๋ณธ ์์ค์ฝ๋ ์์ ์ ๋ณต: AI ๋ํ ์์ง ๋ง๋ค๊ธฐ
์น๊ตฌ์ฒ๋ผ ๋ํํ๋ ๋๋ํ ์ฑ๋ด, ์ง์ ๋ง๋ค์ด๋ณด์!
๐ฌ ์์ฆ ์น์ฌ์ดํธ๋ ์ฑ์ ๋ค์ด๊ฐ๋ฉด ์ค๋ฅธ์ชฝ ํ๋จ์ "๋ฌด์์ ๋์๋๋ฆด๊น์?"๋ผ๊ณ ๋ง ๊ฑฐ๋ ์์ ์ฐฝ, ํ ๋ฒ์ฏค ๋ดค์ ๊ฑฐ์ผ! ๋ฐ๋ก ์ฑ๋ด์ด์ง. ๊ณ ๊ฐ ๋ฌธ์๋ฅผ ๋ฐ๊ฑฐ๋, ์ํ์ ์ถ์ฒํ๊ฑฐ๋, ์ฌ์ง์ด ๋๋ด๊น์ง ์ฃผ๊ณ ๋ฐ๋ ์ด ๋๋ํ ์น๊ตฌ๋ค์ ์ด๋ป๊ฒ ๋ง๋ค์ด์ง๊น? ๐ค
์ค๋์ ์ฑ๋ด์ ๊ธฐ๋ณธ ์์ค์ฝ๋๋ถํฐ AI ๋ํ ์์ง์ ํต์ฌ ์๋ฆฌ๊น์ง, ์น๊ตฌ์๊ฒ ์ค๋ช
ํ๋ฏ ์ฝ๊ณ ์ฌ๋ฏธ์๊ฒ ํ์ด๋ณผ๊ฒ. ํ๋ก๊ทธ๋๋ฐ์ด ์ฒ์์ด๋ผ๋ ๊ฑฑ์ ๋ง! ์ฐจ๊ทผ์ฐจ๊ทผ ๋ฐ๋ผ์ค๋ฉด ๋๋ ๋๋ง์ ์ฑ๋ด์ ๋ง๋ค ์ ์์ด. ๐
๐ฏ ์ฑ๋ด์ด๋ ๋ฌด์์ธ๊ฐ?
์ฑ๋ด(Chatbot)์ 'Chat(์ฑํ )'๊ณผ 'Bot(๋ก๋ด)'์ ํฉ์ฑ์ด์ผ. ์ฌ๋์ฒ๋ผ ๋ํํ ์ ์๋ ํ๋ก๊ทธ๋จ์ ๋งํ์ง. ๋จ์ํ ์ ํด์ง ๋ต๋ณ๋ง ํ๋ ์ด๋ณด ์ฑ๋ด๋ถํฐ, AI๋ฅผ ํ์ฉํด์ ๋งฅ๋ฝ์ ์ดํดํ๊ณ ์์ฐ์ค๋ฝ๊ฒ ๋ํํ๋ ๊ณ ๊ธ ์ฑ๋ด๊น์ง ๋ค์ํด. ๐ญ
๐ ์ฑ๋ด์ 3๊ฐ์ง ๋ ๋ฒจ
Level 1: ๊ท์น ๊ธฐ๋ฐ ์ฑ๋ด
๋ฏธ๋ฆฌ ์ ํด์ง ์๋๋ฆฌ์ค๋๋ก๋ง ์์ง์ฌ. "์๋
"์ด๋ผ๊ณ ํ๋ฉด "์๋
ํ์ธ์!"๋ผ๊ณ ๋ตํ๋ ์์ด์ง. ๊ฐ๋จํ์ง๋ง ์ตํต์ฑ์ด ์์ด.
Level 2: ํค์๋ ์ธ์ ์ฑ๋ด
์ฌ์ฉ์ ์
๋ ฅ์์ ํต์ฌ ํค์๋๋ฅผ ์ฐพ์๋ด์ ๋ฐ์ํด. "๋ฐฐ์ก"์ด๋ผ๋ ๋จ์ด๋ฅผ ๋ฐ๊ฒฌํ๋ฉด ๋ฐฐ์ก ๊ด๋ จ ๋ต๋ณ์ ํ๋ ๊ฑฐ์ผ. ์กฐ๊ธ ๋ ๋๋ํ์ง?
Level 3: AI ๊ธฐ๋ฐ ์ฑ๋ด
์์ฐ์ด ์ฒ๋ฆฌ(NLP)์ ๋จธ์ ๋ฌ๋์ ํ์ฉํด์ ๋ฌธ๋งฅ์ ์ดํดํ๊ณ ํ์ตํด. ๋ํํ ์๋ก ๋๋ํด์ง๋ ์ง์ง AI ์น๊ตฌ! ๐ง
๐ก ์ฌ๋ฅ๋ท TIP ์ฑ๋ด ๊ฐ๋ฐ์ด ์ฒ์์ด๋ผ๋ฉด ์ฌ๋ฅ๋ท์์ ์ฑ๋ด ๊ฐ๋ฐ ์ ๋ฌธ๊ฐ์ ๋์์ ๋ฐ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ. ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ฅผ ๋ฐฐ์ฐ๊ณ ๋์ ์ง์ ์ปค์คํฐ๋ง์ด์งํ๋ฉด ํจ์ฌ ๋น ๋ฅด๊ฒ ์ฑ์ฅํ ์ ์์ด!
๐ ๏ธ ์ฑ๋ด ๊ฐ๋ฐ ํ๊ฒฝ ์ค๋นํ๊ธฐ
๋ณธ๊ฒฉ์ ์ผ๋ก ์ฝ๋ฉ์ ๋ค์ด๊ฐ๊ธฐ ์ ์, ํ์ํ ๋๊ตฌ๋ค์ ๋จผ์ ์ฑ๊ฒจ๋ณด์! ์๋ฆฌํ๊ธฐ ์ ์ ์ฌ๋ฃ ์ค๋นํ๋ ๊ฒ์ฒ๋ผ ๋ง์ด์ผ. ๐จโ๐ณ
1๋จ๊ณ: ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ์ ํ ๐จ
์ฑ๋ด์ ๋ค์ํ ์ธ์ด๋ก ๋ง๋ค ์ ์์ด. ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ๊ฑด:
โข Python: ์ด๋ณด์ ์นํ์ ์ด๊ณ AI ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํ๋ถํด
โข JavaScript: ์น ๊ธฐ๋ฐ ์ฑ๋ด์ ์ต์ ํ
โข Java: ๋๊ท๋ชจ ์ํฐํ๋ผ์ด์ฆ ํ๊ฒฝ์ ์ ํฉ
โข Node.js: ์ค์๊ฐ ํต์ ์ ๊ฐ๋ ฅํจ
2๋จ๊ณ: ๊ฐ๋ฐ ๋๊ตฌ ์ค์น ๐ป
โข ์ฝ๋ ์๋ํฐ: VS Code, PyCharm, Sublime Text ๋ฑ
โข ํจํค์ง ๊ด๋ฆฌ์: pip(Python), npm(JavaScript)
โข ๋ฒ์ ๊ด๋ฆฌ: Git (๋์ค์ ํ์
ํ ๋ ํ์!)
3๋จ๊ณ: ํ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น ๐ฆ
Python ๊ธฐ์ค์ผ๋ก ์ค๋ช ํ ๊ฒ:
# ๊ธฐ๋ณธ ์น ํ๋ ์์ํฌ
pip install flask
# ์์ฐ์ด ์ฒ๋ฆฌ
pip install nltk
pip install spacy
# ๋จธ์ ๋ฌ๋
pip install tensorflow
pip install scikit-learn
# ์ฑ๋ด ํ๋ ์์ํฌ
pip install chatterbot
pip install rasa
โ ๏ธ ์ฃผ์์ฌํญ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฒ์ ํธํ์ฑ์ ๊ผญ ํ์ธํด! ํนํ TensorFlow์ Python ๋ฒ์ ๊ฐ์ ๊ถํฉ์ด ์ค์ํด. ๊ณต์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๋ ์ต๊ด์ ๋ค์ด์.
๐ ์ฒซ ๋ฒ์งธ ์ฑ๋ด ๋ง๋ค๊ธฐ: ๊ท์น ๊ธฐ๋ฐ ๋ฒ์
์, ์ด์ ์ง์ง ์ฝ๋ฉ์ ์์ํด๋ณผ๊น? ๊ฐ์ฅ ๊ฐ๋จํ ๊ท์น ๊ธฐ๋ฐ ์ฑ๋ด๋ถํฐ ๋ง๋ค์ด๋ณด์! ๐
๐ Python์ผ๋ก ๋ง๋๋ ์ด๊ฐ๋จ ์ฑ๋ด
# simple_chatbot.py
# ๊ธฐ๋ณธ ์ฑ๋ด ํด๋์ค ์ ์
class SimpleChatbot:
def __init__(self):
# ์๋ต ๊ท์น ๋์
๋๋ฆฌ
self.responses = {
"์๋
": "์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์? ๐",
"์ด๋ฆ": "์ ๋ ํ์ด๋ด์ด์์! ๋น์ ์ ์ด๋ฆ์ ๋ฌด์์ธ๊ฐ์?",
"๋ ์จ": "์ฃ์กํด์, ์์ง ๋ ์จ ์ ๋ณด๋ ์ ๊ณตํ์ง ๋ชปํด์. ๐ค๏ธ",
"๊ณ ๋ง์": "์ฒ๋ง์์! ๋ ๊ถ๊ธํ ๊ฒ ์์ผ๋ฉด ๋ฌผ์ด๋ณด์ธ์!",
"์๋
ํ": "์ข์ ํ๋ฃจ ๋์ธ์! ๋ค์์ ๋ ๋ง๋์! ๐"
}
def get_response(self, user_input):
# ์ฌ์ฉ์ ์
๋ ฅ์ ์๋ฌธ์๋ก ๋ณํ
user_input = user_input.lower().strip()
# ํค์๋ ๋งค์นญ
for keyword, response in self.responses.items():
if keyword in user_input:
return response
# ๋งค์นญ๋๋ ๊ฒ ์์ผ๋ฉด ๊ธฐ๋ณธ ์๋ต
return "์... ์ ์ดํดํ์ง ๋ชปํ์ด์. ๋ค์ ๋ง์ํด์ฃผ์๊ฒ ์ด์? ๐ค"
def start(self):
print("์ฑ๋ด: ์๋
ํ์ธ์! ๋ํ๋ฅผ ์์ํด๋ณผ๊น์? (์ข
๋ฃํ๋ ค๋ฉด '์ข
๋ฃ' ์
๋ ฅ)")
print("-" * 50)
while True:
user_input = input("๋น์ : ")
if user_input.lower() in ['์ข
๋ฃ', 'quit', 'exit']:
print("์ฑ๋ด: ์๋
ํ ๊ฐ์ธ์! ๐")
break
response = self.get_response(user_input)
print(f"์ฑ๋ด: {response}")
print()
# ์ฑ๋ด ์คํ
if __name__ == "__main__":
bot = SimpleChatbot()
bot.start()
๐ก ์ฝ๋ ์ค๋ช ์ด ์ฝ๋๋ ๋์ ๋๋ฆฌ๋ฅผ ์ฌ์ฉํด์ ํค์๋์ ์๋ต์ ๋งค์นญํด. ์ฌ์ฉ์๊ฐ "์๋ "์ด๋ผ๊ณ ์ ๋ ฅํ๋ฉด, ๋์ ๋๋ฆฌ์์ "์๋ " ํค๋ฅผ ์ฐพ์์ ํด๋น ์๋ต์ ๋ฐํํ๋ ๊ฑฐ์ผ. ๊ฐ๋จํ์ง๋ง ์ฑ๋ด์ ๊ธฐ๋ณธ ์๋ฆฌ๋ฅผ ์ดํดํ๊ธฐ์ ์๋ฒฝํด! ๐ฏ
๐ ์น ๊ธฐ๋ฐ ์ฑ๋ด์ผ๋ก ์ ๊ทธ๋ ์ด๋
์ฝ์์์๋ง ์๋ํ๋ ๊ฑด ์ข ์ฌ์ฌํ์ง? Flask๋ฅผ ์ฌ์ฉํด์ ์น ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค์ด๋ณด์!
# web_chatbot.py
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
class WebChatbot:
def __init__(self):
self.responses = {
"์๋
": "์๋
ํ์ธ์! ์น ์ฑ๋ด์
๋๋ค! ๐",
"๊ธฐ๋ฅ": "์ ๋ ๊ฐ๋จํ ์ง๋ฌธ์ ๋ต๋ณํ ์ ์์ด์!",
"์๊ฐ": "์ฃ์กํด์, ์๊ฐ ๊ธฐ๋ฅ์ ์์ง ๊ฐ๋ฐ ์ค์ด์์. โฐ",
"๋์": "๋ฌด์์ด๋ ๋ฌผ์ด๋ณด์ธ์! ์ต์ ์ ๋คํด ๋ต๋ณํ ๊ฒ์.",
}
def get_response(self, message):
message = message.lower().strip()
for keyword, response in self.responses.items():
if keyword in message:
return response
return "ํฅ๋ฏธ๋ก์ด ์ง๋ฌธ์ด๋ค์! ๋ ๊ณต๋ถํด์ ๋ต๋ณ๋๋ฆด๊ฒ์. ๐"
chatbot = WebChatbot()
@app.route('/')
def home():
return '''
<!DOCTYPE html>
<html>
<head>
<title>์น ์ฑ๋ด</title>
<style>.tx-header{background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);
color:#fff;
padding:40px 30px;
border-radius:16px;
text-align:center;
margin-bottom:40px;
box-shadow:0 8px 24px rgba(102,126,234,0.25);}
.tx-intro{background:#f8f9fa;
padding:25px;
border-radius:12px;
margin:30px 0;
border-left:5px solid #667eea;}
.tx-section{margin:40px 0;}
.tx-highlight{background:linear-gradient(120deg,#ffeaa7 0%,#fdcb6e 100%);
padding:3px 8px;
border-radius:4px;
font-weight:600;}
.tx-tip{background:#e8f5e9;
padding:20px;
border-radius:10px;
margin:25px 0;
border-left:4px solid #4caf50;}
.tx-warning{background:#fff3e0;
padding:20px;
border-radius:10px;
margin:25px 0;
border-left:4px solid #ff9800;}
.tx-point{color:#667eea;
font-weight:700;}
.tx-subtitle{color:#764ba2;
font-weight:600;
font-size:1.1em;
margin:20px 0 10px 0;}
.tx-card{background:#ffffff;
padding:20px;
border-radius:12px;
margin:20px 0;
box-shadow:0 4px 12px rgba(0,0,0,0.08);}
.tx-step{background:#f1f3f5;
padding:15px 20px;
border-radius:8px;
margin:15px 0;
border-left:3px solid #667eea;}</style>
</head>
<body>
<h1 style="text-align:center; color:#667eea;">๐ค ์น ์ฑ๋ด</h1>
<div id="chat-box"></div>
<div id="input-area">
<input type="text" id="user-input" placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์...">
<button id="send-btn" onclick="sendMessage()">์ ์ก</button>
</div>
<script>
function sendMessage() {
const input = document.getElementById('user-input');
const message = input.value.trim();
if (!message) return;
// ์ฌ์ฉ์ ๋ฉ์์ง ํ์
addMessage(message, 'user');
input.value = '';
// ์๋ฒ์ ๋ฉ์์ง ์ ์ก
fetch('/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: message })
})
.then(response => response.json())
.then(data => {
addMessage(data.response, 'bot');
});
}
function addMessage(text, sender) {
const chatBox = document.getElementById('chat-box');
const messageDiv = document.createElement('div');
messageDiv.className = 'message ' + sender;
messageDiv.textContent = text;
chatBox.appendChild(messageDiv);
chatBox.scrollTop = chatBox.scrollHeight;
}
// ์ํฐํค๋ก ์ ์ก
document.getElementById('user-input').addEventListener('keypress', function(e) {
if (e.key === 'Enter') sendMessage();
});
</script>
</body>
</html>
'''
@app.route('/chat', methods=['POST'])
def chat():
user_message = request.json['message']
bot_response = chatbot.get_response(user_message)
return jsonify({'response': bot_response})
if __name__ == '__main__':
app.run(debug=True, port=5000)
์ด์ ๋ธ๋ผ์ฐ์ ์์ http://localhost:5000์ผ๋ก ์ ์ํ๋ฉด ๋ฉ์ง ์น ์ฑ๋ด์ ๋ง๋ ์ ์์ด! ๐
๐ง AI ๋ํ ์์ง์ ํต์ฌ: ์์ฐ์ด ์ฒ๋ฆฌ(NLP)
๊ท์น ๊ธฐ๋ฐ ์ฑ๋ด์ ํ๊ณ๊ฐ ๋ช ํํด. "์๋ ํ์ธ์"๋ ์ดํดํ์ง๋ง "ํ์ด~"๋ ๋ชป ์์๋ฃ๋ ์์ด์ง. ์ง์ง ๋๋ํ ์ฑ๋ด์ ๋ง๋ค๋ ค๋ฉด ์์ฐ์ด ์ฒ๋ฆฌ ๊ธฐ์ ์ด ํ์ํด! ๐
๐ NLP์ ์ฃผ์ ๊ธฐ์ ๋ค
1. ํ ํฐํ(Tokenization) โ๏ธ
๋ฌธ์ฅ์ ๋จ์ด๋ ํํ์ ๋จ์๋ก ์ชผ๊ฐ๋ ๊ฑฐ์ผ. "๋๋ ๋ฐฅ์ ๋จน์๋ค" โ ["๋๋", "๋ฐฅ์", "๋จน์๋ค"]
2. ํํ์ ๋ถ์(Morphological Analysis) ๐ฌ
๋จ์ด์ ๊ธฐ๋ณธํ์ ์ฐพ์๋ด. "๋จน์๋ค" โ "๋จน๋ค" (๋์ฌ ์ํ)
3. ๊ฐ์ฒด๋ช
์ธ์(NER: Named Entity Recognition) ๐ท๏ธ
๋ฌธ์ฅ์์ ์ฌ๋ ์ด๋ฆ, ์ฅ์, ๋ ์ง ๋ฑ์ ์ฐพ์๋ด. "๋ด์ผ ์์ธ์์ ๋ง๋์" โ ์๊ฐ(๋ด์ผ), ์ฅ์(์์ธ)
4. ๊ฐ์ ๋ถ์(Sentiment Analysis) ๐๐ข
๋ฌธ์ฅ์ ๊ธ์ /๋ถ์ /์ค๋ฆฝ์ ํ๋จํด. ๊ณ ๊ฐ ๋ง์กฑ๋ ์กฐ์ฌ์ ์ ์ฉํ์ง!
5. ์๋ ๋ถ๋ฅ(Intent Classification) ๐ฏ
์ฌ์ฉ์๊ฐ ๋ญ ์ํ๋์ง ํ์
ํด. "ํผ์ ์ฃผ๋ฌธํ๊ณ ์ถ์ด" โ ์๋: ์ฃผ๋ฌธ
๐ NLTK๋ฅผ ํ์ฉํ ๊ธฐ๋ณธ NLP
# nlp_chatbot.py
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
import string
# ํ์ํ ๋ฐ์ดํฐ ๋ค์ด๋ก๋ (์ต์ด 1ํ)
# nltk.download('punkt')
# nltk.download('stopwords')
class NLPChatbot:
def __init__(self):
self.stemmer = PorterStemmer()
self.stop_words = set(stopwords.words('english'))
# ์๋๋ณ ํค์๋์ ์๋ต
self.intents = {
'greeting': {
'keywords': ['hello', 'hi', 'hey', 'greetings'],
'responses': [
"Hello! How can I help you today? ๐",
"Hi there! What can I do for you?",
"Hey! Nice to meet you!"
]
},
'goodbye': {
'keywords': ['bye', 'goodbye', 'see you', 'farewell'],
'responses': [
"Goodbye! Have a great day! ๐",
"See you later!",
"Take care!"
]
},
'thanks': {
'keywords': ['thank', 'thanks', 'appreciate'],
'responses': [
"You're welcome! ๐",
"Happy to help!",
"Anytime!"
]
},
'help': {
'keywords': ['help', 'assist', 'support'],
'responses': [
"I'm here to help! What do you need?",
"Sure! Tell me what you need assistance with.",
"I'd be happy to help you!"
]
}
}
def preprocess(self, text):
"""ํ
์คํธ ์ ์ฒ๋ฆฌ"""
# ์๋ฌธ์ ๋ณํ
text = text.lower()
# ํ ํฐํ
tokens = word_tokenize(text)
# ๊ตฌ๋์ ์ ๊ฑฐ
tokens = [word for word in tokens if word not in string.punctuation]
# ๋ถ์ฉ์ด ์ ๊ฑฐ
tokens = [word for word in tokens if word not in self.stop_words]
# ์ด๊ฐ ์ถ์ถ
tokens = [self.stemmer.stem(word) for word in tokens]
return tokens
def classify_intent(self, tokens):
"""์๋ ๋ถ๋ฅ"""
for intent, data in self.intents.items():
keywords = [self.stemmer.stem(kw) for kw in data['keywords']]
for token in tokens:
if token in keywords:
return intent
return None
def get_response(self, user_input):
"""์๋ต ์์ฑ"""
import random
# ์ ์ฒ๋ฆฌ
tokens = self.preprocess(user_input)
# ์๋ ๋ถ๋ฅ
intent = self.classify_intent(tokens)
# ์๋ต ์ ํ
if intent:
responses = self.intents[intent]['responses']
return random.choice(responses)
else:
return "I'm not sure I understand. Could you rephrase that? ๐ค"
# ์ฌ์ฉ ์์
if __name__ == "__main__":
bot = NLPChatbot()
print("NLP Chatbot: Hello! I'm an NLP-powered chatbot. (Type 'quit' to exit)")
print("-" * 60)
while True:
user_input = input("You: ")
if user_input.lower() in ['quit', 'exit']:
print("Bot: Goodbye! ๐")
break
response = bot.get_response(user_input)
print(f"Bot: {response}\n")
๐ก ํ๊ตญ์ด NLP๋? ํ๊ตญ์ด๋ ์์ด๋ณด๋ค ๋ณต์กํด! ์กฐ์ฌ, ์ด๋ฏธ ๋ณํ๊ฐ ๋ง๊ฑฐ๋ . ํ๊ตญ์ด NLP์๋ KoNLPy, Mecab, Okt(Twitter) ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด. ํํ์ ๋ถ์์ด ํต์ฌ์ด์ผ!
๐ค ๋จธ์ ๋ฌ๋ ๊ธฐ๋ฐ ์ฑ๋ด ๋ง๋ค๊ธฐ
์ด์ ์ง์ง AI์ ์์ญ์ผ๋ก ๋ค์ด๊ฐ๋ณด์! ๋จธ์ ๋ฌ๋์ ์ฌ์ฉํ๋ฉด ์ฑ๋ด์ด ๋ฐ์ดํฐ๋ก๋ถํฐ ํ์ตํด์ ์ ์ ๋๋ํด์ ธ. ๐
๐ ์๋ ๋ถ๋ฅ ๋ชจ๋ธ ํ์ตํ๊ธฐ
# ml_chatbot.py
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
import pickle
class MLChatbot:
def __init__(self):
# ํ์ต ๋ฐ์ดํฐ
self.training_data = [
# ์ธ์ฌ
("์๋
ํ์ธ์", "greeting"),
("์๋
", "greeting"),
("ํ์ด", "greeting"),
("๋ฐ๊ฐ์ต๋๋ค", "greeting"),
("์ข์ ์์นจ", "greeting"),
# ์๋ณ
("์๋
ํ ๊ฐ์ธ์", "goodbye"),
("์๊ฐ", "goodbye"),
("๋ค์์ ๋ด", "goodbye"),
("๋ ๋ง๋์", "goodbye"),
# ๊ฐ์ฌ
("๊ณ ๋ง์", "thanks"),
("๊ฐ์ฌํฉ๋๋ค", "thanks"),
("๊ณ ๋ง์ต๋๋ค", "thanks"),
("๋กํ", "thanks"),
# ๋์ ์์ฒญ
("๋์์ค", "help"),
("๋์์ด ํ์ํด", "help"),
("์ด๋ป๊ฒ ํด", "help"),
("๋ชจ๋ฅด๊ฒ ์ด", "help"),
# ์ฃผ๋ฌธ
("์ฃผ๋ฌธํ๊ณ ์ถ์ด", "order"),
("๊ตฌ๋งคํ ๊ฒ", "order"),
("์ฌ๊ณ ์ถ์ด", "order"),
("์ฅ๋ฐ๊ตฌ๋", "order"),
# ๋ฐฐ์ก
("๋ฐฐ์ก ์ธ์ ์", "delivery"),
("๋ฐฐ์ก ์กฐํ", "delivery"),
("์ธ์ ๋์ฐฉํด", "delivery"),
("๋ฐฐ์ก ์ํ", "delivery"),
]
# ์๋ต ํ
ํ๋ฆฟ
self.responses = {
"greeting": [
"์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์? ๐",
"๋ฐ๊ฐ์ต๋๋ค! ๊ถ๊ธํ ์ ์ด ์์ผ์ ๊ฐ์?",
"์๋
ํ์ธ์! ์ค๋ ํ๋ฃจ ์ด๋ ์ธ์?"
],
"goodbye": [
"์๋
ํ ๊ฐ์ธ์! ์ข์ ํ๋ฃจ ๋์ธ์! ๐",
"๋ ๋ง๋์! ์ธ์ ๋ ์ฐพ์์ฃผ์ธ์!",
"๋ค์์ ๋ ๋ต๊ฒ ์ต๋๋ค!"
],
"thanks": [
"์ฒ๋ง์์! ๋ ๋์๋๋ฆด ๊ฒ ์๋์? ๐",
"๋ณ๋ง์์์! ์ธ์ ๋ ๋ฌผ์ด๋ณด์ธ์!",
"๋์์ด ๋์๋ค๋ ๊ธฐ์ฉ๋๋ค!"
],
"help": [
"๋ฌด์์ ๋์๋๋ฆด๊น์? ์์ธํ ๋ง์ํด์ฃผ์ธ์!",
"์ ๊ฐ ๋์๋๋ฆฌ๊ฒ ์ต๋๋ค! ์ด๋ค ๋ถ๋ถ์ด ๊ถ๊ธํ์ ๊ฐ์?",
"๋ฌธ์ ๋ฅผ ํจ๊ป ํด๊ฒฐํด๋ด์! ๐ค"
],
"order": [
"์ฃผ๋ฌธํ์๋ ค๋ ์ํ์ ์๋ ค์ฃผ์ธ์!",
"์ด๋ค ์ ํ์ ๊ตฌ๋งคํ๊ณ ์ถ์ผ์ ๊ฐ์?",
"์ฅ๋ฐ๊ตฌ๋์ ๋ด์ผ์ค ์ํ์ ์ ํํด์ฃผ์ธ์! ๐"
],
"delivery": [
"์ฃผ๋ฌธ๋ฒํธ๋ฅผ ์๋ ค์ฃผ์๋ฉด ๋ฐฐ์ก ์ํ๋ฅผ ํ์ธํด๋๋ฆด๊ฒ์!",
"๋ฐฐ์ก ์กฐํ๋ ๋ง์ดํ์ด์ง์์๋ ๊ฐ๋ฅํฉ๋๋ค! ๐ฆ",
"๋ฐฐ์ก ๊ด๋ จ ๋ฌธ์๋ ๊ณ ๊ฐ์ผํฐ๋ก ์ฐ๊ฒฐํด๋๋ฆด๊น์?"
]
}
self.model = None
self.train()
def train(self):
"""๋ชจ๋ธ ํ์ต"""
# ๋ฐ์ดํฐ ๋ถ๋ฆฌ
X = [text for text, label in self.training_data]
y = [label for text, label in self.training_data]
# ํ์ดํ๋ผ์ธ ์์ฑ (TF-IDF + Naive Bayes)
self.model = Pipeline([
('tfidf', TfidfVectorizer(
analyzer='char', # ํ๊ตญ์ด๋ ๋ฌธ์ ๋จ์๊ฐ ํจ๊ณผ์
ngram_range=(2, 4) # 2~4๊ธ์ ์กฐํฉ
)),
('clf', MultinomialNB())
])
# ํ์ต
self.model.fit(X, y)
print("โ
๋ชจ๋ธ ํ์ต ์๋ฃ!")
def predict_intent(self, text):
"""์๋ ์์ธก"""
if self.model is None:
return None
intent = self.model.predict([text])[0]
probability = self.model.predict_proba([text]).max()
# ์ ๋ขฐ๋๊ฐ ๋ฎ์ผ๋ฉด None ๋ฐํ
if probability < 0.5:
return None
return intent
def get_response(self, user_input):
"""์๋ต ์์ฑ"""
import random
intent = self.predict_intent(user_input)
if intent and intent in self.responses:
return random.choice(self.responses[intent])
else:
return "์ฃ์กํด์, ์ ์ดํดํ์ง ๋ชปํ์ด์. ๋ค์ ๋ง์ํด์ฃผ์๊ฒ ์ด์? ๐ค"
def save_model(self, filename='chatbot_model.pkl'):
"""๋ชจ๋ธ ์ ์ฅ"""
with open(filename, 'wb') as f:
pickle.dump(self.model, f)
print(f"โ
๋ชจ๋ธ์ด {filename}์ ์ ์ฅ๋์์ต๋๋ค!")
def load_model(self, filename='chatbot_model.pkl'):
"""๋ชจ๋ธ ๋ถ๋ฌ์ค๊ธฐ"""
with open(filename, 'rb') as f:
self.model = pickle.load(f)
print(f"โ
๋ชจ๋ธ์ {filename}์์ ๋ถ๋ฌ์์ต๋๋ค!")
# ์คํ ์์
if __name__ == "__main__":
bot = MLChatbot()
print("ML Chatbot: ์๋
ํ์ธ์! ๋จธ์ ๋ฌ๋ ์ฑ๋ด์
๋๋ค. (์ข
๋ฃ: 'quit')")
print("-" * 60)
while True:
user_input = input("๋น์ : ")
if user_input.lower() in ['quit', 'exit', '์ข
๋ฃ']:
print("Bot: ์๋
ํ ๊ฐ์ธ์! ๐")
break
response = bot.get_response(user_input)
print(f"Bot: {response}\n")
# ๋ชจ๋ธ ์ ์ฅ
bot.save_model()
๐ฏ ์ฝ๋ ํต์ฌ ํฌ์ธํธ
TF-IDF (Term Frequency-Inverse Document Frequency)
๋จ์ด์ ์ค์๋๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ด์ผ. ์์ฃผ ๋์ค์ง๋ง ๋ชจ๋ ๋ฌธ์์ ๋์ค๋ ๋จ์ด(์: "์", "๋")๋ ์ค์๋๊ฐ ๋ฎ๊ณ , ํน์ ๋ฌธ์์๋ง ๋์ค๋ ๋จ์ด๋ ์ค์๋๊ฐ ๋์. ๐
Naive Bayes ๋ถ๋ฅ๊ธฐ
ํ๋ฅ ๊ธฐ๋ฐ ๋ถ๋ฅ ์๊ณ ๋ฆฌ์ฆ์ด์ผ. "์ด ๋ฌธ์ฅ์ด ์ธ์ฌ์ผ ํ๋ฅ ์ 80%, ์๋ณ ์ธ์ฌ์ผ ํ๋ฅ ์ 20%"์ฒ๋ผ ๊ณ์ฐํด์ ๊ฐ์ฅ ํ๋ฅ ๋์ ๊ฑธ ์ ํํ์ง. ๊ฐ๋จํ์ง๋ง ํจ๊ณผ์ ์ด์ผ! ๐ฒ
Character N-gram
ํ๊ตญ์ด๋ ๋์ด์ฐ๊ธฐ๊ฐ ๋ถ๊ท์นํ๊ณ ์กฐ์ฌ ๋ณํ๊ฐ ๋ง์์, ๋ฌธ์ ๋จ์๋ก ๋ถ์ํ๋ ๊ฒ ํจ๊ณผ์ ์ด์ผ. "์๋
ํ์ธ์" โ ["์๋
", "๋
ํ", "ํ์ธ", "์ธ์"] ์ด๋ฐ ์์ผ๋ก!
๐ ๋ฅ๋ฌ๋ ๊ธฐ๋ฐ ๊ณ ๊ธ ์ฑ๋ด
์ด์ ์ต์ ๊ธฐ์ ์ ์ ์ , ๋ฅ๋ฌ๋ ์ฑ๋ด์ ๋ง๋ค์ด๋ณด์! ํธ๋์คํฌ๋จธ, BERT, GPT ๊ฐ์ ์ฉ์ด๋ค ๋ค์ด๋ดค์ง? ์ด๊ฒ ๋ฐ๋ก ๊ทธ๊ฑฐ์ผ! ๐ฅ
๐งฌ Seq2Seq ๋ชจ๋ธ์ ์ดํด
Sequence-to-Sequence ๋ชจ๋ธ์ ์ ๋ ฅ ์ํ์ค๋ฅผ ๋ฐ์์ ์ถ๋ ฅ ์ํ์ค๋ฅผ ์์ฑํด. ๋ฒ์ญ๊ธฐ๋ ์ฑ๋ด์ ๋ฑ์ด์ง! ๊ตฌ์กฐ๋ ์ด๋:
์ธ์ฝ๋(Encoder) ๐ฅ ์ฌ์ฉ์ ์ ๋ ฅ์ ๋ฐ์์ ์๋ฏธ๋ฅผ ์์ถ๋ ๋ฒกํฐ๋ก ๋ณํํด. "์๋ ํ์ธ์" โ [0.2, 0.8, 0.1, ...]
๋์ฝ๋(Decoder) ๐ค
์์ถ๋ ๋ฒกํฐ๋ฅผ ๋ฐ์์ ์๋ต์ ๋จ์ด ํ๋์ฉ ์์ฑํด. [0.2, 0.8, 0.1, ...] โ "์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์?"
์ดํ
์
(Attention) ๐
์
๋ ฅ์ ์ด๋ ๋ถ๋ถ์ ์ง์คํด์ผ ํ๋์ง ์๋ ค์ค. ๊ธด ๋ฌธ์ฅ๋ ์ ํํ๊ฒ ์ดํดํ ์ ์๊ฒ ํด์ฃผ๋ ํต์ฌ ๊ธฐ์ !
๐ป TensorFlow๋ก ๊ฐ๋จํ Seq2Seq ์ฑ๋ด
# seq2seq_chatbot.py
import tensorflow as tf
from tensorflow.keras.layers import Input, LSTM, Dense, Embedding
from tensorflow.keras.models import Model
import numpy as np
class Seq2SeqChatbot:
def __init__(self, vocab_size=5000, embedding_dim=256, hidden_units=512):
self.vocab_size = vocab_size
self.embedding_dim = embedding_dim
self.hidden_units = hidden_units
# ๋จ์ด-์ธ๋ฑ์ค ๋งคํ (์ค์ ๋ก๋ ํ์ต ๋ฐ์ดํฐ์์ ์์ฑ)
self.word2idx = {}
self.idx2word = {}
self.encoder_model = None
self.decoder_model = None
def build_model(self):
"""Seq2Seq ๋ชจ๋ธ ๊ตฌ์ถ"""
# ์ธ์ฝ๋
encoder_inputs = Input(shape=(None,))
encoder_embedding = Embedding(self.vocab_size, self.embedding_dim)(encoder_inputs)
encoder_lstm = LSTM(self.hidden_units, return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(encoder_embedding)
encoder_states = [state_h, state_c]
# ๋์ฝ๋
decoder_inputs = Input(shape=(None,))
decoder_embedding = Embedding(self.vocab_size, self.embedding_dim)(decoder_inputs)
decoder_lstm = LSTM(self.hidden_units, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=encoder_states)
decoder_dense = Dense(self.vocab_size, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
# ์ ์ฒด ๋ชจ๋ธ
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
print("โ
Seq2Seq ๋ชจ๋ธ ๊ตฌ์ถ ์๋ฃ!")
return model
def build_inference_models(self, training_model):
"""์ถ๋ก ์ฉ ์ธ์ฝ๋/๋์ฝ๋ ๋ชจ๋ธ ๋ถ๋ฆฌ"""
# ์ธ์ฝ๋ ๋ชจ๋ธ
encoder_inputs = training_model.input[0]
encoder_outputs, state_h, state_c = training_model.layers[2].output
self.encoder_model = Model(encoder_inputs, [state_h, state_c])
# ๋์ฝ๋ ๋ชจ๋ธ
decoder_inputs = training_model.input[1]
decoder_state_input_h = Input(shape=(self.hidden_units,))
decoder_state_input_c = Input(shape=(self.hidden_units,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_embedding = training_model.layers[3]
decoder_lstm = training_model.layers[4]
decoder_dense = training_model.layers[5]
decoder_embedded = decoder_embedding(decoder_inputs)
decoder_outputs, state_h, state_c = decoder_lstm(
decoder_embedded, initial_state=decoder_states_inputs
)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
self.decoder_model = Model(
[decoder_inputs] + decoder_states_inputs,
[decoder_outputs] + decoder_states
)
print("โ
์ถ๋ก ๋ชจ๋ธ ๊ตฌ์ถ ์๋ฃ!")
def decode_sequence(self, input_seq):
"""์
๋ ฅ ์ํ์ค๋ฅผ ์๋ต์ผ๋ก ๋์ฝ๋ฉ"""
# ์ธ์ฝ๋๋ก ์ํ ๋ฒกํฐ ์ป๊ธฐ
states_value = self.encoder_model.predict(input_seq)
# ์์ ํ ํฐ์ผ๋ก ํ๊ฒ ์ํ์ค ์ด๊ธฐํ
target_seq = np.zeros((1, 1))
target_seq[0, 0] = self.word2idx.get('<start>', 1)
# ์๋ต ์์ฑ
decoded_sentence = []
max_length = 50
for _ in range(max_length):
output_tokens, h, c = self.decoder_model.predict([target_seq] + states_value)
# ๊ฐ์ฅ ํ๋ฅ ๋์ ๋จ์ด ์ ํ
sampled_token_index = np.argmax(output_tokens[0, -1, :])
sampled_word = self.idx2word.get(sampled_token_index, '<unk>')
# ์ข
๋ฃ ํ ํฐ์ด๋ฉด ์ค๋จ
if sampled_word == '<end>':
break
decoded_sentence.append(sampled_word)
# ๋ค์ ์
๋ ฅ ์ค๋น
target_seq = np.zeros((1, 1))
target_seq[0, 0] = sampled_token_index
states_value = [h, c]
return ' '.join(decoded_sentence)
def prepare_data(self, conversations):
"""๋ํ ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ"""
# ์ค์ ๊ตฌํ์์๋ ํ ํฐํ, ํจ๋ฉ, ์ธ๋ฑ์ฑ ๋ฑ ์ํ
# ์ฌ๊ธฐ์๋ ๊ฐ๋
์ ์ค๋ช
์ ์ํ ๊ฐ์ํ ๋ฒ์
input_texts = []
target_texts = []
for question, answer in conversations:
input_texts.append(question)
target_texts.append('<start> ' + answer + ' <end>')
# ์ดํ ์ฌ์ ๊ตฌ์ถ
all_words = set()
for text in input_texts + target_texts:
all_words.update(text.split())
self.word2idx = {word: idx+2 for idx, word in enumerate(sorted(all_words))}
self.word2idx['<pad>'] = 0
self.word2idx['<unk>'] = 1
self.idx2word = {idx: word for word, idx in self.word2idx.items()}
print(f"โ
์ดํ ํฌ๊ธฐ: {len(self.word2idx)}")
return input_texts, target_texts
# ์ฌ์ฉ ์์
if __name__ == "__main__":
# ํ์ต ๋ฐ์ดํฐ ์์
conversations = [
("์๋
ํ์ธ์", "์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์?"),
("๋ ์จ ์ด๋", "์ค๋ ๋ ์จ๋ ๋ง์ต๋๋ค!"),
("๊ณ ๋ง์", "์ฒ๋ง์์! ๋ ๊ถ๊ธํ ๊ฒ ์์ผ๋ฉด ๋ฌผ์ด๋ณด์ธ์!"),
# ... ์ค์ ๋ก๋ ์์ฒ~์๋ง ๊ฐ์ ๋ํ ์ ํ์
]
chatbot = Seq2SeqChatbot()
input_texts, target_texts = chatbot.prepare_data(conversations)
# ๋ชจ๋ธ ๊ตฌ์ถ
training_model = chatbot.build_model()
print("\n๐ก ์ค์ ํ์ต์ ์ํด์๋ ๋๋์ ๋ํ ๋ฐ์ดํฐ๊ฐ ํ์ํฉ๋๋ค!")
print(" - ์ต์ ์์ฒ ๊ฐ ์ด์์ ์ง๋ฌธ-๋ต๋ณ ์")
print(" - GPU ํ๊ฒฝ์์ ์ ์๊ฐ~์์ผ ํ์ต")
print(" - ๋ฐ์ดํฐ ํ์ง์ด ์ฑ๋ฅ์ ์ข์ฐํฉ๋๋ค!")
โ ๏ธ ๋ฅ๋ฌ๋ ์ฑ๋ด์ ํ์ค ๋ฅ๋ฌ๋ ์ฑ๋ด์ ๊ฐ๋ ฅํ์ง๋ง ์ฝ์ง ์์! ๋๋์ ํ์ต ๋ฐ์ดํฐ, ๊ฐ๋ ฅํ ์ปดํจํ ํ์(GPU), ๊ธด ํ์ต ์๊ฐ์ด ํ์ํด. ์ฒ์ ์์ํ๋ค๋ฉด ์ฌ์ ํ์ต๋ ๋ชจ๋ธ(Pre-trained Model)์ ํ์ฉํ๋ ๊ฒ ํ๋ช ํด. ๐ฏ
๐ ์ค์ ์ฑ๋ด ํ๋ ์์ํฌ ํ์ฉํ๊ธฐ
์ฒ์๋ถํฐ ๋ค ๋ง๋ค ํ์๋ ์์ด! ๊ฒ์ฆ๋ ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ๋ฉด ํจ์ฌ ๋น ๋ฅด๊ณ ์์ ์ ์ผ๋ก ์ฑ๋ด์ ๊ฐ๋ฐํ ์ ์์ง. ๐ ๏ธ
๐ญ Rasa: ์คํ์์ค ๋ํํ AI ํ๋ ์์ํฌ
Rasa๋ ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ์คํ์์ค ์ฑ๋ด ํ๋ ์์ํฌ์ผ. NLU(์์ฐ์ด ์ดํด)์ ๋ํ ๊ด๋ฆฌ๋ฅผ ๋ชจ๋ ์ง์ํด!
# Rasa ์ค์น
pip install rasa
# ์ ํ๋ก์ ํธ ์์ฑ
rasa init --no-prompt
# ํ๋ก์ ํธ ๊ตฌ์กฐ
# my_chatbot/
# โโโ data/
# โ โโโ nlu.yml # ํ์ต ๋ฐ์ดํฐ
# โ โโโ stories.yml # ๋ํ ์๋๋ฆฌ์ค
# โโโ models/ # ํ์ต๋ ๋ชจ๋ธ
# โโโ config.yml # ์ค์ ํ์ผ
# โโโ domain.yml # ๋๋ฉ์ธ ์ ์
# โโโ endpoints.yml # API ์๋ํฌ์ธํธ
# data/nlu.yml - ์๋์ ์์ ์ ์
version: "3.0"
nlu:
- intent: greet
examples: |
- ์๋
ํ์ธ์
- ์๋
- ํ์ด
- ๋ฐ๊ฐ์ต๋๋ค
- ์ข์ ์์นจ์ด์์
- intent: goodbye
examples: |
- ์๋
ํ ๊ฐ์ธ์
- ์๊ฐ
- ๋ค์์ ๋ด
- ๋ ๋ง๋์
- ๋ฐ์ด
- intent: order_pizza
examples: |
- ํผ์ ์ฃผ๋ฌธํ๊ณ ์ถ์ด์
- ํผ์ [ํํผ๋ก๋](pizza_type) ํ๋ ์ฃผ์ธ์
- [๋ง๋ฅด๊ฒ๋ฆฌํ](pizza_type) ํผ์ ์ฃผ๋ฌธํ ๊ฒ์
- [ํ์์ด์](pizza_type) ํผ์ 2ํ ์ฃผ๋ฌธ
- intent: check_delivery
examples: |
- ๋ฐฐ์ก ์ธ์ ์์
- ์ฃผ๋ฌธํ ๊ฑฐ ์ธ์ ๋์ฐฉํด์
- ๋ฐฐ์ก ์กฐํํ๊ณ ์ถ์ด์
- ๋ฐฐ์ก ์ํ ์๋ ค์ฃผ์ธ์
# data/stories.yml - ๋ํ ํ๋ฆ ์ ์
version: "3.0"
stories:
- story: ์ธ์ฌ ํ ํผ์ ์ฃผ๋ฌธ
steps:
- intent: greet
- action: utter_greet
- intent: order_pizza
- action: utter_ask_pizza_type
- intent: inform
entities:
- pizza_type
- action: utter_confirm_order
- action: action_process_order
- story: ๋ฐฐ์ก ์กฐํ
steps:
- intent: check_delivery
- action: utter_ask_order_number
- intent: inform
entities:
- order_number
- action: action_check_delivery_status
# domain.yml - ๋๋ฉ์ธ ์ ์
version: "3.0"
intents:
- greet
- goodbye
- order_pizza
- check_delivery
- inform
entities:
- pizza_type
- order_number
slots:
pizza_type:
type: text
mappings:
- type: from_entity
entity: pizza_type
order_number:
type: text
mappings:
- type: from_entity
entity: order_number
responses:
utter_greet:
- text: "์๋
ํ์ธ์! ํผ์ ์ฃผ๋ฌธ์ ๋์๋๋ฆด๊ฒ์! ๐"
utter_goodbye:
- text: "๊ฐ์ฌํฉ๋๋ค! ์ข์ ํ๋ฃจ ๋์ธ์! ๐"
utter_ask_pizza_type:
- text: "์ด๋ค ํผ์๋ฅผ ์ํ์๋์? (ํํผ๋ก๋, ๋ง๋ฅด๊ฒ๋ฆฌํ, ํ์์ด์)"
utter_confirm_order:
- text: "{pizza_type} ํผ์ ์ฃผ๋ฌธ์ด ์ ์๋์์ต๋๋ค!"
utter_ask_order_number:
- text: "์ฃผ๋ฌธ๋ฒํธ๋ฅผ ์๋ ค์ฃผ์ธ์!"
actions:
- action_process_order
- action_check_delivery_status
# actions.py - ์ปค์คํ
์ก์
์ ์
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
class ActionProcessOrder(Action):
def name(self) -> Text:
return "action_process_order"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
pizza_type = tracker.get_slot("pizza_type")
# ์ค์ ๋ก๋ ์ฌ๊ธฐ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฃผ๋ฌธ ์ ์ฅ
order_number = "ORD-12345" # ์์
message = f"{pizza_type} ํผ์ ์ฃผ๋ฌธ์ด ์๋ฃ๋์์ต๋๋ค! " \
f"์ฃผ๋ฌธ๋ฒํธ๋ {order_number}์
๋๋ค. " \
f"์ฝ 30๋ถ ํ ๋ฐฐ๋ฌ๋ ์์ ์
๋๋ค! ๐"
dispatcher.utter_message(text=message)
return []
class ActionCheckDeliveryStatus(Action):
def name(self) -> Text:
return "action_check_delivery_status"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
order_number = tracker.get_slot("order_number")
# ์ค์ ๋ก๋ ๋ฐฐ์ก ์์คํ
API ํธ์ถ
status = "๋ฐฐ๋ฌ ์ค" # ์์
message = f"์ฃผ๋ฌธ๋ฒํธ {order_number}๋ ํ์ฌ {status} ์ํ์
๋๋ค. " \
f"๊ณง ๋์ฐฉํ ์์ ์
๋๋ค! ๐"
dispatcher.utter_message(text=message)
return []
# ๋ชจ๋ธ ํ์ต
rasa train
# ์ฑ๋ด ์คํ
rasa run
# ์ก์
์๋ฒ ์คํ (๋ณ๋ ํฐ๋ฏธ๋)
rasa run actions
# ๋ํ ํ
์คํธ
rasa shell
๐ก Rasa์ ์ฅ์
โข ์์ ํ ์คํ์์ค (๋ฌด๋ฃ!)
โข ์จํ๋ ๋ฏธ์ค ๋ฐฐํฌ ๊ฐ๋ฅ (๋ฐ์ดํฐ ๋ณด์ ์ค์ํ ๋)
โข ์ปค์คํฐ๋ง์ด์ง ์์ ๋ ๋์
โข ํ๋ฐํ ์ปค๋ฎค๋ํฐ์ ํ๋ถํ ๋ฌธ์
โข ๋ค๊ตญ์ด ์ง์ (ํ๊ตญ์ด ํฌํจ)
๐ฌ Dialogflow: ๊ตฌ๊ธ์ ๋ํํ AI ํ๋ซํผ
์ฝ๋ฉ์ด ๋ถ๋ด์ค๋ฝ๋ค๋ฉด? Dialogflow๋ GUI ๊ธฐ๋ฐ์ผ๋ก ์ฝ๊ฒ ์ฑ๋ด์ ๋ง๋ค ์ ์์ด! ๊ตฌ๊ธ์ ๊ฐ๋ ฅํ NLP ์์ง์ ์ฌ์ฉํ์ง. โ๏ธ
๐ฏ Dialogflow ์์ํ๊ธฐ
1๋จ๊ณ: ํ๋ก์ ํธ ์์ฑ
Dialogflow ์ฝ์(https://dialogflow.cloud.google.com)์ ์ ์ํด์ ์ ์์ด์ ํธ๋ฅผ ๋ง๋ค์ด.
2๋จ๊ณ: Intent ์ ์
Intent๋ ์ฌ์ฉ์์ ์๋์ผ. "ํผ์ ์ฃผ๋ฌธ"์ด๋ผ๋ Intent๋ฅผ ๋ง๋ค๊ณ , ํ์ต ๋ฌธ๊ตฌ๋ค์ ์ถ๊ฐํด.
3๋จ๊ณ: Entity ์ ์
Entity๋ ์ค์ํ ์ ๋ณด์ผ. "ํผ์ ์ข
๋ฅ", "์ฌ์ด์ฆ", "์๋" ๊ฐ์ ๊ฑธ ์ถ์ถํด.
4๋จ๊ณ: Response ์์ฑ
๊ฐ Intent์ ๋ํ ์๋ต์ ์์ฑํด. ์ฌ๋ฌ ๋ฒ์ ์ ๋ง๋ค๋ฉด ๋๋ค์ผ๋ก ์ ํ๋ผ!
5๋จ๊ณ: Fulfillment ์ฐ๊ฒฐ
๋ณต์กํ ๋ก์ง์ด ํ์ํ๋ฉด ์นํ
(Webhook)์ผ๋ก ๋ฐฑ์๋ ์๋ฒ์ ์ฐ๊ฒฐํด.
# Dialogflow Webhook ์์ (Flask)
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
req = request.get_json()
# Intent ์ด๋ฆ ๊ฐ์ ธ์ค๊ธฐ
intent_name = req['queryResult']['intent']['displayName']
# ํ๋ผ๋ฏธํฐ ๊ฐ์ ธ์ค๊ธฐ
parameters = req['queryResult']['parameters']
# Intent๋ณ ์ฒ๋ฆฌ
if intent_name == "์ฃผ๋ฌธํ๊ธฐ":
pizza_type = parameters.get('pizza_type')
quantity = parameters.get('quantity', 1)
# ์ฃผ๋ฌธ ์ฒ๋ฆฌ ๋ก์ง
order_number = process_order(pizza_type, quantity)
response_text = f"{pizza_type} ํผ์ {quantity}ํ ์ฃผ๋ฌธ์ด ์๋ฃ๋์์ต๋๋ค! " \
f"์ฃผ๋ฌธ๋ฒํธ: {order_number}"
elif intent_name == "๋ฐฐ์ก์กฐํ":
order_number = parameters.get('order_number')
# ๋ฐฐ์ก ์กฐํ ๋ก์ง
status = check_delivery(order_number)
response_text = f"์ฃผ๋ฌธ๋ฒํธ {order_number}๋ ํ์ฌ {status} ์ํ์
๋๋ค."
else:
response_text = "์ฃ์กํฉ๋๋ค. ์ฒ๋ฆฌํ ์ ์๋ ์์ฒญ์
๋๋ค."
# ์๋ต ๋ฐํ
return jsonify({
'fulfillmentText': response_text
})
def process_order(pizza_type, quantity):
# ์ค์ ์ฃผ๋ฌธ ์ฒ๋ฆฌ ๋ก์ง
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์ฅ, ๊ฒฐ์ ์ฒ๋ฆฌ ๋ฑ
return "ORD-" + str(hash(pizza_type + str(quantity)))[:8]
def check_delivery(order_number):
# ์ค์ ๋ฐฐ์ก ์กฐํ ๋ก์ง
# ๋ฐฐ์ก ์์คํ
API ํธ์ถ ๋ฑ
statuses = ["์ฃผ๋ฌธ ์ ์", "์กฐ๋ฆฌ ์ค", "๋ฐฐ๋ฌ ์ค", "๋ฐฐ๋ฌ ์๋ฃ"]
return statuses[hash(order_number) % len(statuses)]
if __name__ == '__main__':
app.run(port=5000)
๐จ ์ฑ๋ด UI/UX ๋์์ธ
์๋ฌด๋ฆฌ ๋๋ํ ์ฑ๋ด์ด๋ผ๋ UI๊ฐ ๋ถํธํ๋ฉด ์ฌ์ฉ์๋ค์ด ๋ ๋! ์ฌ์ฉํ๊ธฐ ํธํ๊ณ ์์ ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค์ด๋ณด์. ๐
๐ฌ ์น ์ฑํ ์์ ฏ ๋ง๋ค๊ธฐ
<!-- chat-widget.html -->
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>์ฑ๋ด ์์ ฏ</title>
<style>.tx-header{background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);
color:#fff;
padding:40px 30px;
border-radius:16px;
text-align:center;
margin-bottom:40px;
box-shadow:0 8px 24px rgba(102,126,234,0.25);}
.tx-intro{background:#f8f9fa;
padding:25px;
border-radius:12px;
margin:30px 0;
border-left:5px solid #667eea;}
.tx-section{margin:40px 0;}
.tx-highlight{background:linear-gradient(120deg,#ffeaa7 0%,#fdcb6e 100%);
padding:3px 8px;
border-radius:4px;
font-weight:600;}
.tx-tip{background:#e8f5e9;
padding:20px;
border-radius:10px;
margin:25px 0;
border-left:4px solid #4caf50;}
.tx-warning{background:#fff3e0;
padding:20px;
border-radius:10px;
margin:25px 0;
border-left:4px solid #ff9800;}
.tx-point{color:#667eea;
font-weight:700;}
.tx-subtitle{color:#764ba2;
font-weight:600;
font-size:1.1em;
margin:20px 0 10px 0;}
.tx-card{background:#ffffff;
padding:20px;
border-radius:12px;
margin:20px 0;
box-shadow:0 4px 12px rgba(0,0,0,0.08);}
.tx-step{background:#f1f3f5;
padding:15px 20px;
border-radius:8px;
margin:15px 0;
border-left:3px solid #667eea;}</style>
</head>
<body>
<!-- ์ฑ๋ด ๋ฒํผ -->
<button class="chat-button" onclick="toggleChat()">๐ฌ</button>
<!-- ์ฑ๋ด ์ฐฝ -->
<div class="chat-window" id="chatWindow">
<div class="chat-header">
<h3>๐ค AI ์ฑ๋ด</h3>
<button class="close-btn" onclick="toggleChat()">ร</button>
</div>
<div class="chat-messages" id="chatMessages">
<div class="message bot">
<div class="message-content">
์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์? ๐
<div class="message-time">๋ฐฉ๊ธ ์ </div>
</div>
</div>
</div>
<div class="typing-indicator" id="typingIndicator">
<span></span>
<span></span>
<span></span>
</div>
<div class="chat-input-area">
<input
type="text"
class="chat-input"
id="chatInput"
placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์..."
onkeypress="handleKeyPress(event)"
>
<button class="send-btn" onclick="sendMessage()">โค</button>
</div>
</div>
<script>
// ์ฑ๋ด ํ ๊ธ
function toggleChat() {
const chatWindow = document.getElementById('chatWindow');
chatWindow.classList.toggle('active');
if (chatWindow.classList.contains('active')) {
document.getElementById('chatInput').focus();
}
}
// ๋ฉ์์ง ์ ์ก
async function sendMessage() {
const input = document.getElementById('chatInput');
const message = input.value.trim();
if (!message) return;
// ์ฌ์ฉ์ ๋ฉ์์ง ํ์
addMessage(message, 'user');
input.value = '';
// ํ์ดํ ์ธ๋์ผ์ดํฐ ํ์
showTypingIndicator();
// ์ฑ๋ด ์๋ต ๋ฐ๊ธฐ (์ค์ ๋ก๋ API ํธ์ถ)
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message: message })
});
const data = await response.json();
// ํ์ดํ ์ธ๋์ผ์ดํฐ ์จ๊ธฐ๊ธฐ
hideTypingIndicator();
// ๋ด ์๋ต ํ์
setTimeout(() => {
addMessage(data.response, 'bot');
}, 500);
} catch (error) {
hideTypingIndicator();
addMessage('์ฃ์กํฉ๋๋ค. ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๐ข', 'bot');
}
}
// ๋ฉ์์ง ์ถ๊ฐ
function addMessage(text, sender) {
const messagesDiv = document.getElementById('chatMessages');
const messageDiv = document.createElement('div');
messageDiv.className = `message ${sender}`;
const now = new Date();
const timeString = now.getHours() + ':' +
String(now.getMinutes()).padStart(2, '0');
messageDiv.innerHTML = `
<div class="message-content">
${text}
<div class="message-time">${timeString}</div>
</div>
`;
messagesDiv.appendChild(messageDiv);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
// ํ์ดํ ์ธ๋์ผ์ดํฐ
function showTypingIndicator() {
document.getElementById('typingIndicator').classList.add('active');
}
function hideTypingIndicator() {
document.getElementById('typingIndicator').classList.remove('active');
}
// ์ํฐํค ์ฒ๋ฆฌ
function handleKeyPress(event) {
if (event.key === 'Enter') {
sendMessage();
}
}
// ๋ฐ๋ชจ์ฉ ์๋ ์๋ต (์ค์ ๋ก๋ ์๋ฒ API ์ฌ์ฉ)
async function getDemoResponse(message) {
// ๊ฐ๋จํ ํค์๋ ๋งค์นญ
const responses = {
'์๋
': '์๋
ํ์ธ์! ๋ฐ๊ฐ์ต๋๋ค! ๐',
'๋์': '๋ฌด์์ ๋์๋๋ฆด๊น์? ๊ถ๊ธํ ์ ์ ๋ง์ํด์ฃผ์ธ์!',
'๊ณ ๋ง์': '์ฒ๋ง์์! ๋ ๊ถ๊ธํ ๊ฒ ์์ผ๋ฉด ๋ฌผ์ด๋ณด์ธ์!',
'์ด๋ฆ': '์ ๋ AI ์ฑ๋ด์ด์์! ๋น์ ์ ์ด๋ฆ์ ๋ฌด์์ธ๊ฐ์?',
};
for (let keyword in responses) {
if (message.includes(keyword)) {
return responses[keyword];
}
}
return 'ํฅ๋ฏธ๋ก์ด ์ง๋ฌธ์ด๋ค์! ๋ ์์ธํ ๋ง์ํด์ฃผ์๊ฒ ์ด์? ๐ค';
}
</script>
</body>
</html>
๐ก UI/UX ๋์์ธ ํ
โข ์๋ต ์๋: 2์ด ์ด๋ด์ ์๋ตํด์ผ ์ฌ์ฉ์๊ฐ ๊ธฐ๋ค๋ ค์ค
โข ํ์ดํ ํจ๊ณผ: ๋ด์ด ์๊ฐํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๊ฒ ํ๋ฉด ์์ฐ์ค๋ฌ์
โข ์๋ฌ ์ฒ๋ฆฌ: ์ดํด ๋ชป ํ์ ๋๋ ์น์ ํ๊ฒ ์๋ดํด
โข ๋น ๋ฅธ ๋ต๋ณ ๋ฒํผ: ์์ฃผ ๋ฌป๋ ์ง๋ฌธ์ ๋ฒํผ์ผ๋ก ์ ๊ณตํ๋ฉด ํธํด
โข ์ด๋ชจ์ง ํ์ฉ: ๊ฐ์ ์ ํํํ๋ฉด ๋ ์น๊ทผํด์ ธ! ๐
๐ ์ธ๋ถ ์๋น์ค ์ฐ๋ํ๊ธฐ
์ฑ๋ด์ ๋ ๋๋ํ๊ฒ ๋ง๋ค๋ ค๋ฉด ์ธ๋ถ API์ ์ฐ๋ํด์ผ ํด! ๋ ์จ, ๋ด์ค, ๊ฒฐ์ , ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฑ ๋ค์ํ ์๋น์ค๋ฅผ ๋ถ์ฌ๋ณด์. ๐
๐ค๏ธ ๋ ์จ API ์ฐ๋ ์์
# weather_chatbot.py
import requests
from flask import Flask, request, jsonify
app = Flask(__name__)
class WeatherChatbot:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = "http://api.openweathermap.org/data/2.5/weather"
def get_weather(self, city):
"""๋ ์จ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ"""
try:
params = {
'q': city,
'appid': self.api_key,
'units': 'metric', # ์ญ์จ ์จ๋
'lang': 'kr' # ํ๊ตญ์ด
}
response = requests.get(self.base_url, params=params)
data = response.json()
if response.status_code == 200:
temp = data['main']['temp']
feels_like = data['main']['feels_like']
description = data['weather'][0]['description']
humidity = data['main']['humidity']
message = f"๐ค๏ธ {city}์ ํ์ฌ ๋ ์จ\n\n" \
f"๐ก๏ธ ์จ๋: {temp}ยฐC (์ฒด๊ฐ: {feels_like}ยฐC)\n" \
f"โ๏ธ ๋ ์จ: {description}\n" \
f"๐ง ์ต๋: {humidity}%"
return message
else:
return "์ฃ์กํด์, ํด๋น ๋์์ ๋ ์จ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ด์. ๐ข"
except Exception as e:
return f"๋ ์จ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ด์: {str(e)}"
def process_message(self, message):
"""๋ฉ์์ง ์ฒ๋ฆฌ"""
message = message.lower()
# ๋ ์จ ๊ด๋ จ ํค์๋ ํ์ธ
weather_keywords = ['๋ ์จ', 'weather', '๊ธฐ์จ', '์จ๋']
if any(keyword in message for keyword in weather_keywords):
# ๋์ ์ด๋ฆ ์ถ์ถ (๊ฐ๋จํ ๋ฐฉ๋ฒ)
cities = ['์์ธ', '๋ถ์ฐ', '๋๊ตฌ', '์ธ์ฒ', '๊ด์ฃผ', '๋์ ', '์ธ์ฐ']
for city in cities:
if city in message:
return self.get_weather(city)
return "์ด๋ ๋์์ ๋ ์จ๊ฐ ๊ถ๊ธํ์ ๊ฐ์? (์: ์์ธ ๋ ์จ)"
return "๋ ์จ ์ ๋ณด๋ฅผ ์ํ์๋ฉด '๋์์ด๋ฆ ๋ ์จ'๋ผ๊ณ ๋ง์ํด์ฃผ์ธ์! ๐ค๏ธ"
# API ์๋ํฌ์ธํธ
@app.route('/chat', methods=['POST'])
def chat():
data = request.json
user_message = data.get('message', '')
# OpenWeatherMap API ํค (์ค์ ๋ก๋ ํ๊ฒฝ๋ณ์๋ก ๊ด๋ฆฌ)
API_KEY = "your_api_key_here"
chatbot = WeatherChatbot(API_KEY)
response = chatbot.process_message(user_message)
return jsonify({'response': response})
if __name__ == '__main__':
app.run(debug=True, port=5000)
๐ณ ๊ฒฐ์ ์์คํ ์ฐ๋
# payment_chatbot.py
import stripe
from flask import Flask, request, jsonify
app = Flask(__name__)
# Stripe API ํค ์ค์
stripe.api_key = "your_stripe_secret_key"
class PaymentChatbot:
def __init__(self):
self.cart = {} # ์ฅ๋ฐ๊ตฌ๋
self.products = {
'ํผ์': {'name': 'ํํผ๋ก๋ ํผ์', 'price': 15000},
'ํ์คํ': {'name': '๊น๋ฅด๋ณด๋๋ผ ํ์คํ', 'price': 12000},
'์๋ฌ๋': {'name': '์์ ์๋ฌ๋', 'price': 8000},
}
def add_to_cart(self, user_id, product_key, quantity=1):
"""์ฅ๋ฐ๊ตฌ๋์ ์ถ๊ฐ"""
if product_key not in self.products:
return "์ฃ์กํด์, ํด๋น ์ํ์ ์ฐพ์ ์ ์์ด์. ๐ข"
if user_id not in self.cart:
self.cart[user_id] = {}
if product_key in self.cart[user_id]:
self.cart[user_id][product_key] += quantity
else:
self.cart[user_id][product_key] = quantity
product = self.products[product_key]
return f"โ
{product['name']} {quantity}๊ฐ๊ฐ ์ฅ๋ฐ๊ตฌ๋์ ์ถ๊ฐ๋์์ด์!"
def show_cart(self, user_id):
"""์ฅ๋ฐ๊ตฌ๋ ๋ณด๊ธฐ"""
if user_id not in self.cart or not self.cart[user_id]:
return "์ฅ๋ฐ๊ตฌ๋๊ฐ ๋น์ด์์ด์! ๐"
message = "๐ ์ฅ๋ฐ๊ตฌ๋\n\n"
total = 0
for product_key, quantity in self.cart[user_id].items():
product = self.products[product_key]
subtotal = product['price'] * quantity
total += subtotal
message += f"โข {product['name']} x{quantity}: {subtotal:,}์\n"
message += f"\n๐ฐ ์ด ๊ธ์ก: {total:,}์"
return message
def create_payment(self, user_id):
"""๊ฒฐ์ ์์ฑ"""
if user_id not in self.cart or not self.cart[user_id]:
return {"success": False, "message": "์ฅ๋ฐ๊ตฌ๋๊ฐ ๋น์ด์์ด์!"}
# ์ด ๊ธ์ก ๊ณ์ฐ
total = 0
items = []
for product_key, quantity in self.cart[user_id].items():
product = self.products[product_key]
subtotal = product['price'] * quantity
total += subtotal
items.append({
'name': product['name'],
'quantity': quantity,
'amount': product['price']
})
try:
# Stripe ๊ฒฐ์ ์ธํ
ํธ ์์ฑ
payment_intent = stripe.PaymentIntent.create(
amount=total, # ์ ๋จ์
currency='krw',
metadata={
'user_id': user_id,
'items': str(items)
}
)
# ์ฅ๋ฐ๊ตฌ๋ ๋น์ฐ๊ธฐ
self.cart[user_id] = {}
return {
"success": True,
"message": f"๊ฒฐ์ ๊ฐ ์์ฑ๋์์ด์! ๐ณ\n์ด {total:,}์",
"client_secret": payment_intent.client_secret
}
except Exception as e:
return {
"success": False,
"message": f"๊ฒฐ์ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ด์: {str(e)}"
}
chatbot = PaymentChatbot()
@app.route('/chat', methods=['POST'])
def chat():
data = request.json
user_id = data.get('user_id', 'guest')
message = data.get('message', '').lower()
# ๋ฉ์์ง ์ฒ๋ฆฌ
if '์ถ๊ฐ' in message or '์ฃผ๋ฌธ' in message:
for product_key in chatbot.products.keys():
if product_key in message:
response = chatbot.add_to_cart(user_id, product_key)
return jsonify({'response': response})
return jsonify({'response': '์ด๋ค ์ํ์ ์ถ๊ฐํ์๊ฒ ์ด์? (ํผ์, ํ์คํ, ์๋ฌ๋)'})
elif '์ฅ๋ฐ๊ตฌ๋' in message or '์นดํธ' in message:
response = chatbot.show_cart(user_id)
return jsonify({'response': response})
elif '๊ฒฐ์ ' in message or '๊ตฌ๋งค' in message:
result = chatbot.create_payment(user_id)
return jsonify(result)
else:
return jsonify({
'response': '์ฃผ๋ฌธํ์๋ ค๋ฉด "ํผ์ ์ถ๊ฐ", ์ฅ๋ฐ๊ตฌ๋๋ฅผ ๋ณด์๋ ค๋ฉด "์ฅ๋ฐ๊ตฌ๋", '
'๊ฒฐ์ ํ์๋ ค๋ฉด "๊ฒฐ์ "๋ผ๊ณ ๋ง์ํด์ฃผ์ธ์! ๐'
})
if __name__ == '__main__':
app.run(debug=True, port=5000)
โ ๏ธ ๋ณด์ ์ฃผ์์ฌํญ
โข API ํค๋ ์ ๋ ์ฝ๋์ ์ง์ ๋ฃ์ง ๋ง๊ณ ํ๊ฒฝ๋ณ์๋ก ๊ด๋ฆฌํด
โข ๊ฒฐ์ ์ ๋ณด๋ PCI DSS ๊ท์ ์ ์ค์ํด์ผ ํด
โข HTTPS๋ฅผ ๋ฐ๋์ ์ฌ์ฉํด (์ํธํ ํต์ )
โข ์ฌ์ฉ์ ์
๋ ฅ์ ํญ์ ๊ฒ์ฆํ๊ณ sanitizeํด
โข ๋ฏผ๊ฐํ ์ ๋ณด๋ ๋ก๊ทธ์ ๋จ๊ธฐ์ง ๋ง!
๐ ์ฑ๋ด ์ฑ๋ฅ ์ธก์ ๊ณผ ๊ฐ์
์ฑ๋ด์ ๋ง๋ค์๋ค๊ณ ๋์ด ์๋์ผ! ์ง์์ ์ผ๋ก ์ฑ๋ฅ์ ์ธก์ ํ๊ณ ๊ฐ์ ํด์ผ ํด. ๋ฐ์ดํฐ ๊ธฐ๋ฐ์ผ๋ก ๋๋ํ๊ฒ ๋ง๋ค์ด๊ฐ์! ๐
๐ฏ ์ฃผ์ ์ฑ๋ฅ ์งํ (KPI)
1. ์ ํ๋ (Accuracy) ๐ฏ
์ฌ์ฉ์ ์๋๋ฅผ ์ผ๋ง๋ ์ ํํ๊ฒ ํ์
ํ๋๊ฐ?
๋ชฉํ: 85% ์ด์
2. ์๋ต ์๊ฐ (Response Time) โฑ๏ธ
์ง๋ฌธ๋ถํฐ ๋ต๋ณ๊น์ง ๊ฑธ๋ฆฌ๋ ์๊ฐ
๋ชฉํ: 2์ด ์ด๋ด
3. ์๋ฃ์จ (Completion Rate) โ
์ฌ์ฉ์๊ฐ ์ํ๋ ์์
์ ์๋ฃํ ๋น์จ
๋ชฉํ: 70% ์ด์
4. ์ฌ์ฉ์ ๋ง์กฑ๋ (CSAT) ๐
๋ํ ํ ์ฌ์ฉ์ ๋ง์กฑ๋ ์ ์
๋ชฉํ: 4.0/5.0 ์ด์
5. ํด๋ฐฑ๋ฅ (Fallback Rate) ๐ค
"์ดํดํ์ง ๋ชปํ์ด์" ์๋ต ๋น์จ
๋ชฉํ: 15% ์ดํ
๐ ๋ก๊น ๋ฐ ๋ถ์ ์์คํ
# analytics_chatbot.py
import logging
from datetime import datetime
import json
from collections import defaultdict
class ChatbotAnalytics:
def __init__(self):
# ๋ก๊น
์ค์
logging.basicConfig(
filename='chatbot.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
self.conversations = []
self.metrics = defaultdict(int)
def log_conversation(self, user_id, user_message, bot_response,
intent=None, confidence=None, response_time=None):
"""๋ํ ๋ก๊น
"""
conversation = {
'timestamp': datetime.now().isoformat(),
'user_id': user_id,
'user_message': user_message,
'bot_response': bot_response,
'intent': intent,
'confidence': confidence,
'response_time': response_time
}
self.conversations.append(conversation)
# ํ์ผ์ ์ ์ฅ
logging.info(json.dumps(conversation, ensure_ascii=False))
# ๋ฉํธ๋ฆญ ์
๋ฐ์ดํธ
self.metrics['total_conversations'] += 1
if intent:
self.metrics[f'intent_{intent}'] += 1
if confidence and confidence < 0.5:
self.metrics['low_confidence'] += 1
def log_feedback(self, user_id, conversation_id, rating, comment=None):
"""์ฌ์ฉ์ ํผ๋๋ฐฑ ๋ก๊น
"""
feedback = {
'timestamp': datetime.now().isoformat(),
'user_id': user_id,
'conversation_id': conversation_id,
'rating': rating,
'comment': comment
}
logging.info(f"FEEDBACK: {json.dumps(feedback, ensure_ascii=False)}")
self.metrics['total_feedback'] += 1
self.metrics['rating_sum'] += rating
def get_statistics(self):
"""ํต๊ณ ์กฐํ"""
total = self.metrics['total_conversations']
if total == 0:
return "์์ง ๋ํ ๋ฐ์ดํฐ๊ฐ ์์ด์!"
stats = {
'์ด ๋ํ ์': total,
'๋ฎ์ ์ ๋ขฐ๋ ๋น์จ': f"{(self.metrics['low_confidence'] / total * 100):.1f}%",
}
# ํ๊ท ๋ง์กฑ๋
if self.metrics['total_feedback'] > 0:
avg_rating = self.metrics['rating_sum'] / self.metrics['total_feedback']
stats['ํ๊ท ๋ง์กฑ๋'] = f"{avg_rating:.2f}/5.0"
# ์ธ๊ธฐ ์๋
intent_counts = {k: v for k, v in self.metrics.items() if k.startswith('intent_')}
if intent_counts:
top_intent = max(intent_counts.items(), key=lambda x: x[1])
stats['๊ฐ์ฅ ๋ง์ ์๋'] = top_intent[0].replace('intent_', '')
return stats
def analyze_failed_conversations(self):
"""์คํจํ ๋ํ ๋ถ์"""
failed = [
conv for conv in self.conversations
if conv.get('confidence', 1.0) < 0.5
]
if not failed:
return "์คํจํ ๋ํ๊ฐ ์์ด์! ๐"
# ์คํจ ํจํด ๋ถ์
failed_messages = [conv['user_message'] for conv in failed]
return {
'์คํจ ๊ฑด์': len(failed),
'์คํจ์จ': f"{(len(failed) / len(self.conversations) * 100):.1f}%",
'์คํจ ๋ฉ์์ง ์์': failed_messages[:5]
}
def generate_report(self):
"""์ข
ํฉ ๋ฆฌํฌํธ ์์ฑ"""
report = "=" * 50 + "\n"
report += "๐ ์ฑ๋ด ์ฑ๋ฅ ๋ฆฌํฌํธ\n"
report += "=" * 50 + "\n\n"
# ๊ธฐ๋ณธ ํต๊ณ
stats = self.get_statistics()
report += "๐ ๊ธฐ๋ณธ ํต๊ณ\n"
for key, value in stats.items():
report += f" โข {key}: {value}\n"
report += "\n"
# ์คํจ ๋ถ์
failed_analysis = self.analyze_failed_conversations()
report += "โ ์คํจ ๋ถ์\n"
if isinstance(failed_analysis, dict):
for key, value in failed_analysis.items():
report += f" โข {key}: {value}\n"
else:
report += f" {failed_analysis}\n"
report += "\n"
# ๊ฐ์ ์ ์
report += "๐ก ๊ฐ์ ์ ์\n"
if self.metrics['low_confidence'] / self.metrics['total_conversations'] > 0.2:
report += " โข ๋ฎ์ ์ ๋ขฐ๋ ๋น์จ์ด ๋์ต๋๋ค. ํ์ต ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ์ธ์.\n"
if self.metrics.get('total_feedback', 0) > 0:
avg_rating = self.metrics['rating_sum'] / self.metrics['total_feedback']
if avg_rating < 3.5:
report += " โข ์ฌ์ฉ์ ๋ง์กฑ๋๊ฐ ๋ฎ์ต๋๋ค. ์๋ต ํ์ง์ ๊ฐ์ ํ์ธ์.\n"
report += "\n" + "=" * 50
return report
# ์ฌ์ฉ ์์
if __name__ == "__main__":
analytics = ChatbotAnalytics()
# ๋ํ ๋ก๊น
analytics.log_conversation(
user_id="user123",
user_message="์๋
ํ์ธ์",
bot_response="์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์?",
intent="greeting",
confidence=0.95,
response_time=0.3
)
analytics.log_conversation(
user_id="user123",
user_message="์ด๊ฑฐ ๋ญ์ผ",
bot_response="์ฃ์กํด์, ์ดํดํ์ง ๋ชปํ์ด์.",
intent=None,
confidence=0.2,
response_time=0.5
)
# ํผ๋๋ฐฑ ๋ก๊น
analytics.log_feedback(
user_id="user123",
conversation_id="conv001",
rating=4,
comment="๋์์ด ๋์์ด์!"
)
# ๋ฆฌํฌํธ ์์ฑ
print(analytics.generate_report())
๐ก A/B ํ
์คํ
๋ ๊ฐ์ง ๋ฒ์ ์ ์๋ต์ ๋ง๋ค์ด์ ์ด๋ ๊ฒ ๋ ํจ๊ณผ์ ์ธ์ง ํ
์คํธํด๋ด! ์๋ฅผ ๋ค์ด:
โข ๋ฒ์ A: "์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์?"
โข ๋ฒ์ B: "๋ฐ๊ฐ์ต๋๋ค! ๊ถ๊ธํ ์ ์ด ์์ผ์ ๊ฐ์?"
์ฌ์ฉ์ ๋ฐ์์ ์ธก์ ํด์ ๋ ๋์ ๋ฒ์ ์ ์ ํํ๋ฉด ๋ผ! ๐
๐ ์ฑ๋ด ๋ฐฐํฌํ๊ธฐ
๋ก์ปฌ์์๋ง ๋์๊ฐ๋ ์ฑ๋ด์ ์๋ฏธ๊ฐ ์์ง! ์ค์ ์ฌ์ฉ์๋ค์ด ์ ๊ทผํ ์ ์๋๋ก ๋ฐฐํฌํด๋ณด์. โ๏ธ
๐ณ Docker๋ก ์ปจํ ์ด๋ํ
# Dockerfile
FROM python:3.9-slim
# ์์
๋๋ ํ ๋ฆฌ ์ค์
WORKDIR /app
# ์์กด์ฑ ํ์ผ ๋ณต์ฌ
COPY requirements.txt .
# ์์กด์ฑ ์ค์น
RUN pip install --no-cache-dir -r requirements.txt
# ์ ํ๋ฆฌ์ผ์ด์
ํ์ผ ๋ณต์ฌ
COPY . .
# ํฌํธ ๋
ธ์ถ
EXPOSE 5000
# ํ๊ฒฝ๋ณ์ ์ค์
ENV FLASK_APP=app.py
ENV FLASK_ENV=production
# ์คํ ๋ช
๋ น
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "4", "app:app"]
# requirements.txt
flask==2.3.0
gunicorn==20.1.0
nltk==3.8.1
tensorflow==2.12.0
scikit-learn==1.2.2
requests==2.31.0
python-dotenv==1.0.0
# docker-compose.yml
version: '3.8'
services:
chatbot:
build: .
ports:
- "5000:5000"
environment:
- DATABASE_URL=postgresql://user:password@db:5432/chatbot
- REDIS_URL=redis://redis:6379
depends_on:
- db
- redis
restart: always
db:
image: postgres:15
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=chatbot
volumes:
- postgres_data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
postgres_data:
# ๋น๋ ๋ฐ ์คํ
docker-compose build
docker-compose up -d
# ๋ก๊ทธ ํ์ธ
docker-compose logs -f chatbot
# ์ค์ง
docker-compose down
โ๏ธ ํด๋ผ์ฐ๋ ๋ฐฐํฌ (AWS, GCP, Azure)
๐ ๋ฐฐํฌ ์ต์ ๋น๊ต
AWS (Amazon Web Services)
โข EC2: ๊ฐ์ ์๋ฒ์ ์ง์ ๋ฐฐํฌ
โข Lambda: ์๋ฒ๋ฆฌ์ค๋ก ๋น์ฉ ์ ๊ฐ
โข ECS/EKS: ์ปจํ
์ด๋ ์ค์ผ์คํธ๋ ์ด์
โข Lex: AWS์ ์ฑ๋ด ์ ์ฉ ์๋น์ค
GCP (Google Cloud Platform)
โข Compute Engine: VM ์ธ์คํด์ค
โข Cloud Run: ์ปจํ
์ด๋ ์๋ ์ค์ผ์ผ๋ง
โข Dialogflow: ๊ตฌ๊ธ์ ์ฑ๋ด ํ๋ซํผ
โข Vertex AI: ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ ๋ฐฐํฌ
Azure (Microsoft)
โข Virtual Machines: VM ๋ฐฐํฌ
โข App Service: PaaS ํ๋ซํผ
โข Bot Service: ์ฑ๋ด ์ ์ฉ ์๋น์ค
โข Cognitive Services: AI ์๋น์ค
๐ง Heroku ๊ฐ๋จ ๋ฐฐํฌ
# Procfile
web: gunicorn app:app
# runtime.txt
python-3.9.16
# Heroku ๋ฐฐํฌ ๋ช
๋ น์ด
# Heroku CLI ์ค์น ํ
# ๋ก๊ทธ์ธ
heroku login
# ์ฑ ์์ฑ
heroku create my-chatbot-app
# Git ์ ์ฅ์ ์ฐ๊ฒฐ
git init
git add .
git commit -m "Initial commit"
# Heroku์ ํธ์
git push heroku main
# ๋ก๊ทธ ํ์ธ
heroku logs --tail
# ์ฑ ์ด๊ธฐ
heroku open
๐ ์ฑ๋ด ๊ฐ๋ฐ ํ์ต ๋ก๋๋งต
์ฑ๋ด ๊ฐ๋ฐ์๊ฐ ๋๊ณ ์ถ๋ค๋ฉด? ์ด ๋ก๋๋งต์ ๋ฐ๋ผ๊ฐ๋ด! ๋จ๊ณ๋ณ๋ก ์ฐจ๊ทผ์ฐจ๊ทผ ์ฑ์ฅํ ์ ์์ด. ๐ฑ
1๋จ๊ณ: ํ๋ก๊ทธ๋๋ฐ ๊ธฐ์ด (3๊ฐ์) ๐
โข Python ๋ฌธ๋ฒ ์๋ฒฝ ๋ง์คํฐ
โข Flask/Django ์น ํ๋ ์์ํฌ
โข Git/GitHub ๋ฒ์ ๊ด๋ฆฌ
โข REST API ์ดํด
โข ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ธฐ์ด (SQL)
2๋จ๊ณ: ์์ฐ์ด ์ฒ๋ฆฌ (4๊ฐ์) ๐ค
โข NLTK, spaCy ๋ผ์ด๋ธ๋ฌ๋ฆฌ
โข ํ ํฐํ, ํํ์ ๋ถ์
โข TF-IDF, Word2Vec
โข ๊ฐ์ ๋ถ์, ๊ฐ์ฒด๋ช
์ธ์
โข ํ๊ตญ์ด NLP (KoNLPy)
3๋จ๊ณ: ๋จธ์ ๋ฌ๋ (4๊ฐ์) ๐ค
โข Scikit-learn ๋ง์คํฐ
โข ๋ถ๋ฅ, ํ๊ท, ํด๋ฌ์คํฐ๋ง
โข ์๋ ๋ถ๋ฅ ๋ชจ๋ธ ๊ตฌ์ถ
โข ๋ชจ๋ธ ํ๊ฐ ๋ฐ ํ๋
โข ์ค์ ํ๋ก์ ํธ (๊ฐ๋จํ ์ฑ๋ด)
4๋จ๊ณ: ๋ฅ๋ฌ๋ (6๊ฐ์) ๐ง
โข TensorFlow/PyTorch
โข RNN, LSTM, GRU
โข Seq2Seq, Attention
โข Transformer, BERT
โข ํ์ธํ๋ ๊ธฐ๋ฒ
5๋จ๊ณ: ์ค์ ๋ฐฐํฌ (3๊ฐ์) ๐
โข Docker ์ปจํ
์ด๋ํ
โข ํด๋ผ์ฐ๋ ๋ฐฐํฌ (AWS/GCP)
โข CI/CD ํ์ดํ๋ผ์ธ
โข ๋ชจ๋ํฐ๋ง ๋ฐ ๋ก๊น
โข ํฌํธํด๋ฆฌ์ค ๊ตฌ์ถ
๐ก ํ์ต ํ
โข ์ด๋ก ๋ง ๊ณต๋ถํ์ง ๋ง๊ณ ์ง์ ๋ง๋ค์ด๋ด!
โข ์์ ํ๋ก์ ํธ๋ถํฐ ์์ํด์ ์ ์ ํค์๊ฐ
โข GitHub์ ์ฝ๋๋ฅผ ๊ณต์ ํ๊ณ ํผ๋๋ฐฑ ๋ฐ์
โข ์จ๋ผ์ธ ์ปค๋ฎค๋ํฐ์ ์ฐธ์ฌํด (Reddit, Stack Overflow)
โข ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์ค์ ๊ฒฝํ ์๊ธฐ!
๐ ์ต์ ํธ๋ ๋: GPT์ LLM ํ์ฉ
2023๋ ์ดํ ์ฑ๋ด์ ํ๋๋ฅผ ๋ฐ๊พผ ๊ฑด ๋ฐ๋ก GPT ๊ฐ์ ๋๊ท๋ชจ ์ธ์ด ๋ชจ๋ธ(LLM)์ด์ผ! OpenAI API๋ฅผ ํ์ฉํ๋ฉด ํจ์ฌ ๋๋ํ ์ฑ๋ด์ ๋ง๋ค ์ ์์ด. ๐ฅ
๐ค OpenAI API ํ์ฉํ๊ธฐ
# gpt_chatbot.py
import openai
from flask import Flask, request, jsonify
import os
app = Flask(__name__)
# OpenAI API ํค ์ค์
openai.api_key = os.getenv("OPENAI_API_KEY")
class GPTChatbot:
def __init__(self, system_prompt=None):
self.system_prompt = system_prompt or """
๋น์ ์ ์น์ ํ๊ณ ๋์์ด ๋๋ AI ์ด์์คํดํธ์
๋๋ค.
์ฌ์ฉ์์ ์ง๋ฌธ์ ์ ํํ๊ณ ์ ์ฉํ ๋ต๋ณ์ ์ ๊ณตํ์ธ์.
ํ๊ตญ์ด๋ก ์์ฐ์ค๋ฝ๊ฒ ๋ํํ์ธ์.
"""
self.conversation_history = []
def chat(self, user_message):
"""GPT์ ๋ํ"""
try:
# ๋ํ ํ์คํ ๋ฆฌ์ ์ถ๊ฐ
self.conversation_history.append({
"role": "user",
"content": user_message
})
# GPT API ํธ์ถ
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # ๋๋ gpt-4
messages=[
{"role": "system", "content": self.system_prompt}
] + self.conversation_history,
temperature=0.7,
max_tokens=500
)
# ์๋ต ์ถ์ถ
bot_message = response.choices[0].message.content
# ๋ํ ํ์คํ ๋ฆฌ์ ์ถ๊ฐ
self.conversation_history.append({
"role": "assistant",
"content": bot_message
})
# ํ์คํ ๋ฆฌ๊ฐ ๋๋ฌด ๊ธธ์ด์ง๋ฉด ์ค๋๋ ๊ฒ ์ ๊ฑฐ
if len(self.conversation_history) > 20:
self.conversation_history = self.conversation_history[-20:]
return bot_message
except Exception as e:
return f"์ฃ์กํด์, ์ค๋ฅ๊ฐ ๋ฐ์ํ์ด์: {str(e)}"
def reset_conversation(self):
"""๋ํ ํ์คํ ๋ฆฌ ์ด๊ธฐํ"""
self.conversation_history = []
# ํนํ๋ ์ฑ๋ด ์์
class CustomerServiceBot(GPTChatbot):
def __init__(self):
system_prompt = """
๋น์ ์ ์ ๋ฌธ์ ์ธ ๊ณ ๊ฐ ์๋น์ค ๋ด๋น์์
๋๋ค.
์ญํ :
- ๊ณ ๊ฐ์ ๋ฌธ์์ฌํญ์ ์น์ ํ๊ณ ์ ํํ๊ฒ ๋ต๋ณ
- ์ ํ ์ ๋ณด, ๋ฐฐ์ก, ๋ฐํ/๊ตํ ๊ด๋ จ ๋์ ์ ๊ณต
- ๋ถ๋ง ์ฌํญ์ ๊ณต๊ฐํ๋ฉฐ ํด๊ฒฐ์ฑ
์ ์
- ํญ์ ์กด๋๋ง ์ฌ์ฉ
์ ํ ์ ๋ณด:
- ํผ์: 15,000์ (ํํผ๋ก๋, ๋ง๋ฅด๊ฒ๋ฆฌํ, ํ์์ด์)
- ๋ฐฐ์ก: ์ฃผ๋ฌธ ํ 30-40๋ถ ์์
- ๋ฐํ: ๋ฐฐ์ก ํ 1์๊ฐ ์ด๋ด ๊ฐ๋ฅ
๋ต๋ณ ํ์:
1. ๊ณ ๊ฐ ๋ฌธ์ ํ์ธ
2. ๋ช
ํํ ๋ต๋ณ ์ ๊ณต
3. ์ถ๊ฐ ๋์ ์ ์
"""
super().__init__(system_prompt)
# API ์๋ํฌ์ธํธ
chatbot = CustomerServiceBot()
@app.route('/chat', methods=['POST'])
def chat():
data = request.json
user_message = data.get('message', '')
if not user_message:
return jsonify({'error': '๋ฉ์์ง๋ฅผ ์
๋ ฅํด์ฃผ์ธ์'}), 400
response = chatbot.chat(user_message)
return jsonify({
'response': response,
'conversation_length': len(chatbot.conversation_history)
})
@app.route('/reset', methods=['POST'])
def reset():
chatbot.reset_conversation()
return jsonify({'message': '๋ํ๊ฐ ์ด๊ธฐํ๋์์ต๋๋ค'})
if __name__ == '__main__':
app.run(debug=True, port=5000)
๐จ Function Calling์ผ๋ก ๊ธฐ๋ฅ ํ์ฅ
# function_calling_bot.py
import openai
import json
class FunctionCallingBot:
def __init__(self):
self.functions = [
{
"name": "get_weather",
"description": "ํน์ ๋์์ ํ์ฌ ๋ ์จ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "๋์ ์ด๋ฆ (์: ์์ธ, ๋ถ์ฐ)"
}
},
"required": ["city"]
}
},
{
"name": "calculate_delivery_time",
"description": "์ฃผ๋ฌธ๋ถํฐ ๋ฐฐ๋ฌ๊น์ง ์์ ์๊ฐ์ ๊ณ์ฐํฉ๋๋ค",
"parameters": {
"type": "object",
"properties": {
"distance": {
"type": "number",
"description": "๋ฐฐ๋ฌ ๊ฑฐ๋ฆฌ (km)"
},
"traffic": {
"type": "string",
"enum": ["์ํ", "๋ณดํต", "ํผ์ก"],
"description": "๊ตํต ์ํฉ"
}
},
"required": ["distance"]
}
}
]
def get_weather(self, city):
"""๋ ์จ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ (์ค์ ๋ก๋ API ํธ์ถ)"""
# ๋ฐ๋ชจ์ฉ ๋๋ฏธ ๋ฐ์ดํฐ
weather_data = {
"์์ธ": {"temp": 15, "condition": "๋ง์"},
"๋ถ์ฐ": {"temp": 18, "condition": "ํ๋ฆผ"},
"๋๊ตฌ": {"temp": 16, "condition": "๋น"}
}
data = weather_data.get(city, {"temp": 20, "condition": "์ ์ ์์"})
return f"{city}์ ํ์ฌ ๋ ์จ๋ {data['condition']}์ด๋ฉฐ, ๊ธฐ์จ์ {data['temp']}ยฐC์
๋๋ค."
def calculate_delivery_time(self, distance, traffic="๋ณดํต"):
"""๋ฐฐ๋ฌ ์๊ฐ ๊ณ์ฐ"""
base_time = distance * 3 # ๊ธฐ๋ณธ: km๋น 3๋ถ
traffic_multiplier = {
"์ํ": 0.8,
"๋ณดํต": 1.0,
"ํผ์ก": 1.5
}
total_time = int(base_time * traffic_multiplier.get(traffic, 1.0))
return f"์์ ๋ฐฐ๋ฌ ์๊ฐ์ ์ฝ {total_time}๋ถ์
๋๋ค."
def chat(self, user_message):
"""Function Calling์ ํ์ฉํ ๋ํ"""
try:
# GPT API ํธ์ถ
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "๋น์ ์ ๋์์ด ๋๋ AI ์ด์์คํดํธ์
๋๋ค."},
{"role": "user", "content": user_message}
],
functions=self.functions,
function_call="auto"
)
message = response.choices[0].message
# Function Call์ด ์๋์ง ํ์ธ
if message.get("function_call"):
function_name = message["function_call"]["name"]
function_args = json.loads(message["function_call"]["arguments"])
# ํจ์ ์คํ
if function_name == "get_weather":
function_response = self.get_weather(**function_args)
elif function_name == "calculate_delivery_time":
function_response = self.calculate_delivery_time(**function_args)
else:
function_response = "์ง์ํ์ง ์๋ ๊ธฐ๋ฅ์
๋๋ค."
# ํจ์ ๊ฒฐ๊ณผ๋ฅผ ํฌํจํด์ ๋ค์ GPT ํธ์ถ
second_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "๋น์ ์ ๋์์ด ๋๋ AI ์ด์์คํดํธ์
๋๋ค."},
{"role": "user", "content": user_message},
message,
{
"role": "function",
"name": function_name,
"content": function_response
}
]
)
return second_response.choices[0].message.content
else:
# ์ผ๋ฐ ์๋ต
return message.content
except Exception as e:
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ด์: {str(e)}"
# ์ฌ์ฉ ์์
if __name__ == "__main__":
bot = FunctionCallingBot()
print("Function Calling Bot: ์๋
ํ์ธ์! ๋ ์จ๋ ๋ฐฐ๋ฌ ์๊ฐ์ ๋ฌผ์ด๋ณด์ธ์!")
print("-" * 60)
# ํ
์คํธ
test_messages = [
"์์ธ ๋ ์จ ์ด๋?",
"5km ๊ฑฐ๋ฆฌ ๋ฐฐ๋ฌ ์๊ฐ ์๋ ค์ค",
"๊ตํต์ด ํผ์กํ๋ฐ 10km ๋ฐฐ๋ฌ ์ผ๋ง๋ ๊ฑธ๋ ค?"
]
for msg in test_messages:
print(f"\n์ฌ์ฉ์: {msg}")
response = bot.chat(msg)
print(f"๋ด: {response}")
๐ฏ LLM ํ์ฉ์ ์ฅ๋จ์
์ฅ์ โ
โข ์์ฐ์ค๋ฌ์ด ๋ํ ๊ฐ๋ฅ
โข ๋ณต์กํ ๋ฌธ๋งฅ ์ดํด
โข ๋ค์ํ ์ฃผ์ ๋์
โข ๋น ๋ฅธ ๊ฐ๋ฐ ์๋
โข ์ง์์ ์ธ ์ฑ๋ฅ ๊ฐ์
๋จ์ โ
โข API ๋น์ฉ ๋ฐ์
โข ์๋ต ์๊ฐ (๋คํธ์ํฌ ์ง์ฐ)
โข ํ ๋ฃจ์๋ค์ด์
(์๋ชป๋ ์ ๋ณด)
โข ๋ฐ์ดํฐ ํ๋ผ์ด๋ฒ์ ์ด์
โข ์ธํฐ๋ท ์ฐ๊ฒฐ ํ์
โ ๏ธ ๋น์ฉ ๊ด๋ฆฌ ํ
GPT API๋ ํ ํฐ ๋จ์๋ก ๊ณผ๊ธ๋ผ. ๋น์ฉ์ ์ ์ฝํ๋ ค๋ฉด:
โข ๋ํ ํ์คํ ๋ฆฌ ๊ธธ์ด ์ ํ
โข ์บ์ฑ ํ์ฉ (๊ฐ์ ์ง๋ฌธ์ ์ ์ฅ๋ ๋ต๋ณ ์ฌ์ฉ)
โข ๊ฐ๋จํ ์ง๋ฌธ์ ๊ท์น ๊ธฐ๋ฐ์ผ๋ก ์ฒ๋ฆฌ
โข ์ ์ฌ์ฉ๋ ๋ชจ๋ํฐ๋ง ๋ฐ ์๋ฆผ ์ค์
๐ผ ์ค์ ํ๋ก์ ํธ ์์ด๋์ด
์ด๋ก ์ ์ถฉ๋ถํ ๋ฐฐ์ ์ด! ์ด์ ์ค์ ํ๋ก์ ํธ๋ก ํฌํธํด๋ฆฌ์ค๋ฅผ ๋ง๋ค์ด๋ณด์. ์ฌ๋ฅ๋ท์์๋ ์ด๋ฐ ์ฑ๋ด ๊ฐ๋ฐ ์ฌ๋ฅ์ ๊ณต์ ํ ์ ์์ด! ๐ช
๐ 1. ์์ ์ฃผ๋ฌธ ์ฑ๋ด
๋์ด๋: โญโญโญ
๊ธฐ๋ฅ:
โข ๋ฉ๋ด ์ถ์ฒ ๋ฐ ์ฃผ๋ฌธ
โข ์ฅ๋ฐ๊ตฌ๋ ๊ด๋ฆฌ
โข ๊ฒฐ์ ์ฐ๋
โข ๋ฐฐ์ก ์ถ์
โข ๋ฆฌ๋ทฐ ์์ง
๊ธฐ์ ์คํ: Flask, SQLite, Stripe API, Kakao Map API
๐ฅ 2. ์๋ฃ ์๋ด ์ฑ๋ด
๋์ด๋: โญโญโญโญ
๊ธฐ๋ฅ:
โข ์ฆ์ ์ฒดํฌ
โข ๋ณ์ ์ถ์ฒ
โข ์์ฝ ๋์
โข ๊ฑด๊ฐ ์ ๋ณด ์ ๊ณต
โข ์๊ธ ์ํฉ ๋์
๊ธฐ์ ์คํ: Rasa, Medical NLP, PostgreSQL
์ฃผ์: ์๋ฃ ์ ๋ณด๋ ๋ฐ๋์ ์ ๋ฌธ๊ฐ ๊ฒ์ฆ ํ์!
๐ 3. ํ์ต ๋์ฐ๋ฏธ ์ฑ๋ด
๋์ด๋: โญโญโญโญ
๊ธฐ๋ฅ:
โข ์ง๋ฌธ ๋ต๋ณ
โข ๊ฐ๋
์ค๋ช
โข ํด์ฆ ์์ฑ
โข ํ์ต ์ง๋ ๊ด๋ฆฌ
โข ๋ง์ถค ํ์ต ์ถ์ฒ
๊ธฐ์ ์คํ: GPT-4, LangChain, Vector DB
๐ฐ 4. ๊ธ์ต ์๋ด ์ฑ๋ด
๋์ด๋: โญโญโญโญโญ
๊ธฐ๋ฅ:
โข ์์ฐ ๊ด๋ฆฌ
โข ํฌ์ ์กฐ์ธ
โข ๋์ถ ์๋ด
โข ํ์จ ์ ๋ณด
โข ๊ธ์ต ์ํ ๋น๊ต
๊ธฐ์ ์คํ: Django, Celery, Redis, Financial APIs
์ฃผ์: ๊ธ์ต ๊ท์ ์ค์ ํ์!
๐ฎ 5. ๊ฒ์ NPC ์ฑ๋ด
๋์ด๋: โญโญโญ
๊ธฐ๋ฅ:
โข ์บ๋ฆญํฐ๋ณ ์ฑ๊ฒฉ ๊ตฌํ
โข ํ์คํธ ์๋ด
โข ์คํ ๋ฆฌ ์งํ
โข ์์ดํ
๊ฑฐ๋
โข ํํธ ์ ๊ณต
๊ธฐ์ ์คํ: Unity, GPT API, WebSocket
๐ก ํ๋ก์ ํธ ์ฑ๊ณต ํ
โข ์๊ฒ ์์ํด์ ์ ์ง์ ์ผ๋ก ํ์ฅ
โข ์ฌ์ฉ์ ํผ๋๋ฐฑ์ ์ ๊ทน ๋ฐ์
โข ๋ฌธ์ํ๋ฅผ ์ฒ ์ ํ (README, API ๋ฌธ์)
โข GitHub์ ์คํ์์ค๋ก ๊ณต๊ฐ
โข ๋ธ๋ก๊ทธ์ ๊ฐ๋ฐ ๊ณผ์ ๊ธฐ๋ก
โข ์ฌ๋ฅ๋ท์์ ์ค์ ์๋ขฐ ๋ฐ์๋ณด๊ธฐ!
๐ฏ ๋ง๋ฌด๋ฆฌ: ์ฑ๋ด ๊ฐ๋ฐ์๋ก ์ฑ์ฅํ๊ธฐ
์! ์ฌ๊ธฐ๊น์ง ์ฝ์๋ค๋ฉด ์ด๋ฏธ ์ฑ๋ด ๊ฐ๋ฐ์ A to Z๋ฅผ ํ์ ๊ฑฐ์ผ. ์ถํํด! ๐
์ฑ๋ด ๊ฐ๋ฐ์ ๋จ์ํ ์ฝ๋๋ฅผ ์ง๋ ๊ฒ ์๋์ผ. ์ฌ์ฉ์๋ฅผ ์ดํดํ๊ณ , ๋ํ๋ฅผ ๋์์ธํ๊ณ , ๊ธฐ์ ์ ์ตํฉํ๋ ์ข
ํฉ ์์ ์ด์ง. ์ฒ์์ ์ด๋ ต๊ฒ ๋๊ปด์ง ์ ์์ง๋ง, ํ๋์ฉ ๋ง๋ค๋ค ๋ณด๋ฉด ์ด๋์ ์ ๋ฌธ๊ฐ๊ฐ ๋์ด ์์ ๊ฑฐ์ผ! ๐ช
๊ธฐ์ตํด! ์๋ฒฝํ ์ฑ๋ด์ ์์ด. ์ง์์ ์ผ๋ก ๊ฐ์ ํ๊ณ , ์ฌ์ฉ์ ํผ๋๋ฐฑ์ ๋ฃ๊ณ , ์๋ก์ด ๊ธฐ์ ์ ๋ฐฐ์ฐ๋ ๊ฒ ์ค์ํด. ๊ทธ๋ฆฌ๊ณ ํผ์ ํ๊ธฐ ํ๋ค ๋ ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ๋ค๋ฅธ ๊ฐ๋ฐ์๋ค๊ณผ ํ์
ํ๊ฑฐ๋ ๋ฉํ ๋ง์ ๋ฐ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ! ๐ค
์, ์ด์ ๋น์ ์ ์ฒซ ์ฑ๋ด์ ๋ง๋ค์ด๋ณผ ์๊ฐ์ด์ผ! ์์ ํ๋ก์ ํธ๋ถํฐ ์์ํด์, ์ ์ ๋ ๋ณต์กํ๊ณ ๋๋ํ ์ฑ๋ด์ผ๋ก ๋ฐ์ ์์ผ ๋๊ฐ๋ด. ์ธ์ ๊ฐ ๋น์ ์ด ๋ง๋ ์ฑ๋ด์ด ์๋ฐฑ๋ง ๋ช
์ ์ฌ์ฉ์์ ๋ํํ๋ ๋ ์ด ์ฌ ๊ฑฐ์ผ! ๐
ํ์ด์ ๋น์ด! ๊ทธ๋ฆฌ๊ณ ๊ถ๊ธํ ๊ฒ ์์ผ๋ฉด ์ธ์ ๋ ๋ฌผ์ด๋ด. ์ฐ๋ฆฌ ๋ชจ๋ ํจ๊ป ์ฑ์ฅํ๋ ๊ฐ๋ฐ์ ์ปค๋ฎค๋ํฐ๋๊น! ๐
๐ ์ฑ๋ด ๊ฐ๋ฐ, ์ด์ ์์์ด์ผ!
์์ ํ๋ก์ ํธ๋ถํฐ ์์ํด์
์ธ์์ ๋ฐ๊พธ๋ AI ์ฑ๋ด์ ๋ง๋ค์ด๋ณด์!
๐ป Happy Coding! ๐ค
์ด ๊ธ์ด ๋์์ด ๋์๋ค๋ฉด, ์ฌ๋ฅ๋ท์์ ๋ ๋ง์ ํ๋ก๊ทธ๋๋ฐ ์ง์์ ํํํด๋ณด์ธ์!
์ฑ๋ด ๊ฐ๋ฐ๋ถํฐ ์น/์ฑ ๊ฐ๋ฐ๊น์ง, ๋ค์ํ ์ฌ๋ฅ์ ๊ฐ์ง ์ ๋ฌธ๊ฐ๋ค์ด ๊ธฐ๋ค๋ฆฌ๊ณ ์์ด์. ๐
์ฌ๋ฅ๋ท์์ ๋น์ ์ ๊ฐ๋ฐ ์ฌ์ ์ ํจ๊ปํ์ธ์!
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.