logo
0
0
WeChat Login

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

What Was Built

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

RAG Flow

  1. User sends a question from the SPA.
  2. Backend calls CNB knowledge base query API:
  3. Backend builds knowledge context from retrieved chunks.
  4. Backend calls DeepSeek chat completion API with stream mode.
  5. Backend streams tokens to frontend via SSE and sends source metadata.

Environment Variables

All tokens and endpoints are read from environment variables.

Required:

  • DEEPSEEK_API_KEY
  • CNB_TOKEN

Optional:

Use .env.example as the reference file.

Local Development

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.

Request Contract

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" } ] }

Notes

  • 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.

About

基于 edgeone 的 express AI 后端

386.00 KiB
0 forks0 stars1 branches0 TagREADMEMIT license
Language
TypeScript54.3%
JavaScript38.4%
CSS7.3%