Hệ thống FastAPI đơn giản để quản lý và gọi các service agent với input/output: text, file (URL), image (URL).
- Input đơn giản: text, file (URL), image (URL)
- Output đơn giản: text, file (URL), image (URL)
- Auto-discovery: Tự động phát hiện và đăng ký agents mới
- Modular: Dễ dàng thêm agent mới
FastAPI/
├── app/
│ ├── main.py # Entry point của FastAPI
│ ├── api/
│ │ ├── router.py # Router chính
│ │ └── v1/
│ │ └── agents.py # API endpoints cho agents
│ ├── services/
│ │ ├── base.py # BaseAgentService abstract class
│ │ ├── registry.py # Auto-discovery và đăng ký agents
│ │ ├── agent_a.py # LLM Agent
│ │ ├── agent_b.py # Vision Agent
│ │ └── file_agent.py # File Processing Agent
│ ├── models/
│ │ └── schemas.py # Pydantic models cho input/output
│ └── utils/
│ └── file_utils.py # Utilities cho file operations
├── requirements.txt
├── test_api.py
└── README.md
pip install -r requirements.txtuvicorn app.main:app --reloadServer sẽ chạy tại: http://localhost:8000
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
{
"text": "text content (optional)",
"file": "https://example.com/file.pdf (optional)",
"image": "https://example.com/image.jpg (optional)"
}{
"text": "response text (optional)",
"file": "https://example.com/output.pdf (optional)",
"image": "https://example.com/output.jpg (optional)"
}- Input: text
- Output: text
- Mô tả: Gọi Large Language Model để xử lý text
- Input: image (URL)
- Output: text (mô tả hình ảnh)
- Mô tả: Phân tích và mô tả hình ảnh
- Input: file (URL)
- Output: text (phân tích file)
- Mô tả: Đọc và phân tích nội dung file
Liệt kê tất cả agents có sẵn
Xử lý request với agent chỉ định
Lấy thông tin chi tiết về agent
curl -X POST "http://localhost:8000/api/v1/agents/llm_agent/process" \
-H "Content-Type: application/json" \
-d '{
"text": "Viết một câu chuyện ngắn về AI"
}'curl -X POST "http://localhost:8000/api/v1/agents/vision_agent/process" \
-H "Content-Type: application/json" \
-d '{
"image": "https://example.com/image.jpg"
}'curl -X POST "http://localhost:8000/api/v1/agents/file_processing_agent/process" \
-H "Content-Type: application/json" \
-d '{
"file": "https://example.com/document.pdf"
}'Chạy script test:
python test_api.py- Tạo file mới trong
app/services/(ví dụ:my_agent.py) - Kế thừa từ
BaseAgentService - Implement method
process() - Agent sẽ tự động được đăng ký khi restart server
Ví dụ:
from .base import BaseAgentService
from app.models.schemas import AgentRequest, AgentResponse
class MyAgent(BaseAgentService):
name = "my_agent"
description = "Mô tả agent của tôi"
async def process(self, request: AgentRequest) -> AgentResponse:
# Xử lý input
if request.text:
result_text = f"Đã xử lý text: {request.text}"
return AgentResponse(text=result_text)
return AgentResponse(text="Không có input để xử lý")- Dễ sử dụng: Request/Response chỉ có 3 trường: text, file, image
- Linh hoạt: Mỗi agent tự quyết định xử lý input nào và trả về output gì
- Mở rộng dễ dàng: Thêm agent mới chỉ cần 1 file Python
- Tự động: Auto-discovery agents không cần config thủ công