Introduction to Generative AI & Prompt Engineering
1. Generative AI là gì?
Generative AI (GenAI) là các mô hình AI có khả năng tạo ra nội dung mới: văn bản, hình ảnh, code, âm thanh, video từ input đơn giản.
1.1 Phân biệt AI truyền thống vs GenAI
| Đặc điểm | AI Truyền thống | Generative AI |
|---|---|---|
| Mục tiêu | Phân loại, dự đoán | Tạo ra nội dung mới |
| Output | Label, số | Text, image, code |
| Ví dụ | Spam filter, house price prediction | ChatGPT, DALL-E, GitHub Copilot |
| Training | Supervised learning | Self-supervised, Transformers |
1.2 Các loại GenAI phổ biến
Text
1GenAI2├── Text Generation (LLMs)3│ ├── GPT-4, Claude, Gemini4│ └── Use cases: Writing, coding, analysis5├── Image Generation6│ ├── DALL-E, Midjourney, Stable Diffusion7│ └── Use cases: Art, design, marketing8├── Code Generation9│ ├── GitHub Copilot, Cursor, Codeium10│ └── Use cases: Programming, debugging11├── Audio/Music12│ └── MusicGen, Suno13└── Video14 └── Runway, Pika2. Large Language Models (LLMs)
2.1 LLM là gì?
LLM (Large Language Model) là mô hình AI được training trên hàng tỷ từ để:
- Hiểu ngôn ngữ tự nhiên
- Tạo ra text mạch lạc
- Reasoning và suy luận
- Thực hiện tasks phức tạp
2.2 Kiến trúc Transformer
LLMs hiện đại đều dựa trên Transformer architecture:
Text
1Input Text → Tokenization → Embeddings → 2Multi-Head Attention → Feed Forward → 3Layer Normalization → Output Probabilities → Generated TextKey components:
- Self-Attention: Model học mối quan hệ giữa các từ
- Positional Encoding: Hiểu vị trí của từ trong câu
- Layer Stacking: Nhiều layers học patterns phức tạp
2.3 Các LLMs phổ biến 2026
| Model | Company | Strengths | Use Cases |
|---|---|---|---|
| GPT-4 Turbo | OpenAI | Versatile, coding | General purpose, complex tasks |
| Claude 3.5 Sonnet | Anthropic | Long context (200K), safe | Document analysis, research |
| Gemini 1.5 Pro | Multimodal, 2M context | Video/image + text | |
| Llama 3 | Meta | Open-source, customizable | On-premise deployment |
| Mistral Large | Mistral AI | Multilingual, efficient | European languages |
3. Prompt Engineering Cơ Bản
3.1 Prompt là gì?
Prompt = Input text bạn đưa cho LLM để nhận output mong muốn.
Anatomy of a good prompt:
Text
1[Instruction] + [Context] + [Input Data] + [Output Format]3.2 Basic Prompting Techniques
Zero-shot prompting:
Text
1Prompt: Phân tích cảm xúc câu sau: "Sản phẩm này thật tuyệt vời!"2 3Output: PositiveFew-shot prompting:
Text
1Prompt:2Câu: "Tôi yêu sản phẩm này" → Positive3Câu: "Chất lượng tệ" → Negative4Câu: "Giao hàng nhanh, nhưng đóng gói kém" → ?5 6Output: MixedChain of Thought (CoT):
Text
1Prompt: Hãy giải bài toán sau từng bước:2Nếu 1 quả táo giá 5k, 3 quả cam giá 12k, 3mua 2 táo + 4 cam hết bao nhiêu?4 5Hãy reasoning step by step.6 7Output:81. Giá 1 táo = 5k → 2 táo = 10k92. Giá 3 cam = 12k → 1 cam = 4k → 4 cam = 16k103. Tổng = 10k + 16k = 26k4. Advanced Prompt Engineering
4.1 CLEAR Framework
Context - Limit - Example - Adjust - Role
Python
1# Example: Content writer2prompt = """3[Role] Bạn là content writer chuyên nghiệp.45[Context] Đang viết blog post về AI cho startup Việt Nam.67[Limit] Độ dài: 300 từ. Tone: Friendly, professional.89[Example] 10Heading: "3 cách AI giúp startup tăng năng suất"11- Tự động hóa customer support12- Phân tích dữ liệu khách hàng13- Tạo content marketing1415[Adjust] Thêm số liệu cụ thể và case study Việt Nam nếu có.16"""4.2 Role Prompting
Text
1Prompt: Act as a [ROLE] with [EXPERTISE].2 3Examples:4- "Act as a senior data scientist with 10 years experience in healthcare analytics..."5- "Act as a creative director at a top advertising agency..."6- "Act as a Python expert specializing in data engineering..."4.3 Output Format Control
JSON
1Prompt: Trả lời theo format JSON:2{3 "summary": "Tóm tắt ngắn gọn",4 "key_points": ["điểm 1", "điểm 2"],5 "sentiment": "positive/negative/neutral",6 "confidence": 0.957}5. Prompt Patterns & Templates
5.1 Template Library
Analysis Template:
Text
1Analyze the following [TYPE] and provide:21. Summary (2-3 sentences)32. Key insights (bullet points)43. Recommendations (actionable steps)54. Risks/Concerns (if any)6 7[DATA/TEXT]:8...Creative Writing Template:
Text
1Write a [FORMAT] about [TOPIC] that:2- Target audience: [WHO]3- Tone: [TONE]4- Length: [WORDS]5- Must include: [REQUIREMENTS]6- Avoid: [RESTRICTIONS]Code Generation Template:
Text
1Write [LANGUAGE] code to [TASK].2 3Requirements:4- Use [LIBRARY/FRAMEWORK]5- Handle [EDGE CASES]6- Include error handling7- Add comments explaining logic8 9Example input: ...10Expected output: ...6. Dự án: Personal AI Assistant
6.1 Mô tả dự án
Xây dựng Personal AI Assistant với Streamlit:
- Chat interface với GPT-4
- Multi-turn conversations
- Context management
- Custom system prompts
- Save/load conversations
6.2 Tech Stack
Python
1# Core2import streamlit as st3import openai4from datetime import datetime5import json67# Features8- Streamlit for UI9- OpenAI API for LLM10- JSON for conversation storage11- Environment variables for API keys6.3 Implementation
Python
1# app.py2import streamlit as st3from openai import OpenAI45st.title("🤖 Personal AI Assistant")67# Initialize OpenAI8client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])910# Session state for conversation history11if "messages" not in st.session_state:12 st.session_state.messages = []1314# System prompt selector15system_role = st.sidebar.selectbox(16 "Choose AI Role:",17 ["General Assistant", "Code Expert", "Writing Coach", "Data Analyst"]18)1920system_prompts = {21 "General Assistant": "You are a helpful AI assistant.",22 "Code Expert": "You are an expert programmer. Provide clean, efficient code with explanations.",23 "Writing Coach": "You are a professional writing coach. Help improve writing clarity and style.",24 "Data Analyst": "You are a data analyst. Provide insights and data-driven recommendations."25}2627# Display chat messages28for message in st.session_state.messages:29 with st.chat_message(message["role"]):30 st.markdown(message["content"])3132# Chat input33if prompt := st.chat_input("What would you like to know?"):34 # Add user message35 st.session_state.messages.append({"role": "user", "content": prompt})36 with st.chat_message("user"):37 st.markdown(prompt)38 39 # Get AI response40 with st.chat_message("assistant"):41 messages = [42 {"role": "system", "content": system_prompts[system_role]},43 *st.session_state.messages44 ]45 46 stream = client.chat.completions.create(47 model="gpt-4-turbo-preview",48 messages=messages,49 stream=True50 )51 52 response = st.write_stream(stream)53 54 st.session_state.messages.append({"role": "assistant", "content": response})5556# Sidebar controls57if st.sidebar.button("Clear Conversation"):58 st.session_state.messages = []59 st.rerun()6061# Export conversation62if st.sidebar.button("Export Chat"):63 chat_export = {64 "timestamp": datetime.now().isoformat(),65 "role": system_role,66 "messages": st.session_state.messages67 }68 st.sidebar.download_button(69 "Download JSON",70 data=json.dumps(chat_export, indent=2),71 file_name=f"chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"72 )6.4 Enhanced Features
Python
1# Temperature control2temperature = st.sidebar.slider("Creativity", 0.0, 2.0, 0.7)34# Token usage tracking5total_tokens = sum(msg.get('tokens', 0) for msg in st.session_state.messages)6st.sidebar.metric("Tokens Used", total_tokens)78# Prompt templates9template = st.sidebar.selectbox("Quick Prompts:", [10 "Summarize this text",11 "Explain like I'm 5",12 "Generate code for",13 "Translate to Vietnamese"14])7. Best Practices
7.1 Prompt Optimization
✅ DO:
- Be specific and clear
- Provide context
- Use examples
- Iterate and refine
- Test with edge cases
❌ DON'T:
- Be vague or ambiguous
- Assume too much knowledge
- Use conflicting instructions
- Ignore output format
- Skip validation
7.2 Cost Optimization
Python
1# Token estimation2def estimate_tokens(text):3 return len(text) // 4 # Rough estimate45# Use cheaper models for simple tasks6if task_complexity == "simple":7 model = "gpt-3.5-turbo"8else:9 model = "gpt-4-turbo-preview"1011# Limit context window12max_history = 1013messages = messages[-max_history:]8. Tài liệu tham khảo
8.1 Official Docs
8.2 Advanced Resources
- Papers: "Chain-of-Thought Prompting", "ReAct: Synergizing Reasoning and Acting"
- Courses: DeepLearning.AI "ChatGPT Prompt Engineering"
- Communities: r/PromptEngineering, LangChain Discord
Bài tập
- Bài 1: Tạo 5 prompts cho các role khác nhau (teacher, lawyer, chef, coach, consultant)
- Bài 2: Implement few-shot learning cho sentiment analysis tiếng Việt
- Bài 3: Build custom system prompt cho AI tutor dạy toán lớp 10
- Bài 4: Deploy Personal AI Assistant lên Streamlit Cloud
- Bài 5: Create prompt template library cho content marketing
