DeepSeek RAG Chatbot SPA on EdgeOne Template
This project is a full-stack chatbot SPA built on the EdgeOne Pages + Cloud Functions template.
It follows the EdgeOne template structure and adds:
- DeepSeek chat generation
- CNB Knowledge Base retrieval (RAG)
- Next.js single-page chat UI
- Express backend in cloud function
- Frontend SPA chat page in src/app/page.tsx
- Backend API in cloud-functions/express/[[default]].js
- RAG retrieval from repository knowledge base:
- Retrieval API usage based on:
EdgeOne Template Compliance
The app keeps the standard EdgeOne cloud-function path:
- cloud-functions/express/[[default]].js
On EdgeOne Pages, this function is exposed under /express.
Implemented routes:
- GET /express/
- GET /express/health
- POST /express/chat
- POST /express/chat/stream
- User sends a question from the SPA.
- Backend calls CNB knowledge base query API:
- Backend builds knowledge context from retrieved chunks.
- Backend calls DeepSeek chat completion API with stream mode.
- Backend streams tokens to frontend via SSE and sends source metadata.
All tokens and endpoints are read from environment variables.
Required:
- DEEPSEEK_API_KEY
- CNB_TOKEN
Optional:
Use .env.example as the reference file.
Install dependencies:
npm install
Run Next.js locally:
npm run dev
If you run with EdgeOne local runtime, use your usual EdgeOne command (for example edgeone pages dev) so cloud functions are available under /express.
POST /express/chat
{
"question": "这个仓库怎么部署到 EdgeOne?",
"history": [
{ "role": "user", "content": "上一条问题" },
{ "role": "assistant", "content": "上一条回答" }
]
}
POST /express/chat/stream
Request body is the same as /express/chat.
SSE events:
- event: meta (data: { sources: [...] })
- event: token (data: { content: "..." })
- event: done (data: { ok: true })
- event: error (data: { message: "..." })
Response:
{
"ok": true,
"answer": "...",
"sources": [
{
"score": 0.86,
"path": "docs/deploy.md",
"url": "https://...",
"name": "deploy"
}
]
}
- The backend limits history length and validates message shape for safety.
- If retrieval or generation fails, the API returns descriptive errors.
- The UI is mobile-friendly and desktop-friendly.