2Slides Logo
Xây Dựng AI Agent Tạo Bài Thuyết Trình: Hướng Dẫn Đầy Đủ Cho Developer
2Slides Team
7 min read

Xây dựng AI Agent Tạo Bài Thuyết Trình: Hướng Dẫn Toàn Diện Cho Developer

AI agent đang chuyển đổi cách phần mềm tạo nội dung. Một AI agent tạo bài thuyết trình có thể tạo, tùy chỉnh và cung cấp các bộ slide chuyên nghiệp như một phần của quy trình làm việc tự động — từ pipeline CRM đến công cụ báo cáo nội bộ.

Hướng dẫn này sẽ đưa bạn qua quy trình xây dựng một presentation agent sẵn sàng cho production bằng cách sử dụng 2Slides API.

Tổng Quan Kiến Trúc

Một presentation agent thường tuân theo luồng này:

Yêu Cầu Người Dùng → Agent Logic → 2Slides API → Poll Job → Cung Cấp Kết Quả Context Gathering (dữ liệu, templates, brand assets)

Các Thành Phần Cốt Lõi

  1. Input Handler — nhận các yêu cầu tạo bài thuyết trình (văn bản, dữ liệu, file)
  2. Context Builder — làm phong phú yêu cầu với templates, thông tin thương hiệu, dữ liệu
  3. Generation Engine — gọi 2Slides API để tạo slides
  4. Status Monitor — theo dõi trạng thái job cho đến khi hoàn thành
  5. Delivery Layer — trả về link tải xuống, gửi qua email, đăng lên Slack

Bắt Đầu

1. Lấy API Key Của Bạn

Đăng ký tại 2slides.com và tạo API key tại 2slides.com/api.

2. Cài Đặt Các Thư Viện Phụ Thuộc

npm install node-fetch dotenv

3. Triển Khai Agent Cơ Bản

