Cấp độ: intermediate 40 phútmin Miễn phí Free Product Developer Product Developer
AI Agent: Tích hợp OpenAI API AI Agent: Integrate OpenAI API
Chatbot song ngữ Việt–Anh đơn giản A simple bilingual Viet–English chatbot
Bạn sẽ học gì What you will learn
- ✓ Gọi OpenAI Chat Completions API Call OpenAI Chat Completions API
- ✓ Viết system prompt hiệu quả Write effective system prompts
- ✓ Xử lý lỗi và rate limit Handle errors and rate limits
Bạn sẽ tạo gì What you will build
Chatbot song ngữ Việt–Anh đơn giản A simple bilingual Viet–English chatbot
Code mẫu Sample code
javascript
import OpenAI from 'openai';
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
async function chat(userMessage) {
const response = await client.chat.completions.create({
model: 'gpt-4o-mini',
messages: [
{
role: 'system',
content: 'Bạn là trợ lý giáo dục song ngữ Việt–Anh. / You are a bilingual Viet–English education assistant.'
},
{ role: 'user', content: userMessage }
],
max_tokens: 500,
});
return response.choices[0].message.content;
}