Lý thuyết
Bài 1/6

Introduction to Generative AI & Prompt Engineering

Khám phá thế giới Generative AI và làm chủ nghệ thuật Prompt Engineering

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ểmAI Truyền thốngGenerative AI
Mục tiêuPhân loại, dự đoánTạo ra nội dung mới
OutputLabel, sốText, image, code
Ví dụSpam filter, house price predictionChatGPT, DALL-E, GitHub Copilot
TrainingSupervised learningSelf-supervised, Transformers

1.2 Các loại GenAI phổ biến

Text
1GenAI
2├── Text Generation (LLMs)
3│ ├── GPT-4, Claude, Gemini
4│ └── Use cases: Writing, coding, analysis
5├── Image Generation
6│ ├── DALL-E, Midjourney, Stable Diffusion
7│ └── Use cases: Art, design, marketing
8├── Code Generation
9│ ├── GitHub Copilot, Cursor, Codeium
10│ └── Use cases: Programming, debugging
11├── Audio/Music
12│ └── MusicGen, Suno
13└── Video
14 └── Runway, Pika

2. 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 Text

Key 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

ModelCompanyStrengthsUse Cases
GPT-4 TurboOpenAIVersatile, codingGeneral purpose, complex tasks
Claude 3.5 SonnetAnthropicLong context (200K), safeDocument analysis, research
Gemini 1.5 ProGoogleMultimodal, 2M contextVideo/image + text
Llama 3MetaOpen-source, customizableOn-premise deployment
Mistral LargeMistral AIMultilingual, efficientEuropean 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: Positive

Few-shot prompting:

Text
1Prompt:
2Câu: "Tôi yêu sản phẩm này" → Positive
3Câu: "Chất lượng tệ" → Negative
4Câu: "Giao hàng nhanh, nhưng đóng gói kém" → ?
5
6Output: Mixed

Chain 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 = 10k
92. Giá 3 cam = 12k → 1 cam = 4k → 4 cam = 16k
103. Tổng = 10k + 16k = 26k

4. Advanced Prompt Engineering

4.1 CLEAR Framework

Context - Limit - Example - Adjust - Role

Python
1# Example: Content writer
2prompt = """
3[Role] Bn là content writer chuyên nghip.
4
5[Context] Đang viết blog post v AI cho startup Vit Nam.
6
7[Limit] Đ dài: 300 t. Tone: Friendly, professional.
8
9[Example]
10Heading: "3 cách AI giúp startup tăng năng suất"
11- T đng hóa customer support
12- Phân tích d liu khách hàng
13- To content marketing
14
15[Adjust] Thêm s liu c th và case study Vit 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.95
7}

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 handling
7- Add comments explaining logic
8
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# Core
2import streamlit as st
3import openai
4from datetime import datetime
5import json
6
7# Features
8- Streamlit for UI
9- OpenAI API for LLM
10- JSON for conversation storage
11- Environment variables for API keys

6.3 Implementation

Python
1# app.py
2import streamlit as st
3from openai import OpenAI
4
5st.title("🤖 Personal AI Assistant")
6
7# Initialize OpenAI
8client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
9
10# Session state for conversation history
11if "messages" not in st.session_state:
12 st.session_state.messages = []
13
14# System prompt selector
15system_role = st.sidebar.selectbox(
16 "Choose AI Role:",
17 ["General Assistant", "Code Expert", "Writing Coach", "Data Analyst"]
18)
19
20system_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}
26
27# Display chat messages
28for message in st.session_state.messages:
29 with st.chat_message(message["role"]):
30 st.markdown(message["content"])
31
32# Chat input
33if prompt := st.chat_input("What would you like to know?"):
34 # Add user message
35 st.session_state.messages.append({"role": "user", "content": prompt})
36 with st.chat_message("user"):
37 st.markdown(prompt)
38
39 # Get AI response
40 with st.chat_message("assistant"):
41 messages = [
42 {"role": "system", "content": system_prompts[system_role]},
43 *st.session_state.messages
44 ]
45
46 stream = client.chat.completions.create(
47 model="gpt-4-turbo-preview",
48 messages=messages,
49 stream=True
50 )
51
52 response = st.write_stream(stream)
53
54 st.session_state.messages.append({"role": "assistant", "content": response})
55
56# Sidebar controls
57if st.sidebar.button("Clear Conversation"):
58 st.session_state.messages = []
59 st.rerun()
60
61# Export conversation
62if st.sidebar.button("Export Chat"):
63 chat_export = {
64 "timestamp": datetime.now().isoformat(),
65 "role": system_role,
66 "messages": st.session_state.messages
67 }
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 control
2temperature = st.sidebar.slider("Creativity", 0.0, 2.0, 0.7)
3
4# Token usage tracking
5total_tokens = sum(msg.get('tokens', 0) for msg in st.session_state.messages)
6st.sidebar.metric("Tokens Used", total_tokens)
7
8# Prompt templates
9template = 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 estimation
2def estimate_tokens(text):
3 return len(text) // 4 # Rough estimate
4
5# Use cheaper models for simple tasks
6if task_complexity == "simple":
7 model = "gpt-3.5-turbo"
8else:
9 model = "gpt-4-turbo-preview"
10
11# Limit context window
12max_history = 10
13messages = 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

  1. Bài 1: Tạo 5 prompts cho các role khác nhau (teacher, lawyer, chef, coach, consultant)
  2. Bài 2: Implement few-shot learning cho sentiment analysis tiếng Việt
  3. Bài 3: Build custom system prompt cho AI tutor dạy toán lớp 10
  4. Bài 4: Deploy Personal AI Assistant lên Streamlit Cloud
  5. Bài 5: Create prompt template library cho content marketing

Next: Bài 2 - LLM APIs & Integration