Vrei sa construiesti o aplicatie AI dar nu stii de unde sa incepi? Acest tutorial te duce de la zero la un chatbot AI functional, bazat pe web, in 30 de minute.
Ce construim
┌─────────────────────────────────────┐
│ 🤖 AI ChatBot │
├─────────────────────────────────────┤
│ │
│ You: What's the weather like? │
│ │
│ Bot: I don't have access to │
│ real-time weather data, but │
│ I can help with other things! │
│ │
│ You: Tell me a joke │
│ │
│ Bot: Why do programmers prefer │
│ dark mode? Because light │
│ attracts bugs! 🐛 │
│ │
│ [Type a message... ] [Send] │
└─────────────────────────────────────┘
Cerinte preliminare
- Python instalat (3.8+)
- Un cont OpenAI
- 30 de minute de timp
- Nu ai nevoie de experienta anterioara in AI!
Pasul 1: Configureaza proiectul (5 minute)
Creeaza folderul proiectului:
# Open terminal and run:
mkdir ai-chatbot
cd ai-chatbotCreeaza mediul virtual:
# Mac/Linux
python3 -m venv venv
source venv/bin/activate
# Windows
python -m venv venv
venv\Scripts\activateInstaleaza dependentele:
pip install flask openai python-dotenvCreeaza structura proiectului:
ai-chatbot/
├── app.py # Main application
├── templates/
│ └── index.html # Web interface
├── .env # API key (secret!)
└── requirements.txt # Dependencies
Pasul 2: Obtine cheia ta API OpenAI (3 minute)
- Mergi la https://platform.openai.com
- Inregistreaza-te sau autentifica-te
- Click pe "API keys" in bara laterala
- Creeaza o cheie noua
- Copiaz-o imediat!
Creeaza fisierul .env:
# .env - NEVER commit this to git!
OPENAI_API_KEY=sk-your-key-paste-hereCreeaza .gitignore:
# .gitignore
.env
venv/
__pycache__/Pasul 3: Construieste backend-ul (10 minute)
Creeaza app.py:
# app.py
from flask import Flask, render_template, request, jsonify
from openai import OpenAI
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize Flask app
app = Flask(__name__)
# Initialize OpenAI client
client = OpenAI()
# Store conversation history (in memory for now)
conversation_history = []
@app.route('/')
def home():
"""Render the chat interface."""
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
"""Handle chat messages."""
# Get message from request
data = request.json
user_message = data.get('message', '').strip()
if not user_message:
return jsonify({'error': 'No message provided'}), 400
# Add user message to history
conversation_history.append({
"role": "user",
"content": user_message
})
try:
# Create messages array with system prompt
messages = [
{
"role": "system",
"content": "You are a friendly and helpful AI assistant. Be concise but informative."
}
] + conversation_history
# Call OpenAI API
response = client.chat.completions.create(
model="gpt-4o-mini", # Cheap and fast!
messages=messages,
max_tokens=500
)
# Extract response
bot_message = response.choices[0].message.content
# Add to history
conversation_history.append({
"role": "assistant",
"content": bot_message
})
# Keep history manageable (last 20 messages)
if len(conversation_history) > 20:
conversation_history.pop(0)
conversation_history.pop(0)
return jsonify({'response': bot_message})
except Exception as e:
print(f"Error: {e}")
return jsonify({'error': 'Something went wrong. Please try again.'}), 500
@app.route('/clear', methods=['POST'])
def clear():
"""Clear conversation history."""
global conversation_history
conversation_history = []
return jsonify({'status': 'cleared'})
if __name__ == '__main__':
app.run(debug=True, port=5000)Pasul 4: Construieste frontend-ul (10 minute)
Creeaza templates/index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI ChatBot</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.chat-container {
width: 100%;
max-width: 600px;
background: white;
border-radius: 20px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
overflow: hidden;
}
.chat-header {
background: #667eea;
color: white;
padding: 20px;
text-align: center;
}
.chat-header h1 {
font-size: 1.5rem;
margin-bottom: 5px;
}
.chat-messages {
height: 400px;
overflow-y: auto;
padding: 20px;
background: #f5f5f5;
}
.message {
margin-bottom: 15px;
display: flex;
flex-direction: column;
}
.message.user {
align-items: flex-end;
}
.message.bot {
align-items: flex-start;
}
.message-content {
max-width: 80%;
padding: 12px 18px;
border-radius: 18px;
line-height: 1.5;
}
.message.user .message-content {
background: #667eea;
color: white;
border-bottom-right-radius: 4px;
}
.message.bot .message-content {
background: white;
color: #333;
border-bottom-left-radius: 4px;
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}
.message-label {
font-size: 0.75rem;
color: #888;
margin-bottom: 4px;
}
.chat-input {
display: flex;
padding: 20px;
background: white;
border-top: 1px solid #eee;
}
.chat-input input {
flex: 1;
padding: 12px 20px;
border: 2px solid #eee;
border-radius: 25px;
font-size: 1rem;
outline: none;
transition: border-color 0.3s;
}
.chat-input input:focus {
border-color: #667eea;
}
.chat-input button {
margin-left: 10px;
padding: 12px 25px;
background: #667eea;
color: white;
border: none;
border-radius: 25px;
font-size: 1rem;
cursor: pointer;
transition: background 0.3s;
}
.chat-input button:hover {
background: #5a6fd6;
}
.chat-input button:disabled {
background: #ccc;
cursor: not-allowed;
}
.typing-indicator {
display: none;
padding: 12px 18px;
background: white;
border-radius: 18px;
border-bottom-left-radius: 4px;
color: #888;
font-style: italic;
}
.typing-indicator.show {
display: inline-block;
}
.clear-btn {
background: #ff6b6b !important;
}
.clear-btn:hover {
background: #ee5a5a !important;
}
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-header">
<h1>🤖 AI ChatBot</h1>
<p>Powered by OpenAI</p>
</div>
<div class="chat-messages" id="chatMessages">
<div class="message bot">
<span class="message-label">Bot</span>
<div class="message-content">
Hello! I'm your AI assistant. How can I help you today?
</div>
</div>
</div>
<div class="chat-input">
<input type="text" id="messageInput" placeholder="Type a message..." autocomplete="off">
<button onclick="sendMessage()" id="sendBtn">Send</button>
<button onclick="clearChat()" class="clear-btn">Clear</button>
</div>
</div>
<script>
const messagesContainer = document.getElementById('chatMessages');
const messageInput = document.getElementById('messageInput');
const sendBtn = document.getElementById('sendBtn');
// Send on Enter key
messageInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
function addMessage(content, isUser) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${isUser ? 'user' : 'bot'}`;
messageDiv.innerHTML = `
<span class="message-label">${isUser ? 'You' : 'Bot'}</span>
<div class="message-content">${content}</div>
`;
messagesContainer.appendChild(messageDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
function showTyping() {
const typingDiv = document.createElement('div');
typingDiv.className = 'message bot';
typingDiv.id = 'typingIndicator';
typingDiv.innerHTML = `
<span class="message-label">Bot</span>
<div class="typing-indicator show">Thinking...</div>
`;
messagesContainer.appendChild(typingDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
function hideTyping() {
const typing = document.getElementById('typingIndicator');
if (typing) typing.remove();
}
async function sendMessage() {
const message = messageInput.value.trim();
if (!message) return;
// Add user message
addMessage(message, true);
messageInput.value = '';
// Disable input while processing
sendBtn.disabled = true;
messageInput.disabled = true;
// Show typing indicator
showTyping();
try {
const response = await fetch('/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message })
});
const data = await response.json();
hideTyping();
if (data.error) {
addMessage('Sorry, something went wrong. Please try again.', false);
} else {
addMessage(data.response, false);
}
} catch (error) {
hideTyping();
addMessage('Connection error. Please check your internet.', false);
}
// Re-enable input
sendBtn.disabled = false;
messageInput.disabled = false;
messageInput.focus();
}
async function clearChat() {
await fetch('/clear', { method: 'POST' });
messagesContainer.innerHTML = `
<div class="message bot">
<span class="message-label">Bot</span>
<div class="message-content">
Chat cleared! How can I help you?
</div>
</div>
`;
}
</script>
</body>
</html>Pasul 5: Ruleaza aplicatia! (2 minute)
# Make sure you're in the project folder with venv activated
python app.pyDeschide browser-ul: Mergi la http://localhost:5000
Ar trebui sa vezi chatbot-ul tau! Incearca sa scrii:
- "Hello!"
- "Tell me a joke"
- "What can you help me with?"
Felicitari!
Tocmai ai construit prima ta aplicatie AI!
Ce ai invatat
✅ Setting up Python virtual environment
✅ Using OpenAI API
✅ Building Flask web server
✅ Creating responsive chat interface
✅ Handling API requests/responses
✅ Managing conversation history
Ce urmeaza?
Imbunatatiri pe care le poti face:
- Adauga o baza de date - Stocheaza conversatiile permanent
- Conturi de utilizator - Permite utilizatorilor sa isi salveze chat-urile
- Streaming - Afiseaza raspunsurile cuvant cu cuvant
- Publica online - Pune aplicatia pe internet
Upgrade rapid - Adauga streaming:
# In app.py, add streaming endpoint
@app.route('/chat-stream', methods=['POST'])
def chat_stream():
def generate():
# ... setup code ...
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
yield f"data: {chunk.choices[0].delta.content}\n\n"
return Response(generate(), mimetype='text/event-stream')Depanare
Eroare "Module not found":
pip install flask openai python-dotenvEroare "Invalid API key":
- Verifica ca fisierul
.envcontine cheia corecta - Fara spatii in jurul semnului
= - Restarteaza serverul Flask
Raspunsuri goale:
- Verifica ca ai credit in contul OpenAI
- Incearca un prompt mai simplu mai intai
Fisierele complete de cod
requirements.txt:
flask==3.0.0
openai==1.12.0
python-dotenv==1.0.0
Ai nevoie de ajutor pentru a merge mai departe?
Construirea aplicatiilor AI pentru productie necesita mai mult decat tutoriale. Echipa noastra te poate ajuta cu:
- Dezvoltare full-stack de aplicatii AI
- Deployment si scalare
- Securitate si conformitate
- Solutii AI personalizate
Obtine ajutor pentru dezvoltare
Sistemul tau AI e conform cu EU AI Act? Evaluare gratuita de risc - afla in 2 minute →