Chào mừng bạn đến với thế giới AI! Nếu bạn đang đọc bài viết này, có thể bạn đã nghe nói về các "công cụ" (tools) của AI và muốn hiểu cách khiến chúng hoạt động cùng nhau. Đừng lo — tôi sẽ giải thích mọi thứ từ đầu, không cần kiến thức lập trình trước đó.
🧠 Trước Khi Bắt Đầu: Tại Sao Cần Hiểu Hai Protocol Này?
Khi bạn sử dụng AI, đôi khi bạn cần AI thực hiện các tác vụ thực tế như tra cứu thời tiết, tìm kiếm thông tin, hoặc thực hiện phép tính. Đây là lúc "Tool Use" (sử dụng công cụ) phát huy tác dụng.
OpenAI và Anthropic là hai công ty tiên phong trong lĩnh vực AI, nhưng mỗi công ty có cách riêng để giao tiếp với các công cụ:
- OpenAI Tool Use: Dùng cấu trúc
functions— giống như khai báo một chiếc điều khiển tivi trước khi bấm nút - Anthropic MCP Protocol: Dùng cấu trúc
tools— giống như một "cổng kết nối" chuẩn hóa, cho phép nhiều ứng dụng trò chuyện với cùng một công cụ
💰 Tại Sao Nên Dùng HolySheep AI Cho Việc Này?
Trước khi đi vào code, tôi muốn chia sẻ lý do tôi chuyển sang HolySheep AI cho các dự án của mình:
- Tiết kiệm 85%+: Tỷ giá ¥1 = $1 — rẻ hơn nhiều so với các nhà cung cấp khác
- Thanh toán linh hoạt: Hỗ trợ WeChat, Alipay, Visa
- Tốc độ siêu nhanh: Độ trễ dưới 50ms
- Nhận tín dụng miễn phí: Ngay khi đăng ký
- Giá cả minh bạch: GPT-4.1 $8/MTok, Claude Sonnet 4.5 $15/MTok, Gemini 2.5 Flash $2.50/MTok, DeepSeek V3.2 chỉ $0.42/MTok
📚 Bài 1: OpenAI Tool Use — Cách Truyền Thống
OpenAI sử dụng tham số functions để định nghĩa công cụ. Đây là cách đơn giản nhất để bắt đầu.
Ví Dụ Đầu Tiên: Tra Cứu Thời Tiết
Hãy tưởng tượng bạn muốn hỏi AI: "Thời tiết ở Hà Nội thế nào?" — và AI thực sự tra cứu được thông tin thời tiết thực.
import requests
Định nghĩa công cụ (function) cho OpenAI
functions = [
{
"name": "get_weather",
"description": "Lấy thông tin thời tiết của một thành phố",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "Tên thành phố cần tra cứu thời tiết"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Đơn vị nhiệt độ"
}
},
"required": ["city"]
}
}
]
Gửi yêu cầu đến HolySheep AI
response = requests.post(
"https://api.holysheep.ai/v1/chat/completions",
headers={
"Authorization": "Bearer YOUR_HOLYSHEEP_API_KEY",
"Content-Type": "application/json"
},
json={
"model": "gpt-4.1",
"messages": [
{"role": "user", "content": "Thời tiết ở Hà Nội như thế nào?"}
],
"functions": functions,
"function_call": "auto"
}
)
print(response.json())
Kết quả mẫu bạn có thể nhận được:
{
"id": "chatcmpl-abc123",
"choices": [{
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_weather",
"arguments": "{\"city\": \"Hà Nội\", \"unit\": \"celsius\"}"
}
}
}]
}
Bước Tiếp Theo: Thực Thi Công Cụ
Sau khi nhận được yêu cầu gọi hàm từ AI, bạn cần thực thi và gửi kết quả trở lại:
# Hàm mô phỏng lấy thời tiết
def get_weather(city, unit="celsius"):
# Trong thực tế, đây sẽ là API thời tiết thật
weather_data = {
"Hà Nội": {"celsius": 28, "fahrenheit": 82},
"TP.HCM": {"celsius": 32, "fahrenheit": 90}
}
return weather_data.get(city, {}).get(unit, "Không có dữ liệu")
Parse kết quả từ AI
function_call = response.json()["choices"][0]["message"]["function_call"]
function_name = function_call["name"]
arguments = json.loads(function_call["arguments"])
Thực thi công cụ
weather_result = get_weather(
city=arguments["city"],
unit=arguments.get("unit", "celsius")
)
Gửi kết quả trở lại để AI tổng hợp câu trả lời
final_response = requests.post(
"https://api.holysheep.ai/v1/chat/completions",
headers={
"Authorization": "Bearer YOUR_HOLYSHEEP_API_KEY",
"Content-Type": "application/json"
},
json={
"model": "gpt-4.1",
"messages": [
{"role": "user", "content": "Thời tiết ở Hà Nội như thế nào?"},
{"role": "assistant", "content": None, "function_call": function_call},
{
"role": "function",
"name": function_name,
"content": json.dumps({"temperature": weather_result, "city": arguments["city"]})
}
]
}
)
print(final_response.json()["choices"][0]["message"]["content"])
Output: "Hà Nội hiện có nhiệt độ 28 độ C, trời nắng nhẹ..."
🔌 Bài 2: Anthropic MCP Protocol — Tiếp Cận Hiện Đại
MCP (Model Context Protocol) là một chuẩn mở cho phép AI kết nối với nhiều nguồn dữ liệu khác nhau. Đây là cách tiếp cận "cổng kết nối" — một cổng duy nhất có thể mở ra nhiều công cụ.
So Sánh Nhanh: OpenAI vs Anthropic MCP
| Tiêu chí | OpenAI Functions | Anthropic MCP |
|---|---|---|
| Định nghĩa công cụ | Trong request JSON | Qua server riêng biệt |
| Kết nối nhiều tools | Mỗi lần gọi 1 function | Nhiều tools cùng lúc |
| Quản lý state | Phức tạp hơn | Đơn giản qua MCP server |
Triển Khai MCP Client Với HolySheep AI
HolySheep AI hỗ trợ cả hai định dạng. Dưới đây là cách sử dụng Anthropic-style tools:
import requests
import json
Định nghĩa tools theo Anthropic MCP format
tools = [
{
"name": "web_search",
"description": "Tìm kiếm thông tin trên internet",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Từ khóa tìm kiếm"
},
"max_results": {
"type": "integer",
"description": "Số lượng kết quả tối đa",
"default": 5
}
},
"required": ["query"]
}
},
{
"name": "calculator",
"description": "Thực hiện phép tính toán học",
"input_schema": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "Biểu thức toán học, ví dụ: 2+2*3"
}
},
"required": ["expression"]
}
},
{
"name": "file_reader",
"description": "Đọc nội dung file văn bản",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Đường dẫn đến file"
}
},
"required": ["path"]
}
}
]
Gửi request với tools
response = requests.post(
"https://api.holysheep.ai/v1/chat/completions",
headers={
"Authorization": "Bearer YOUR_HOLYSHEEP_API_KEY",
"Content-Type": "application/json"
},
json={
"model": "claude-sonnet-4.5",
"messages": [
{
"role": "user",
"content": "Tính 15% của 5000 và tìm kiếm thông tin về AI năm 2026"
}
],
"tools": tools,
"tool_choice": "auto"
}
)
Xử lý kết quả
result = response.json()
print(json.dumps(result, indent=2, ensure_ascii=False))
🔄 Bài 3: Kết Hợp Cả Hai — Interoperability Thực Chiến
Trong dự án thực tế, bạn thường cần kết hợp cả hai. Dưới đây là một class Python hoàn chỉnh giúp bạn làm điều này dễ dàng.
import requests
import json
from typing import List, Dict, Any, Callable
class AIToolBridge:
"""Class kết nối AI với nhiều loại tools khác nhau"""
def __init__(self, api_key: str, base_url: str = "https://api.holysheep.ai/v1"):
self.api_key = api_key
self.base_url = base_url
self.tools_registry: Dict[str, Callable] = {}
self.conversation_history: List[Dict] = []
def register_tool(self, name: str, func: Callable, description: str):
"""Đăng ký một công cụ mới"""
self.tools_registry[name] = {
"function": func,
"description": description
}
print(f"✅ Đã đăng ký tool: {name}")
def convert_to_openai_format(self) -> List[Dict]:
"""Chuyển đổi tools sang format OpenAI"""
openai_tools = []
for name, tool_info in self.tools_registry.items():
openai_tools.append({
"type": "function",
"function": {
"name": name,
"description": tool_info["description"],
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
})
return openai_tools
def convert_to_anthropic_format(self) -> List[Dict]:
"""Chuyển đổi tools sang format Anthropic MCP"""
anthropic_tools = []
for name, tool_info in self.tools_registry.items():
anthropic_tools.append({
"name": name,
"description": tool_info["description"],
"input_schema": {
"type": "object",
"properties": {},
"required": []
}
})
return anthropic_tools
def chat(self, message: str, model: str = "claude-sonnet-4.5") -> str:
"""Gửi tin nhắn và nhận phản hồi từ AI"""
# Thêm tin nhắn vào lịch sử
self.conversation_history.append({"role": "user", "content": message})
# Chọn format phù hợp với model
if "claude" in model:
tools = self.convert_to_anthropic_format()
tool_param = "tools"
else:
tools = self.convert_to_openai_format()
tool_param = "functions"
# Gửi request
response = requests.post(
f"{self.base_url}/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": model,
"messages": self.conversation_history,
tool_param: tools
}
)
result = response.json()
# Kiểm tra xem có yêu cầu gọi tool không
if "choices" in result:
choice = result["choices"][0]["message"]
# Kiểm tra format của từng provider
if "tool_calls" in choice: # Anthropic format
tool_calls = choice["tool_calls"]
elif "function_call" in choice and choice["function_call"]: # OpenAI format
tool_calls = [choice["function_call"]]
else:
# Không có tool call, trả về nội dung trực tiếp
self.conversation_history.append({"role": "assistant", "content": choice.get("content", "")})
return choice.get("content", "Không có phản hồi.")
# Xử lý từng tool call
for tool_call in tool_calls:
if isinstance(tool_call, dict) and "function" in tool_call:
# Anthropic/OpenAI compatible format
tool_name = tool_call["function"]["name"]
tool_args = json.loads(tool_call["function"]["arguments"]) if tool_call["function"].get("arguments") else {}
else:
# OpenAI function_call format
tool_name = tool_call["name"]
tool_args = json.loads(tool_call["arguments"]) if tool_call.get("arguments") else {}
# Thực thi tool
if tool_name in self.tools_registry:
tool_result = self.tools_registry[tool_name]["function"](**tool_args)
# Thêm kết quả vào conversation
self.conversation_history.append({
"role": "assistant",
"content": None,
"tool_calls": [tool_call]
})
self.conversation_history.append({
"role": "tool",
"tool_call_id": tool_call.get("id", "call_" + tool_name),
"name": tool_name,
"content": json.dumps(tool_result)
})
# Gọi lại API để nhận phản hồi cuối cùng
final_response = requests.post(
f"{self.base_url}/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": model,
"messages": self.conversation_history
}
)
final_content = final_response.json()["choices"][0]["message"]["content"]
self.conversation_history.append({"role": "assistant", "content": final_content})
return final_content
============ SỬ DỤNG CLASS ============
Khởi tạo bridge
bridge = AIToolBridge(api_key="YOUR_HOLYSHEEP_API_KEY")
Đăng ký các tools
def search_google(query: str, max_results: int = 5):
"""Tìm kiếm Google (thay bằng API thật trong production)"""
return {
"results": [
{"title": f"Kết quả {i+1} cho '{query}'", "url": f"https://example.com/{i}"}
for i in range(min(max_results, 3))
]
}
def calculate(expression: str):
"""Tính toán biểu thức"""
try:
result = eval(expression) # Cẩn thận: chỉ dùng trong môi trường an toàn
return {"expression": expression, "result": result}
except Exception as e:
return {"error": str(e)}
bridge.register_tool("search", search_google, "Tìm kiếm thông tin trên Google")
bridge.register_tool("calculate", calculate, "Thực hiện phép tính toán học")
Trò chuyện với AI
print("🤖 Chat với AI (Claude):")
print(bridge.chat("Tính 25 * 4 + 100 = ?", model="claude-sonnet-4.5"))
print("\n" + "="*50 + "\n")
print("🤖 Chat với AI (GPT):")
bridge.conversation_history = [] # Reset
print(bridge.chat("Tính 50 + 50 bằng bao nhiêu?", model="gpt-4.1"))
⚡ Bài 4: Demo Thực Chiến — Ứng Dụng Quản Lý Công Việc
Hãy xem một ứng dụng thực tế kết hợp cả hai protocol để quản lý công việc:
import requests
import json
from datetime import datetime
class TaskManager:
"""Ứng dụng quản lý công việc với AI"""
def __init__(self):
self.tasks = []
self.api_key = "YOUR_HOLYSHEEP_API_KEY"
def add_task(self, title: str, priority: str = "medium", due_date: str = None):
"""Thêm công việc mới"""
task = {
"id": len(self.tasks) + 1,
"title": title,
"priority": priority,
"due_date": due_date,
"status": "pending",
"created_at": datetime.now().isoformat()
}
self.tasks.append(task)
return f"Đã thêm công việc: {title}"
def list_tasks(self, status: str = None):
"""Liệt kê công việc"""
if status:
filtered = [t for t in self.tasks if t["status"] == status]
else:
filtered = self.tasks
return filtered
def complete_task(self, task_id: int):
"""Đánh dấu hoàn thành"""
for task in self.tasks:
if task["id"] == task_id:
task["status"] = "completed"
return f"Đã hoàn thành: {task['title']}"
return "Không tìm thấy công việc"
Định nghĩa tools cho OpenAI
openai_functions = [
{
"type": "function",
"function": {
"name": "add_task",
"description": "Thêm công việc mới vào danh sách",
"parameters": {
"type": "object",
"properties": {
"title": {"type": "string", "description": "Tiêu đề công việc"},
"priority": {"type": "string", "enum": ["high", "medium", "low"]},
"due_date": {"type": "string", "description": "Hạn chót (YYYY-MM-DD)"}
},
"required": ["title"]
}
}
},
{
"type": "function",
"function": {
"name": "list_tasks",
"description": "Xem danh sách công việc",
"parameters": {
"type": "object",
"properties": {
"status": {"type": "string", "enum": ["pending", "completed"]}
}
}
}
}
]
Khởi tạo TaskManager
manager = TaskManager()
Demo: Thêm công việc
print(manager.add_task("Hoàn thành báo cáo", "high", "2026-01-15"))
print(manager.add_task("Họp team", "medium", "2026-01-10"))
print(manager.add_task("Review code", "low"))
Demo: AI nhận diện intent và gọi function
def process_user_intent(user_message: str):
"""Xử lý yêu cầu từ người dùng"""
# Gọi AI để phân tích intent
response = requests.post(
"https://api.holysheep.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {manager.api_key}",
"Content-Type": "application/json"
},
json={
"model": "gpt-4.1",
"messages": [
{
"role": "system",
"content": """Bạn là trợ lý quản lý công việc. Phân tích yêu cầu và gọi function phù hợp.
Nếu người dùng muốn thêm việc -> gọi add_task
Nếu muốn xem danh sách -> gọi list_tasks
Nếu muốn đánh dấu hoàn thành -> gọi complete_task"""
},
{"role": "user", "content": user_message}
],
"functions": openai_functions
}
)
result = response.json()
print(f"AI Response: {json.dumps(result, indent=2, ensure_ascii=False)}")
return result
Test
print("\n--- Test 1: Thêm công việc ---")
process_user_intent("Thêm giúp tôi việc mua đồ ăn trưa")
print("\n--- Test 2: Xem danh sách ---")
process_user_intent("Cho tôi xem các công việc cần làm")
print("\n--- Danh sách hiện tại ---")
print(json.dumps(manager.list_tasks(), indent=2, ensure_ascii=False))
🛠️ Triển Khai Thực Tế: Weather Bot Hoàn Chỉnh
Đây là một bot thời tiết hoàn chỉnh mà bạn có thể sao chép và chạy ngay:
dict:
"""
Lấy thông tin thời tiết (demo - thay bằng API thật)
"""
# Database thời tiết mẫu
weather_db = {
"hà nội": {"temp": 28, "condition": "Nắng", "humidity": 75},
"tp.hcm": {"temp": 32, "condition": "Nắng nóng", "humidity": 80},
"đà nẵng": {"temp": 30, "condition": "Mưa rào", "humidity": 85},
"london": {"temp": 15, "condition": "Mây mù", "humidity": 70},
"new york": {"temp": 20, "condition": "Nắng", "humidity": 55},
}
city_lower = city.lower().strip()
if city_lower in weather_db:
data = weather_db[city_lower]
temp = data["temp"]
if unit == "fahrenheit":
temp = temp * 9/5 + 32
return {
"city": city,
"temperature": temp,
"unit": unit,
"condition": data["condition"],
"humidity": data["humidity"]
}
return {"error": f"Không có dữ liệu cho {city}"}
def chat(self, message: str, model: str = "claude-sonnet-4.5") -> str:
"""Gửi tin nhắn và nhận phản hồi từ AI"""
# Định nghĩa tools cho cả hai format
tools_openai = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Lấy thông tin thời tiết của một thành phố",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "Tên thành phố"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
}
]
tools_anthropic = [
{
"name": "get_weather",
"description": "Lấy thông tin thời tiết của một thành phố",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
]
# Chọn format phù hợp
if "claude" in model:
tools = tools_anthropic
tools_param = "tools"
else:
tools = tools_openai
tools_param = "functions"
# Thêm tin nhắn user
self.conversation.append({"role": "user", "content": message})
# Lần gọi đầu tiên - AI sẽ quyết định có gọi tool không
response = requests.post(
f"{self.base_url}/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": model,
"messages": self.conversation,
tools_param: tools,
"tool_choice": "auto"
}
)
result = response.json()
# Kiểm tra tool calls
choice = result["choices"][0]["message"]
# Xử lý response format khác nhau
if "tool_calls" in choice:
# Anthropic format
for tool_call in choice["tool_calls"]:
func_name = tool_call["function"]["name"]
func_args = json.loads(tool_call["function"]["arguments"])
elif "function_call" in choice and choice["function_call"]:
# OpenAI format (legacy)
func_name = choice["function_call"]["name"]
func_args = json.loads(choice["function_call"]["arguments"])
else:
# Không có tool call
self.conversation.append({"role": "assistant", "content": choice.get("content", "")})
return choice.get("content", "")
# Thực thi tool
tool_result = self.get_weather(**func_args)
# Thêm vào conversation
self.conversation.append({
"role": "assistant",
"content": None,
"tool_calls": [choice.get("tool_calls", [{}])[0]] if "tool_calls" in choice else None,
"function_call": choice.get("function_call") if "function_call" in choice else None
})
tool_message = {
"role": "tool",
"content": json.dumps(tool_result)
}
# Anthropic format cần tool_call_id
if "tool_calls" in choice and choice["tool_calls"]:
tool_message["tool_call_id"] = choice["tool_calls"][0].get("id")
tool_message["name"] = func_name
self.conversation.append(tool_message)
# Gọi lại để nhận phản hồi cuối cùng
final_response = requests.post(
f"{self.base_url}/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json={
"model": model,
"messages": self.conversation
}
)
final_content = final_response.json()["choices"][0]["message"]["content"]
self.conversation.append({"role": "assistant", "content": final_content})
return final_content
============ CHẠY DEMO ============
if __name__ == "__main__":
print("🌤️ Weather Bot Demo")
print("=" * 50)
bot = WeatherBot(api_key="YOUR_HOLYSHEEP_API_KEY")
# Test với Claude
print("\n🤖 Model: Claude Sonnet 4.5")
print("-" * 30)
response = bot.chat("Thời tiết ở Hà Nội thế nào?")
print(f"User: Thời tiết ở Hà Nội thế nào?")
print(f"Bot: {response}")
# Test với GPT
print("\n🤖 Model: GPT-4.1")
print("-" * 30)
bot.conversation = [] # Reset
response = bot.chat("Cho tôi biết thời tiết ở London")
print(f"User: Cho tôi biết thời tiết ở London")
print(f"Bot: {response}")
# Test đơn vị Fahrenheit
print("\n🤖 Model: Claude Sonnet 4.5")
print("-" * 30)
response = bot.chat("Thời tiết New York theo Fahrenheit")
print(f"User: Thời tiết New York theo Fahrenheit")
print(f"Bot: {response}")
Lỗi Thường Gặp và Cách Khắc Phục
Qua quá trình làm việc với cả hai protocol, tôi đã gặp nhiều lỗi phổ biến. Dưới đây là cách giải quyết:
1. Lỗi 401 Unauthorized — Sai API Key
# ❌ SAI: Copy paste sai hoặc có khoảng trắng thừa
headers = {"Authorization": "Bearer YOUR_HOLYSHEEP