Web Agent là ứng dụng tìm kiếm web dạng chat. Hệ thống ưu tiên Tavily để lấy dữ liệu web, có thể fallback sang SearXNG khi Tavily không khả dụng hoặc chất lượng nguồn chưa đủ tốt. Câu trả lời cuối cùng được tổng hợp bằng LLM OpenAI-compatible và hiển thị trong giao diện Next.js.
Repository: https://github.com/baolnq-ai/web-agent
- Chat UI giống trợ lý tìm kiếm: nhập câu hỏi, xem câu trả lời, nguồn, confidence và lịch sử phiên.
- Tavily-first retrieval, fallback SearXNG, query expansion, evidence merge và quality gate.
- LLM final summarizer hỗ trợ runtime config: base URL, model, temperature, max tokens, prompt và target output length theo token.
- Prompt Manager cho phép cấu hình prompt tổng hợp cuối cùng mà không cần sửa code.
- Session history theo từng phiên chat, hỗ trợ local JSON hoặc PostgreSQL.
- Streaming qua SSE để frontend nhận trạng thái pipeline và nội dung trả lời.
- Ops Dashboard để kiểm tra Tavily key, LLM health/test, audit logs và trạng thái vận hành.
- Script chạy đa nền tảng: Bash cho Linux/macOS/Git Bash, PowerShell cho Windows.
- CI GitHub Actions chạy backend tests, frontend lint và frontend build.
User
-> Next.js Chat UI
-> FastAPI /api/v1/search/stream
-> Context Query Rewriter
-> Query Analyst
-> Query Planner
-> Multi-query Retrieval
-> Tavily first
-> SearXNG fallback
-> Evidence Merge
-> Quality Gate
-> LLM Final Summary
-> SSE status/token/done
-> Session/Search Trace Store
Chi tiết xem docs/architecture-pipeline.md.
- Backend: FastAPI, Pydantic Settings, HTTPX, SQLAlchemy, Alembic, psycopg.
- Frontend: Next.js 16, React 19, Tailwind CSS 4.
- Search providers: Tavily, SearXNG.
- LLM: API OpenAI-compatible, ví dụ vLLM/local model server.
- Local infra tùy chọn: PostgreSQL, pgAdmin, SearXNG Docker.
- CI/CD: GitHub Actions.
web-agent/
backend/ FastAPI backend
frontend/ Next.js frontend
config/ cấu hình local cho infra phụ trợ
docs/ tài liệu dự án
.github/workflows/ GitHub Actions CI
setup.sh setup Linux/macOS/Git Bash
run.sh chạy app Linux/macOS/Git Bash
stop.sh dừng app Linux/macOS/Git Bash
delete.sh xoá container/image Docker local
setup.ps1 setup Windows PowerShell
run.ps1 chạy app Windows PowerShell
stop.ps1 dừng app Windows PowerShell
delete.ps1 xoá container/image Docker local trên Windows
cd web-agent
cp .env.example .env
./setup.shcd web-agent
Copy-Item .env.example .env -Force
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\setup.ps1Nếu không muốn đổi execution policy trong phiên PowerShell hiện tại, có thể gọi trực tiếp:
powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\setup.ps1Linux/macOS/Git Bash:
./run.sh
./stop.shWindows PowerShell:
powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\run.ps1
powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\stop.ps1URL mặc định:
- Frontend:
http://localhost:3005 - Backend:
http://127.0.0.1:8011 - API prefix:
/api/v1
Web Agent cần Tavily API key để dùng provider tìm kiếm chính. Nếu chưa có key, hệ thống vẫn có thể fallback sang SearXNG khi được cấu hình, nhưng chất lượng và độ ổn định thường sẽ thấp hơn Tavily.
Cách lấy key:
- Truy cập Tavily dashboard:
https://tavily.com/hoặc trang API keyshttps://tavily.com/api-keys. - Đăng nhập/đăng ký tài khoản Tavily.
- Tạo hoặc copy API key trong dashboard. Tavily docs mô tả API dùng key ở header
Authorization: Bearer tvly-YOUR_API_KEY: https://docs.tavily.com/documentation/api-reference/introduction
Cách thêm key vào Web Agent:
- Chạy app và mở
http://localhost:3005. - Bấm
Cài đặt. - Mở phần
Tavily Keys. - Dán API key, đặt label nếu muốn, rồi lưu.
Key được lưu local trong backend/config/tavily_keys.json. File này không nên commit lên GitHub.
Nếu muốn thêm bằng API:
curl -X POST http://127.0.0.1:8011/api/v1/keys/tavily \
-H "Content-Type: application/json" \
-d '{"api_key":"tvly-YOUR_API_KEY","label":"Default key"}'Nếu bật PostgreSQL, pgAdmin hoặc SearXNG bằng Docker, có thể dọn container/image bằng:
./delete.shpowershell.exe -NoProfile -ExecutionPolicy Bypass -File .\delete.ps1Giữ lại image, chỉ xoá container:
./delete.sh --keep-imagespowershell.exe -NoProfile -ExecutionPolicy Bypass -File .\delete.ps1 -KeepImagesRoot .env dùng cho script setup/run:
BACKEND_HOST,BACKEND_PORT: host/port backend.FRONTEND_HOST,FRONTEND_PORT: host/port frontend.LLM_BASE_URL: endpoint OpenAI-compatible, ví dụhttp://127.0.0.1:8007/v1.LLM_MODEL: model ID.FEATURE_SESSION_HISTORY: bật/tắt lịch sử chat.FEATURE_OPS_DASHBOARD: bật/tắt Ops Dashboard.FEATURE_LLM_RUNTIME_CONFIG: bật/tắt Prompt Manager/LLM runtime config.POSTGRES_AUTO_START,PGADMIN_AUTO_START,SEARXNG_AUTO_START: tự start infra Docker khi setup.
Xem đầy đủ tại docs/env-reference.md.
GET /api/v1/healthPOST /api/v1/searchPOST /api/v1/search/streamPOST /api/v1/chat/sessionsGET /api/v1/chat/sessionsGET /api/v1/chat/sessions/{session_id}DELETE /api/v1/chat/sessions/{session_id}GET /api/v1/keys/tavilyPOST /api/v1/keys/tavilyDELETE /api/v1/keys/tavily/{key_id}GET /api/v1/llm/configPATCH /api/v1/llm/configGET /api/v1/llm/healthPOST /api/v1/llm/testGET /api/v1/ops/audit/logs
Backend:
cd backend
../.venv/Scripts/python.exe -m pytest -qFrontend:
cd frontend
npm run lint
npm run buildTrên Linux/macOS thay ../.venv/Scripts/python.exe bằng ../.venv/bin/python.
Workflow hiện tại nằm tại .github/workflows/ci.yml.
CI chạy khi push, pull_request hoặc workflow_dispatch:
- Backend job: setup Python 3.12, install
backend[dev], chạypytest. - Frontend job: setup Node 24,
npm ci,npm run lint,npm run build.
CD chưa bật vì chưa có target deploy chính thức. Khi có staging/production, nên thêm workflow deploy riêng, dùng GitHub Environments và Secrets cho endpoint/key.
Chi tiết xem docs/ci-cd.md.
- docs/README.md: mục lục tài liệu.
- docs/setup-cross-platform.md: setup đa nền tảng.
- docs/architecture-pipeline.md: kiến trúc và pipeline.
- docs/env-reference.md: biến môi trường.
- docs/ci-cd.md: CI/CD.
- Không commit
.env,backend/.env,frontend/.env.local, logs hoặc key thật. - Nếu model chạy trên máy khác, máy chạy backend phải truy cập được
LLM_BASE_URL. - Tavily key thật chỉ nhập qua UI/API local, không ghi vào
.env.examplevà không commit lên GitHub. - Nếu public SearXNG bị
403/429, nên bật SearXNG local bằng Docker. - Nếu chạy PowerShell bị chặn script, dùng
-ExecutionPolicy Bypassnhư ví dụ phía trên.