Python client library for BuzuAI chat system via Socket.IO
Project description
BuzuAI Python Client
Thư viện Python client để giao tiếp với hệ thống AI của BuzuAI qua Socket.IO.
🚀 Tính năng
- ✅ Kết nối real-time qua Socket.IO
- 🔄 Singleton pattern - tái sử dụng kết nối
- 📝 Logging tự động với emoji
- ⏱️ Chờ response từ AI (timeout 300s)
- 🎯 API đơn giản, dễ sử dụng
- 🐍 Hỗ trợ Python 3.7+
- 📦 Chuẩn PyPI, dễ dàng cài đặt
📦 Cài đặt
pip install buzuai
Hoặc cài đặt từ source:
git clone https://github.com/Luisnguyen1/buzuai.git
cd buzuai
pip install -e .
🎯 Sử dụng cơ bản
Cách 1: Helper function (Đơn giản nhất)
from buzuai import send_message_to_buzuai
# Gửi tin nhắn và nhận response
response = send_message_to_buzuai(
user_id="user_123",
message_text="Xin chào, bạn khỏe không?",
language_code="vi"
)
if response:
print("AI trả lời:", response)
else:
print("Không nhận được response")
Cách 2: Sử dụng client singleton
from buzuai import get_buzuai_client
# Lấy client instance (singleton)
client = get_buzuai_client()
# Kết nối
if client.connect():
print("✅ Đã kết nối")
# Join room
client.join_room("user_123")
# Gửi tin nhắn và chờ response
response = client.send_message(
visitor_id="user_123",
text="Hôm nay thời tiết thế nào?",
language_code="vi"
)
if response:
print("💬 AI:", response)
# Ngắt kết nối
client.disconnect()
Cách 3: Tạo instance mới
from buzuai import BuzuAIClient
# Tạo client instance mới
client = BuzuAIClient()
# Kết nối và sử dụng
if client.connect():
response = client.send_message(
visitor_id="user_123",
text="Xin chào",
language_code="vi"
)
print(response)
client.disconnect()
Ví dụ đầy đủ với logging
from buzuai import get_buzuai_client
import logging
# Setup logging
logging.basicConfig(level=logging.INFO)
# Lấy client
client = get_buzuai_client()
# Kết nối
if not client.connect():
print("❌ Không thể kết nối")
exit(1)
print("✅ Đã kết nối thành công!")
# Gửi nhiều tin nhắn
questions = [
"Xin chào!",
"Bạn có thể giúp gì cho tôi?",
"Hôm nay thời tiết thế nào?",
]
for question in questions:
print(f"\n📤 Hỏi: {question}")
response = client.send_message(
visitor_id="user_123",
text=question,
language_code="vi"
)
if response:
# Response có thể chứa: text, type, content, etc.
text = response.get("text", response.get("content", ""))
print(f"💬 AI: {text}")
else:
print("⚠️ Không nhận được response")
# Ngắt kết nối
client.disconnect()
print("👋 Đã ngắt kết nối")
📖 API Reference
Helper Functions
send_message_to_buzuai(user_id, message_text, language_code="vi")
Helper function đơn giản nhất để gửi tin nhắn.
Tham số:
user_id(str): ID của usermessage_text(str): Nội dung tin nhắnlanguage_code(str): Mã ngôn ngữ (mặc định: "vi")
Returns: Dict[str, Any] hoặc None
Ví dụ:
response = send_message_to_buzuai("user_123", "Xin chào")
get_buzuai_client()
Lấy singleton instance của BuzuAI client.
Returns: BuzuAIClient
Ví dụ:
client = get_buzuai_client()
BuzuAIClient Class
Constructor
BuzuAIClient()
Không cần tham số. Sử dụng singleton pattern.
Methods
connect() -> bool
Kết nối đến BuzuAI server.
Returns: True nếu kết nối thành công
Ví dụ:
if client.connect():
print("Đã kết nối")
join_room(visitor_id, bot_id=None) -> bool
Join vào room chat với bot.
Tham số:
visitor_id(str): ID của userbot_id(str, optional): ID của bot (mặc định: DEFAULT_BOT_ID)
Returns: True nếu join thành công
Ví dụ:
client.join_room("user_123")
send_message(visitor_id, text, language_code="vi") -> Optional[Dict[str, Any]]
Gửi tin nhắn đến AI và chờ phản hồi.
Tham số:
visitor_id(str): ID của usertext(str): Nội dung tin nhắnlanguage_code(str): Mã ngôn ngữ (mặc định: "vi")
Returns: Dict chứa response từ AI hoặc None nếu timeout/lỗi
Ví dụ:
response = client.send_message("user_123", "Xin chào")
disconnect()
Ngắt kết nối khỏi server.
Ví dụ:
client.disconnect()
Properties
is_connected
Kiểm tra trạng thái kết nối.
Returns: bool
Ví dụ:
if client.is_connected:
print("Đang kết nối")
Constants
DEFAULT_BOT_ID: "4489b201-a08a-4d87-81e1-632bcbdb44a8"BUZUAI_URL: "https://api.buzuai.com/app-chat"NAMESPACE: "/app-chat"RESPONSE_TIMEOUT: 300 (seconds)
🔧 Cấu hình Logging
import logging
# Bật logging với level INFO
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Sử dụng client - logging sẽ tự động hoạt động
from buzuai import get_buzuai_client
client = get_buzuai_client()
🔍 Các sự kiện Socket.IO
Thư viện tự động xử lý các sự kiện sau:
Outgoing (Gửi đi):
joinRoom- Join vào room chatsendMessageToAI- Gửi tin nhắn đến AI
Incoming (Nhận về):
connect- Khi kết nối thành côngdisconnect- Khi bị ngắt kết nốireceiveMessage- Nhận response từ AIreceiveNotification- Nhận thông báo từ hệ thống
🐛 Troubleshooting
Không nhận được response
Nguyên nhân:
- Timeout (mặc định 300 giây)
- Bot đang bận
- Lỗi kết nối
Giải pháp:
response = client.send_message(user_id, "test")
if response is None:
print("Không nhận được response - thử lại")
Không kết nối được
Kiểm tra:
client = get_buzuai_client()
if not client.connect():
print("Lỗi kết nối - kiểm tra internet")
Bật debug logging
import logging
logging.basicConfig(level=logging.DEBUG)
� Ví dụ (Examples)
Thư viện đi kèm 3 file ví dụ:
1. example_simple.py - Đơn giản nhất
Sử dụng helper function để gửi 1 tin nhắn:
python example_simple.py
2. example_multiple.py - Gửi nhiều tin nhắn
Sử dụng client để gửi nhiều câu hỏi:
python example_multiple.py
3. example_interactive.py - Chế độ chat
Chat tương tác với AI như terminal chat:
python example_interactive.py
📋 Requirements
- Python >= 3.7
- python-socketio[client] >= 5.0.0
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🔗 Links
- GitHub: https://github.com/Luisnguyen1/buzuai
- Issues: https://github.com/Luisnguyen1/buzuai/issues
- Homepage: https://buzuai.com
📞 Support
Nếu bạn gặp vấn đề hoặc có câu hỏi:
- Mở issue trên GitHub: https://github.com/Luisnguyen1/buzuai/issues
- Email: support@buzuai.com
Version: 0.1.0
Python: 3.7+
Made with ❤️ by BuzuAI Team
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file buzuai-0.1.0.tar.gz.
File metadata
- Download URL: buzuai-0.1.0.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fde96fa69c42e91dae691f2d40f5b9214b99775fe920cfbe0b96b61f0c124d0e
|
|
| MD5 |
56643eb71f68f3a84b0880c76027f604
|
|
| BLAKE2b-256 |
614891ca938d26d1d4c9e0a0b58eae6a31f13bb695fc45ff80deb7b259e72f9c
|
File details
Details for the file buzuai-0.1.0-py3-none-any.whl.
File metadata
- Download URL: buzuai-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52c568c3a69258b10d3341e2b8267eadcb6831e451adda44d7b0464edf017295
|
|
| MD5 |
54e596c7cd5f96ddcb6bafb61d5396d4
|
|
| BLAKE2b-256 |
3904b884e306eac12bc483211cb70849c345bbeacf6e4ba766449ebaec197b3d
|