|
|
楼主 |
发表于 2025-1-24 13:35:19
来自手机
|
显示全部楼层
以下的操作步骤,帮助您将 DeepSeek 集成到 NocoBase 中。0 j8 A H, n" X: K# Q1 M3 k! M4 M
) e5 |4 c- i6 f2 B+ _3 M, L6 D
我们将从环境准备、NocoBase 安装、DeepSeek API 集成、插件开发到测试部署,逐步展开。% s7 C/ }6 V5 x7 u* M
- [* H* ^8 G' B/ R% E--- y8 q+ M/ _+ M$ M
. ^8 P8 X- h: p# x
### **1. 环境准备**3 O" p- b/ p. N
#### 1.1 服务器要求
1 X% k( x6 l/ t& U- M- **操作系统**:Linux(Ubuntu 20.04 或 CentOS 7)或 macOS。
$ z- |8 r! g) T3 Q# |" [& j- **Node.js**:安装 Node.js v16.x 或更高版本。
Z, @5 ^( }, A- **数据库**:PostgreSQL(推荐)或 MySQL。1 r! t: f$ ]: [8 v
- **DeepSeek API 密钥**:注册 DeepSeek 账户并获取 API 密钥。
( P. ^8 s3 `" x% ?& `" V
* u' k6 Z$ y$ v0 \3 v# e0 J% ?#### 1.2 安装依赖
* ?/ o1 T, m& r b. @- 安装 Node.js 和 npm:
8 s* U. k) k0 {; h& g) I9 h% ^# c ```bash
. }3 p: s5 L- j# S: b) H" ^- D curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
3 C- H( J9 Y2 I8 O6 P [ sudo apt-get install -y nodejs" ^: [8 b# V5 Y" M# T) E1 \8 d
```/ @( Y. S- k: Y% \4 Y* q& o- s
- 安装 PostgreSQL:6 V3 s8 k1 V1 {) f5 ^
```bash/ r+ N3 i/ B: O4 V
sudo apt-get install postgresql postgresql-contrib% b j6 Q) {: P4 `; A
sudo systemctl start postgresql- }! u' {% b3 {* r ~
sudo systemctl enable postgresql
2 t0 Q: n+ S9 \5 n ```2 I2 Y9 n% P2 H' P
% n! P* H- N% z5 b
---
/ J9 q* e$ p7 s3 C3 H5 Y2 H0 s- Y
) B6 a3 R8 D) u$ T( K: J### **2. 安装 NocoBase**# ^, Q `0 u2 m, l
#### 2.1 下载 NocoBase
. d% F7 U1 s4 E/ I: {4 i, O0 G- 从 GitHub 克隆 NocoBase 仓库:9 ]( G* D$ z. M) B5 B
```bash
- k& ^" B% n+ ?1 @- G: A git clone https://github.com/nocobase/nocobase.git
. V p# B0 Y) [4 N1 a+ r9 \9 p cd nocobase, D6 E& o% z `2 ]( ?
```$ V) ~* \) _$ A( m6 J3 c g
( O. T2 ~! r( C#### 2.2 安装依赖
; k7 t6 ^) D) h, y+ D- J- 安装项目依赖:
) u; j% M ]9 u ```bash
1 P$ Y& e, I, z: a2 p% ^+ O npm install
3 H$ M( q+ r/ ~% [ ```" \2 D6 R5 F7 u/ u0 s# S
) x P7 a+ [% e! L* E
#### 2.3 配置数据库) h/ p% f" V; w! \& G. e; H
- 创建 PostgreSQL 数据库:
/ s# W& c; f1 V2 {" r2 B" s/ a ```bash* S- S" |! c$ E$ c' W. b( V; {" s& e
sudo -u postgres psql0 f% J; ?, ^. c+ v; z8 U
CREATE DATABASE nocobase;
* Q! s7 v( b/ z: W) V CREATE USER nocobase_user WITH PASSWORD 'your_password';
# e; `9 K/ R) t% ]7 A GRANT ALL PRIVILEGES ON DATABASE nocobase TO nocobase_user;
# r; T* U1 ]6 i) Q \q: n) w Y8 c) V' @
```! ^# P W- V" j7 \* v0 \2 z8 d# w0 E& `
- 修改 NocoBase 配置文件:- c% y0 e. N) E% D
编辑 `config/config.json`,配置数据库连接信息:* b# F- ^( V, M3 a0 Z
```json; W0 G* d6 I3 d/ X
{
" _! E+ a; S5 I5 }) } "database": {
; q2 C4 @) S; ]% ~' s7 W c "dialect": "postgres",. ?( c& a; W ~- w
"host": "localhost",
0 t; D$ s: v( Z/ k "port": 5432,/ y# d! J9 ^2 c5 F
"username": "nocobase_user",# q1 M' \1 V% i; E' h
"password": "your_password",5 c& F! o. r# p0 N. M5 c: f
"database": "nocobase"* `4 B: N7 I7 R' P0 N' w
}( z9 a9 F& B# R; @
}6 [1 K# d$ J o% @3 l% f( _
```
! z9 }! ~4 L/ t8 H9 j
3 h1 O; g* j9 E/ {. L# ]6 P( }#### 2.4 初始化数据库0 l0 `3 S& a1 K
- 运行数据库迁移脚本:
. F0 \0 p* k7 ] ```bash2 i9 }! a6 D# I8 n C3 O
npm run db:migrate
) {1 d0 P; @$ Z: m& `8 f5 Y ```
' x/ q/ ^2 H5 u% F' C* e2 _4 o2 x6 q
#### 2.5 启动 NocoBase1 a% f: I1 l& U5 C7 S4 {9 [* [" q' W+ _
- 启动开发服务器:& H$ x9 j4 F m, l6 D5 L
```bash
. y4 I/ W* Q a: g npm run start# y8 E4 Y3 U/ X+ V$ h
```
# h3 D. p6 n* U5 ^$ w. f- 访问 NocoBase:
, z' d# j& L* S 打开浏览器,访问 `http://localhost:3000`,完成初始设置。
Z. I" _) |" _5 |' R2 x) `
8 {4 r$ w6 p0 N& v---
. c& T6 k6 w* x% c( v; J
4 j8 D6 l4 d7 B9 H### **3. 集成 DeepSeek**
: x) v2 f l+ F#### 3.1 创建 NocoBase 插件
9 ~7 Z! f5 o2 Y' r* f" v u) d- P- 使用 NocoBase CLI 创建插件:
# A7 z B" `! K/ [' d ```bash
1 w/ g8 E+ U& b) x npm run cli plugin:create deepseek-integration
5 r, c5 \" x6 V% ^ cd plugins/deepseek-integration* F* m P( h% r- Z% Y* g& t9 ~
```
+ E) V2 N! R& i( K) c5 |2 x
' Y/ A) M$ e7 p/ ?) i" z3 m# r2 _#### 3.2 安装 DeepSeek SDK
: s- [, K# U) n+ ~4 P8 s( ~- 安装 DeepSeek 的 Node.js SDK(假设 DeepSeek 提供 SDK):. {" s" u n; K% Q( @9 K
```bash
: j0 z% `$ n: k. c! j7 H npm install deepseek-sdk3 h# n7 _2 Q: o( f) V
```/ h8 a6 `! Z4 l. E9 F
9 n( x z h/ z# Z2 j5 u
#### 3.3 配置 DeepSeek API 密钥
9 A/ ~% c' Z4 s. |: f6 g- 在插件配置文件中添加 DeepSeek API 密钥:2 a. k" |, i$ N0 d1 D
编辑 `plugins/deepseek-integration/config/config.json`:
. a( m, Z" z, Y7 e# D ```json& `+ t$ \4 r: F) W* R* H
{
! ^6 e9 O K; o" p3 ^8 i "deepseek": {
" o5 C9 `2 k$ s) ^+ B- x/ y7 D "apiKey": "your_deepseek_api_key". q, T3 I* W* z2 G# T
}$ X9 ?* I! q$ d- N: d% J* J
}( W% v$ ^4 ]( z- g
```
- A4 {" v6 O7 a* x& {' ]4 m& T5 U
: A+ o* U: M; g$ q#### 3.4 实现 DeepSeek 服务
- e. p8 y0 B% Z; z- 创建服务文件 `plugins/deepseek-integration/src/services/deepseekService.js`:
7 U$ W1 b( N1 b3 T ```javascript
! p+ U2 R+ A' q0 L* } const DeepSeek = require('deepseek-sdk');. h9 Z# J9 ~* ^: ~) P# e
% [. @0 t. i- j* \, ]- Q class DeepSeekService {# l% v4 `9 w0 w6 }6 I0 k" |( a. ~
constructor(apiKey) {
8 B) G* {' @0 s8 e/ H6 @0 V this.deepseek = new DeepSeek(apiKey);' `$ i# x3 n% s$ _
}
+ E7 C6 }1 o+ n. E( X \
% n. I2 W: \" l$ y) f" i6 z& C) X async query(prompt) {: g. X, D5 l! p' Z3 c* X& M! i7 z: w
const response = await this.deepseek.complete({
! j9 _9 l; k! S8 [ prompt: prompt,
' s- }' K; P8 M2 C9 @ max_tokens: 150
" @: ]& o/ D# l% k$ J: m5 C });3 _2 o, l9 o* R
return response.choices[0].text.trim();" Z) }( B) \- V$ S t
}
8 u: v' h) s- q6 G }
7 T' o$ \- J3 Y6 ^! N8 \( H7 y( i$ U9 d' S9 u$ N X2 ^; h( k6 i8 O2 l
module.exports = DeepSeekService;
# X, f% ?9 v/ A0 q, \) e ```0 p- i' V9 B: w; ?0 X: o
- w7 d. h6 s h6 v5 G#### 3.5 创建 API 路由
1 I3 c& }, f& ~6 {/ h7 d; x R* g- 创建路由文件 `plugins/deepseek-integration/src/routes/deepseekRoutes.js`:9 @% t( P- w# ^
```javascript
) x& H5 P% U3 V( w const express = require('express');
+ q, y4 q6 R* C2 d8 i const DeepSeekService = require('../services/deepseekService');
. k' p' c; I" J- Y# ^& k const router = express.Router();( Z: f1 c# y4 k( b: T9 d6 |
const config = require('config');
$ _+ |4 {: N9 c$ g
" N4 U0 g7 c' U( H4 R! e const deepseekService = new DeepSeekService(config.deepseek.apiKey);' K& K+ y, z! T! ~# T
( e0 q: @6 u5 z! R3 J0 K8 y2 n router.post('/query', async (req, res) => {2 l6 L4 Y# o' x- e' W& \, a
const { prompt } = req.body;
3 y' o6 u) _6 c try {0 v. m$ ?. r' b) O1 |) P: ~5 G
const result = await deepseekService.query(prompt);
6 B/ \4 }& q/ @! W' w4 Z res.json({ success: true, result });
1 t- W* E6 l- R5 {( I* }* D, D } catch (error) {6 C' I0 f/ q2 ^( j
res.status(500).json({ success: false, error: error.message });8 i r# |# e1 `7 M
}2 F+ E. @( }. y7 O0 L( K
});
+ z2 @& o6 m% U4 s
& j! v+ L5 L/ e5 d, w6 T& o module.exports = router;+ C! ^ y: ]* u' C2 n! I, W
```8 W. ~5 M# W, [0 i6 `
1 m1 ^9 U P. w. F; ^# _#### 3.6 注册插件. E3 r; l1 C* m5 E% [" V
- 编辑插件入口文件 `plugins/deepseek-integration/src/index.js`:4 b; H# O% K( Q5 [( d# n
```javascript3 `7 | r: L, D5 `' m
const deepseekRoutes = require('./routes/deepseekRoutes');1 J( O& B: `+ J0 C: {
, e# H+ \# o; I module.exports = function(app) {3 i% T% G; T3 K9 b6 ^
app.use('/api/deepseek', deepseekRoutes);9 B+ [9 y: v1 y
};
$ @; ~* D. Q/ z/ W, e6 K$ T ```# D9 i/ ]. N+ X6 L& f
3 L, @! w5 s. f
---
) z6 G# E) R7 x" D5 R' R' ?8 T
9 t( l( ] C( t& }) @### **4. 测试集成**
/ E; K! T: Z* J0 e8 O7 C [#### 4.1 启动 NocoBase: }- u+ t% E& h2 F7 k0 S V
- 启动 NocoBase:
6 L8 Q! d! E Y" q3 A ```bash' u5 [$ W, m+ r' X2 V
npm run start
- m8 M! L5 a8 X8 R5 f ```
9 n6 G- r3 ?: W' ^) k) H
* G" G+ K1 U8 U8 R8 ]8 d#### 4.2 测试 DeepSeek API
& g: ^, R7 ]9 W) N8 Y, S5 F- d# D1 h3 ?- 使用 `curl` 或 Postman 测试 API:
; c" w- X6 E5 X* l7 ~5 T9 s0 O ```bash
) ^/ p- f1 n/ r0 z# ?- x curl -X POST http://localhost:3000/api/deepseek/query \
9 p7 \5 @! X: I; u. A# l2 S -H "Content-Type: application/json" \7 J v7 K3 [; L ~1 |: Y0 D; C7 s) L
-d '{"prompt": "显示最近三个月联系过的客户"}'2 a3 w' A3 r2 B, Y9 O$ L
```8 n) z( o9 J9 F- }# s
C- b$ Q- i* j---: M' ~8 h" b6 ~8 G
3 y$ X r1 Y0 \: S; u
### **5. 部署到生产环境**
# t3 }1 m8 J R0 j#### 5.1 使用 Docker 部署3 D& N8 \+ z) I7 o! `
- 创建 `docker-compose.yml` 文件:
) E% n7 t- ?4 Z1 L: e, f ```yaml3 l" [" [. t* _3 M# |7 j5 x& T
version: '3'
1 |* u1 E6 D$ F* `7 t! j services: J! j# }; r2 j8 R8 F
app:
, I" T4 `1 Z) T$ F% p2 ?. b* a) G image: node:16
6 ]1 f. N6 F5 y, r+ m container_name: nocobase- h% g; Q* ~& R
working_dir: /app" k5 R5 h& Q" J Z% [
volumes:. G( n5 h% ^; G9 P
- .:/app
" n% Y9 T; B0 k$ V3 r3 c8 W" @ ports:
3 D# M2 t4 ^3 H - "3000:3000"
! A% |( }7 I+ K! `# I( e" M) b environment:
0 W' R# `; t1 Z8 m; N - NODE_ENV=production) M) L) U1 T6 {4 f3 b& Y, Q
command: npm run start
& t0 V) n) a. X. ^4 t: T db:8 o( f4 i- n) f' O* O, s
image: postgres:13
5 p. G3 f V; M0 d container_name: nocobase_db
% ^8 a0 {1 j$ {5 m7 U2 c( L environment:3 H) Y# }9 K. I* o* _1 a& O
POSTGRES_USER: nocobase_user
% e2 i" Z8 t4 ` POSTGRES_PASSWORD: your_password
4 u ~. h+ a5 Z9 a- X/ }- x% p) _ POSTGRES_DB: nocobase
, m9 e; E; c, ^# I ports:; F) Y) ]4 `6 d2 f
- "5432:5432"( \/ P/ y$ h2 v$ E% C1 V4 h- ]6 c# O0 N
```! S- z: o) U b9 T9 P/ g: d0 `
- 启动 Docker 容器:4 u1 W! {0 p& ], u$ b4 f6 b: O
```bash
+ s3 G' \8 w8 U( _2 p* B docker-compose up -d- i; o( @& M4 Z6 ?, B1 ]# d" d
```
0 h* q% Y6 `$ @# }4 O4 O( Y* ?" f6 V! @8 N0 V
#### 5.2 配置 HTTPS$ m; V1 n. }) ^, F5 ^
- 使用 Nginx 反向代理并配置 HTTPS:9 @! c) n7 @% ^- @. [: Y5 }& O
```nginx
) m$ z( @' q: }$ l, W& `4 b% U server {
) |( }1 [. W" x, h+ V listen 80;
7 `7 q, w; S" o J server_name yourdomain.com;# d5 j5 @9 Y/ U3 O) d1 Y) C+ n
return 301 https://$host$request_uri;, t# I8 K# \6 p3 U! i' k
}
9 O3 M/ K+ v) ~# b+ w* \& ~" E! l$ W, V o9 F0 L& F7 H1 r
server {5 C4 q4 L- G5 ^4 g6 _) W0 n. x: D
listen 443 ssl;) T3 Y% Z1 h( |' q
server_name yourdomain.com;
8 t) V% m; l, c8 p1 T% h3 g' W/ g1 _1 \1 M3 j. o
ssl_certificate /path/to/your/certificate.crt;
. G G/ S& Q( J" N! H ssl_certificate_key /path/to/your/private.key;
+ _/ ?5 J1 h* V3 x8 T7 O. @. r* q& N1 G# U5 {" d
location / {
4 F/ i( ^! K2 f5 n" B. g6 @- O proxy_pass http://localhost:3000;6 a; I- o0 r; F* @" p/ h7 Q
proxy_set_header Host $host;/ R1 Q$ X4 M$ c% l( ]
proxy_set_header X-Real-IP $remote_addr;
: s9 r# a! E t; b+ l }* r. D U* ^. Z
}* o! P) c0 J* T
```
( [7 p8 R2 H* x9 n# z6 [+ X- y7 g& f( r/ Q
---
! q: P+ }/ P, k9 i! L0 l$ m/ Y" A+ R' R( t
### **6. 总结**
/ e3 P& y. F, i7 x& s; M通过以上步骤,您已经成功将 DeepSeek 集成到 NocoBase 中,并可以通过 API 调用实现自然语言查询、智能建议等功能。这一集成显著提升了 NocoBase 的智能化水平,为用户提供了更高效的操作体验。 |
|