Bạn đã từng xây một chatbot AI? Có thể bạn cũng biết đến blockchain và IPFS? Vậy tại sao không kết hợp cả hai để tạo ra một ứng dụng vừa thông minh, vừa phi tập trung?
Trong bài viết này, bạn sẽ được hướng dẫn:
Ứng dụng này là bước khởi đầu để bạn tiến vào thế giới của AI + Web3 – một hướng đi đang rất tiềm năng!
I. Kiến trúc tổng thể
User
Front-end (React)

GPT API
IPFS
Smart Contract
Quy trình:
II. Công cụ cần chuẩn bị
III. Bước 1: Tạo chatbot đơn giản với GPT
// /api/chat.js
import axios from 'axios';
export default async function handler(req, res) {
const { message } = req.body;
const reply = await axios.post('https://api.openai.com/v1/chat/completions', {
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: message }],
}, {
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
});
res.json({ answer: reply.data.choices[0].message.content });
}
Sau đó trong front-end: Gửi user input → hiển thị bot reply
IV. Bước 2: Lưu hội thoại lên IPFS
Cài đặt SDK từ Pinata:
npm install @pinata/sdk
// /utils/ipfs.js
import pinataSDK from '@pinata/sdk';
const pinata = pinataSDK('PINATA_API_KEY', 'PINATA_SECRET');
export async function saveToIPFS(conversation) {
const result = await pinata.pinJSONToIPFS(conversation);
return result.IpfsHash; // Đây là CID
}
Hội thoại sẽ có dạng:
{
"user": "Hello",
"bot": "Hi! How can I help you?"
}
V. Bước 3: Ghi CID vào blockchain
Viết một smart contract đơn giản:
// contracts/ChatStore.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ChatStore {
mapping(address => string[]) public chats;
function saveChat(string memory cid) public {
chats[msg.sender].push(cid);
}
function getChats(address user) public view returns (string[] memory) {
return chats[user];
}
}
Deploy bằng Hardhat:
npx hardhat run scripts/deploy.js --network goerli
VI. Bước 4: Kết nối front-end với smart contract
Sử dụng ethers.js để gọi hàm saveChat() từ front-end:
import { ethers } from "ethers";
import ChatStoreAbi from './abi/ChatStore.json';
const contractAddress = "YOUR_DEPLOYED_CONTRACT_ADDRESS";
export async function saveChatToBlockchain(cid) {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, ChatStoreAbi.abi, signer);
const tx = await contract.saveChat(cid);
await tx.wait();
}
VII. Trải nghiệm người dùng
VIII. Kết luận
Chỉ với kiến thức cơ bản về React, API, IPFS và Solidity, bạn đã có thể tạo ra một ứng dụng AI dApp đơn giản. Đây là ví dụ tuyệt vời cho:
Trong bài viết này, bạn sẽ được hướng dẫn:
- Tạo chatbot AI với OpenAI GPT hoặc mô hình mã nguồn mở
- Lưu trữ hội thoại lên IPFS
- Ghi lại địa chỉ dữ liệu (CID) vào smart contract trên blockchain
- Xây front-end đơn giản để tương tác
Ứng dụng này là bước khởi đầu để bạn tiến vào thế giới của AI + Web3 – một hướng đi đang rất tiềm năng!
I. Kiến trúc tổng thể
User


GPT API


Quy trình:
Người dùng nhập câu hỏi → gửi đến GPT → nhận câu trả lời
Lưu hội thoại vào IPFS → nhận CID
Ghi CID vào smart contract → có thể tra cứu lại sau
II. Công cụ cần chuẩn bị
Công cụ | Mục đích |
---|---|
Node.js | Chạy project React và server |
React (hoặc Next.js) | Giao diện người dùng |
OpenAI API (hoặc GPT-J) | Chatbot trả lời câu hỏi |
IPFS (qua Infura hoặc Pinata) | Lưu trữ dữ liệu hội thoại |
Solidity + Hardhat | Viết và deploy smart contract |
MetaMask | Kết nối ví Web3 |
III. Bước 1: Tạo chatbot đơn giản với GPT
// /api/chat.js
import axios from 'axios';
export default async function handler(req, res) {
const { message } = req.body;
const reply = await axios.post('https://api.openai.com/v1/chat/completions', {
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: message }],
}, {
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
});
res.json({ answer: reply.data.choices[0].message.content });
}

IV. Bước 2: Lưu hội thoại lên IPFS
Cài đặt SDK từ Pinata:
npm install @pinata/sdk
// /utils/ipfs.js
import pinataSDK from '@pinata/sdk';
const pinata = pinataSDK('PINATA_API_KEY', 'PINATA_SECRET');
export async function saveToIPFS(conversation) {
const result = await pinata.pinJSONToIPFS(conversation);
return result.IpfsHash; // Đây là CID
}
Hội thoại sẽ có dạng:
{
"user": "Hello",
"bot": "Hi! How can I help you?"
}
V. Bước 3: Ghi CID vào blockchain
Viết một smart contract đơn giản:
// contracts/ChatStore.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ChatStore {
mapping(address => string[]) public chats;
function saveChat(string memory cid) public {
chats[msg.sender].push(cid);
}
function getChats(address user) public view returns (string[] memory) {
return chats[user];
}
}
Deploy bằng Hardhat:
npx hardhat run scripts/deploy.js --network goerli
VI. Bước 4: Kết nối front-end với smart contract
Sử dụng ethers.js để gọi hàm saveChat() từ front-end:
import { ethers } from "ethers";
import ChatStoreAbi from './abi/ChatStore.json';
const contractAddress = "YOUR_DEPLOYED_CONTRACT_ADDRESS";
export async function saveChatToBlockchain(cid) {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, ChatStoreAbi.abi, signer);
const tx = await contract.saveChat(cid);
await tx.wait();
}
VII. Trải nghiệm người dùng
- Nhập câu hỏi → nhận câu trả lời từ GPT
- Nhấn “Lưu hội thoại” → gửi lên IPFS → nhận CID
- CID được lưu lên blockchain, gắn với ví người dùng
- Bạn có thể xem lại các cuộc hội thoại đã lưu bằng cách gọi getChats(address)
VIII. Kết luận
Chỉ với kiến thức cơ bản về React, API, IPFS và Solidity, bạn đã có thể tạo ra một ứng dụng AI dApp đơn giản. Đây là ví dụ tuyệt vời cho:
- Lưu trữ dữ liệu AI một cách phi tập trung
- Đảm bảo tính xác thực và minh bạch của nội dung AI
- Mở đường cho các hệ thống AI cộng đồng, không phụ thuộc Big Tech