import fetch from 'node-fetch'; class PresentationAgent { constructor(apiKey) { this.apiKey = apiKey; this.baseUrl = 'https://2slides.com/api/v1'; } async generateSlides({ topic, themeId, language = 'en', resolution = '2K' }) { // Bước 1: Bắt đầu tạo const response = await fetch(`${this.baseUrl}/slides/generate`, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ userInput: topic, themeId, responseLanguage: language, resolution, mode: 'async' }) }); const job = await response.json(); if (!response.ok) throw new Error(job.error || 'Generation failed'); // Bước 2: Kiểm tra định kỳ cho đến khi hoàn thành return await this.waitForCompletion(job.jobId); } async waitForCompletion(jobId, maxWait = 300000) { const start = Date.now(); while (Date.now() - start < maxWait) { const response = await fetch(`${this.baseUrl}/jobs/${jobId}`, { headers: { 'Authorization': `Bearer ${this.apiKey}` } }); const status = await response.json(); if (status.status === 'success') return status; if (status.status === 'failed') throw new Error('Generation failed'); await new Promise(r => setTimeout(r, 3000)); } throw new Error('Generation timed out'); } async generateFromFile({ fileUrl, prompt, themeId }) { const response = await fetch(`${this.baseUrl}/slides/create-pdf-slides`, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ fileUrl, userInput: prompt, themeId, mode: 'async' }) }); const job = await response.json(); return await this.waitForCompletion(job.jobId); } async generateWithDesign({ topic, referenceImageUrl, resolution = '2K' }) { const response = await fetch(`${this.baseUrl}/slides/create-like-this`, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ userInput: topic, designStyle: { global: { referenceImageUrl } }, resolution, mode: 'async' }) }); const job = await response.json(); return await this.waitForCompletion(job.jobId); } async addNarration({ jobId, mode = 'single', voice = 'Charon' }) { const response = await fetch(`${this.baseUrl}/slides/generate-narration`, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ jobId, mode, voice, contentMode: 'concise' }) }); return await response.json(); } } // Cách sử dụng const agent = new PresentationAgent('sk-2slides-xxx'); const result = await agent.generateSlides({ topic: 'Đánh Giá Kinh Doanh Q1 2026', themeId: 'mckinsey-theme-id', resolution: '2K' }); console.log('Tải xuống:', result.downloadUrl);

Triển khai Python

import requests import time class PresentationAgent: def __init__(self, api_key): self.api_key = api_key self.base_url = 'https://2slides.com/api/v1' self.headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' } def generate_slides(self, topic, theme_id=None, language='en', resolution='2K'): response = requests.post( f'{self.base_url}/slides/generate', headers=self.headers, json={ 'userInput': topic, 'themeId': theme_id, 'responseLanguage': language, 'resolution': resolution, 'mode': 'async' } ) response.raise_for_status() job = response.json() return self._wait_for_completion(job['jobId']) def generate_from_file(self, file_url, prompt, theme_id=None): response = requests.post( f'{self.base_url}/slides/create-pdf-slides', headers=self.headers, json={ 'fileUrl': file_url, 'userInput': prompt, 'themeId': theme_id, 'mode': 'async' } ) response.raise_for_status() job = response.json() return self._wait_for_completion(job['jobId']) def _wait_for_completion(self, job_id, max_wait=300): start = time.time() while time.time() - start < max_wait: response = requests.get( f'{self.base_url}/jobs/{job_id}', headers=self.headers ) status = response.json() if status['status'] == 'success': return status if status['status'] == 'failed': raise Exception('Tạo bài thuyết trình thất bại') time.sleep(3) raise TimeoutError('Quá thời gian chờ tạo bài thuyết trình') # Cách sử dụng agent = PresentationAgent('sk-2slides-xxx') result = agent.generate_slides('Xu hướng AI 2026', resolution='4K') print(f"Tải xuống: {result['downloadUrl']}")

Các Mẫu Nâng Cao

Mẫu 1: Tạo Dựa Trên Template

Cấu hình trước các chủ đề cho các trường hợp sử dụng khác nhau:

const TEMPLATES = { 'pitch-deck': { themeId: 'apple-id', resolution: '2K' }, 'quarterly-review': { themeId: 'mckinsey-id', resolution: '2K' }, 'training': { themeId: 'corporate-id', resolution: '1K' }, 'social-media': { themeId: 'saul-bass-id', resolution: '4K', aspectRatio: '1:1' }, }; async function generateByType(type, topic) { const config = TEMPLATES[type]; return agent.generateSlides({ topic, ...config }); }

Mẫu 2: Tích Hợp Quy Trình Dữ Liệu

Kết nối với các nguồn dữ liệu của bạn:

async function weeklyReport(dataSource) { // 1. Lấy dữ liệu mới nhất const data = await dataSource.getWeeklyMetrics(); // 2. Định dạng làm đầu vào bài thuyết trình const topic = `Báo Cáo Tuần: Doanh Thu $${data.revenue}, Người Dùng ${data.users}, Tỷ Lệ Rời Bỏ ${data.churn}%`; // 3. Tạo slides const result = await agent.generateSlides({ topic, themeId: 'mckinsey-id' }); // 4. Phân phối await slackBot.postMessage('#team-updates', `Báo cáo tuần đã sẵn sàng: ${result.downloadUrl}`); }

Mẫu 3: Đầu Ra Đa Định Dạng

Tạo slides + thuyết minh + video trong một quy trình:

async function fullPresentation(topic) { // 1. Tạo slides const slides = await agent.generateSlides({ topic }); // 2. Thêm thuyết minh bằng giọng nói await agent.addNarration({ jobId: slides.jobId, mode: 'multi', voice: 'Charon' }); // 3. Tải xuống kèm âm thanh const download = await fetch(`${agent.baseUrl}/slides/download-slides-pages-voices`, { method: 'POST', headers: agent.headers, body: JSON.stringify({ jobId: slides.jobId }) }); return download.json(); }

Các Cân Nhắc Khi Triển Khai

Giới Hạn Tần Suất

  • Mặc định: 60 yêu cầu/phút
  • Xử lý phản hồi 429 với cơ chế backoff theo cấp số nhân
  • Sử dụng chế độ async cho các thao tác hàng loạt

Xử Lý Lỗi

  • Bọc tất cả các lời gọi API trong try/catch
  • Triển khai logic thử lại cho các lỗi tạm thời
  • Ghi log ID công việc để debug

Quản Lý Chi Phí

  • Theo dõi mức sử dụng credit cho mỗi lần tạo
  • Đặt cảnh báo ngân sách trong bảng điều khiển 2Slides của bạn
  • Sử dụng độ phân giải thấp hơn cho nội dung nội bộ/bản nháp

Bảo Mật

  • Lưu trữ API key trong biến môi trường, không bao giờ trong code
  • Chỉ sử dụng lời gọi API phía server — không bao giờ để lộ key cho client
  • Triển khai ký yêu cầu cho các webhook callback

Câu Hỏi Thường Gặp

Tôi có thể xây dựng sản phẩm SaaS dựa trên 2Slides API không?

Có — API được thiết kế cho việc sử dụng trong môi trường production. Không có hạn chế nào đối với các ứng dụng thương mại.

Giới hạn tạo đồng thời tối đa là bao nhiêu?

API xử lý các yêu cầu đồng thời thông qua giới hạn tần suất. Đối với nhu cầu khối lượng lớn, hãy liên hệ với đội ngũ 2Slides để được hỗ trợ giới hạn doanh nghiệp.

Có webhook cho việc hoàn thành công việc không?

Hiện tại, sử dụng polling thông qua endpoint jobs. Hỗ trợ webhook đang trong lộ trình phát triển.


Xây dựng agent tạo bài thuyết trình của bạn — lấy API key tại 2Slides và bắt đầu tạo slides một cách tự động.

About 2Slides

Create stunning AI-powered presentations in seconds. Transform your ideas into professional slides with 2slides AI Agent.

Try For Free