|
|
楼主 |
发表于 2025-1-24 13:35:19
来自手机
|
显示全部楼层
以下的操作步骤,帮助您将 DeepSeek 集成到 NocoBase 中。
8 ]0 f- a! l1 t+ c. o! Y5 I" [$ |/ ` Y, |9 }& z. e; Y
我们将从环境准备、NocoBase 安装、DeepSeek API 集成、插件开发到测试部署,逐步展开。
% G) E$ {2 X( F* \: F! h9 z5 j1 P7 }- x" V( Q @/ |
---
4 U! h, c" r* B* k8 c5 M d8 b9 K5 d. _0 }% @) v
### **1. 环境准备**1 J4 D- `4 n# A) I0 r8 |7 i7 O
#### 1.1 服务器要求. l1 x8 v2 n' s1 B% O
- **操作系统**:Linux(Ubuntu 20.04 或 CentOS 7)或 macOS。
0 q7 U/ R0 ?: L1 {' N- **Node.js**:安装 Node.js v16.x 或更高版本。# R) U- _& k. L1 T5 k+ P0 h& D
- **数据库**:PostgreSQL(推荐)或 MySQL。
% j# a# T4 e% c. N" I9 G, J3 a- I7 Y- **DeepSeek API 密钥**:注册 DeepSeek 账户并获取 API 密钥。8 T: @8 y1 d+ t" i1 q
6 Z* _5 S+ X/ @$ X. o0 j5 d
#### 1.2 安装依赖
3 W0 _" J; S2 x% q; Z8 j- 安装 Node.js 和 npm:% F( O6 G* K/ R, o* }
```bash: w" e! D- R) X% Q: W
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -" g7 s. k! E- I% o+ Y; l2 y
sudo apt-get install -y nodejs% X0 I# I+ j4 ] d
```* L \6 j* {" G7 ~0 w* F* V
- 安装 PostgreSQL:
; r+ j) r1 u4 z( l0 e) N ```bash+ V4 ^$ W8 H ]* X
sudo apt-get install postgresql postgresql-contrib% q, j8 s9 {9 |2 Y
sudo systemctl start postgresql
0 W/ o0 j" I2 m8 u; a% n r sudo systemctl enable postgresql/ v9 F8 [+ o7 n2 d4 V7 g
```: [' k6 g0 A, i& P; |7 h
9 X6 {8 i! K8 A8 M3 Y# d! O; B5 b* r---
' C4 I( H c: w6 t- a; F! o+ P
8 t* L/ O) ~% I### **2. 安装 NocoBase**
) ]# o+ W) n/ k R; U, \. `#### 2.1 下载 NocoBase
7 q" _/ a: l+ `, A/ ^9 p- 从 GitHub 克隆 NocoBase 仓库:+ m$ z: v1 x! { D
```bash) G/ j% n4 } p# A2 @
git clone https://github.com/nocobase/nocobase.git
1 _* n. w* V" K9 O- l! X$ J8 s+ q cd nocobase
9 q1 D* v( h) E' K, Z" C ```8 s2 d% r+ L2 ~
: d2 x; A+ O% z u
#### 2.2 安装依赖3 O1 x' }) E6 _/ l8 J2 d
- 安装项目依赖:2 x' w$ [' e3 }; ?
```bash
& w4 W5 E3 v- \/ K+ I/ k npm install
" e% i& ~7 z9 o% f: M( }1 S2 ^ ```
) a. E1 _- C2 }0 @/ d* F
- O7 ^: B6 A, c! k J/ Z#### 2.3 配置数据库1 W, L; B) y, o9 s7 @0 C
- 创建 PostgreSQL 数据库:
1 \$ A* E/ o& f" {% q ```bash
) j- Z8 t( h5 o5 L sudo -u postgres psql8 f$ F) [: P& Z3 ]7 S
CREATE DATABASE nocobase;
. w! [+ T, @( ~" s; Q' K4 R CREATE USER nocobase_user WITH PASSWORD 'your_password';7 S! v0 [" s8 j! D Q& \" k
GRANT ALL PRIVILEGES ON DATABASE nocobase TO nocobase_user;
1 [! o( t6 ^& F+ E9 W4 K( I+ p \ \q9 D. h3 Q: r" O. y6 _
```
# l# E- [& Z% J! ~3 X% ?- 修改 NocoBase 配置文件:# k1 I' G3 @ M& |) ~
编辑 `config/config.json`,配置数据库连接信息:
$ ]1 `* P' f' v- T% l* A& Y ```json
) I% E5 a! j) J( b, }7 b {! \1 \' H% C' \2 t
"database": {5 A/ O: _& F8 R: ^: u2 k+ E
"dialect": "postgres",
4 z/ K3 E4 z. j' R! }7 q t "host": "localhost",& F" G2 M3 W) b) G0 ]& o
"port": 5432,
1 e5 r; w$ o9 N "username": "nocobase_user",! j* r9 U8 h& R: T4 ^7 G
"password": "your_password",
! s+ i) S$ c3 v3 d' k1 a' q "database": "nocobase"8 ~9 N* |% }: J h/ _! O
}. y- Y& h$ ]2 z* h J9 i
}- L% Z" ]% Z, p2 a8 Y- `
```5 f4 G+ L' r* X. f7 a
& Q) ~8 T. G& T) [& o" w* l
#### 2.4 初始化数据库
! t, K# P. _& ^4 G% ~( H; M4 r- 运行数据库迁移脚本:. l3 N3 s% i8 }; X
```bash
' n( {4 w( {% u# R6 o( | npm run db:migrate5 G$ `: i8 E( y+ Y" \4 t. [3 a
``` g( n3 h. b2 Z! q! O* G
$ l% @4 w4 ~) K1 H! f( n
#### 2.5 启动 NocoBase
/ ~' x! _9 _7 X$ X) F- 启动开发服务器:7 Y |6 i b" t" p" N+ b
```bash( n& u9 [6 ^+ }5 `9 h5 k
npm run start x5 ], R& ]2 V" M
```
+ B' S5 _! v/ p/ e! M/ X- 访问 NocoBase:+ e2 B; |9 X) ?# s$ ]
打开浏览器,访问 `http://localhost:3000`,完成初始设置。0 R) h, g) t1 F4 r% [2 M) u
& H2 e, |/ ^ z% m1 e: x* N0 P
---
1 e% t; z4 ^! E- }) A5 W. ?# t
- e4 g4 l' p" ]### **3. 集成 DeepSeek** s; U X0 M |, q* T* S$ N
#### 3.1 创建 NocoBase 插件2 U2 d3 A- C/ H" \) t, p' {
- 使用 NocoBase CLI 创建插件:
0 O$ m9 M# w' w5 _* f( { ```bash
7 o$ g. ~% x1 _3 _+ m0 y4 a7 T: b1 P npm run cli plugin:create deepseek-integration/ I/ |* u# `; B W2 T
cd plugins/deepseek-integration
: m, ]7 x4 [' d- B' X+ a1 [ ```! l6 I3 H( q- N2 U1 ^8 K2 Y
" E9 u7 f& }/ [( Z# l
#### 3.2 安装 DeepSeek SDK
7 f4 i; F" G0 q( j5 z' A# P- 安装 DeepSeek 的 Node.js SDK(假设 DeepSeek 提供 SDK): `, M! v7 e: u6 Q6 [% V, u. ^
```bash
6 y- `5 x0 ~( r4 ] npm install deepseek-sdk
3 y; h7 [$ t }; g" P+ p7 S1 C ```
: z3 a& y. N+ v1 m8 p+ D) Z+ [3 M: J' {4 W+ Q" n8 R2 L2 A/ u# ?
#### 3.3 配置 DeepSeek API 密钥
. w5 }0 R% l; T- 在插件配置文件中添加 DeepSeek API 密钥:
b1 B8 K* W, S+ o 编辑 `plugins/deepseek-integration/config/config.json`:( B/ k2 l4 d& ~
```json4 D) g9 x5 v% k- _* ~9 f
{
8 T6 Y( l6 W# }( M2 Z9 n "deepseek": {5 ~0 ?0 d- m/ I# u6 A
"apiKey": "your_deepseek_api_key"
# S; B9 Z- D% d7 j7 u }
8 _4 L8 T0 G% |9 H) v. c5 D }3 `9 ^: L$ V. q+ z% `2 L
```" w, s1 Q3 ^0 N7 G
$ L% D2 A% A" W' d$ k3 Z) k1 i
#### 3.4 实现 DeepSeek 服务
- f! m0 O8 B* P- 创建服务文件 `plugins/deepseek-integration/src/services/deepseekService.js`:
! d( a! I) e: ` ```javascript0 o7 V, c' \0 M* H, q, \
const DeepSeek = require('deepseek-sdk');
9 K4 Q, I; m0 _ S- \ H7 R9 K0 _$ P' ]& C
class DeepSeekService {9 M! k7 b! J) e4 g
constructor(apiKey) {, |8 O% {2 s. e" a+ e7 V. `( z' e4 V
this.deepseek = new DeepSeek(apiKey);
6 m) ~7 Z( F5 S, I }
P1 F" x2 P% s5 G! i( i4 V
0 ^* @" L" x+ ~1 q async query(prompt) {6 }; v" N$ n. A
const response = await this.deepseek.complete({
6 n4 E. C- ?0 p& K2 h' G prompt: prompt,
9 c% I8 p3 Z: b9 F% E+ z max_tokens: 150- ?% ~1 F' C m2 {$ R
});$ [; z# C1 o3 z4 D
return response.choices[0].text.trim();
0 y" u' ~+ f* L& v) Z1 U }! }* E* S6 S1 i. I3 ^0 m$ y) L
}7 Y8 @$ c) a, t0 J
0 V' Q. D _; {# h. V1 M# f module.exports = DeepSeekService;- y5 i) K' r/ ]3 L) c3 @, I9 A: |
```
5 N. G1 I8 O+ b8 q' ~1 Q: q
$ e, l( ~/ ]" ~; K5 V#### 3.5 创建 API 路由
4 Q$ c- X" m& G+ t/ z' F. ^- 创建路由文件 `plugins/deepseek-integration/src/routes/deepseekRoutes.js`:4 }0 W0 b! d: k; g) A, E( H
```javascript( U2 F6 P3 T! u5 V! R" |
const express = require('express'); [. S: F7 ~! k5 B. E0 M* y
const DeepSeekService = require('../services/deepseekService');
- F' F/ i8 \" Y: Z const router = express.Router();% Q. p: i4 L) e3 S" b" a7 k
const config = require('config');9 E2 U9 J1 k' |0 s4 `) q/ N
& A' ?% A6 R& a/ c4 d0 n const deepseekService = new DeepSeekService(config.deepseek.apiKey);
$ }* u# b+ {' L% ?, e. X) ?; z, h. w2 z/ C
router.post('/query', async (req, res) => {1 K; q x0 x" Q; K3 Q$ U* K+ C
const { prompt } = req.body;/ C& d3 O( M3 t
try {
$ i- f9 X& {) C3 o9 w8 y; a const result = await deepseekService.query(prompt);0 ~: w$ m0 @* O
res.json({ success: true, result });
: P! O& C* d( Y } catch (error) {% _. v; l E1 q* s' s
res.status(500).json({ success: false, error: error.message });8 E' z* S0 G$ K# Y- e; a0 n
}
# T, ^1 U/ {* f });
" j0 k% N- L1 l2 B6 l5 Z3 a. ?( r% g/ ]. W. `. m- m/ _
module.exports = router;( { g2 U) I/ E3 Z# E% B
```. T% d8 D w9 _7 `: i- n
+ u z2 i( V. `#### 3.6 注册插件
0 y% a. t/ a! u& v$ |- 编辑插件入口文件 `plugins/deepseek-integration/src/index.js`:
& ~! M/ u6 R- i+ y2 K' N$ u) _ ```javascript# M! m, E) t% e: {; H. Q
const deepseekRoutes = require('./routes/deepseekRoutes');
+ q& E$ D9 h% ^; x4 g( }1 |1 u. K2 C) ]9 M* v7 y& s1 F5 ], e. p1 ]
module.exports = function(app) {
% o3 E! x) B, t# S app.use('/api/deepseek', deepseekRoutes);
' \# j; p0 e7 U8 y9 n# r };, S+ R$ v$ z3 E5 F7 q. y7 U
```
' v5 O1 R( [5 ^8 _# r/ f+ B. ^/ s& W8 I' t2 Y
---& E7 N+ Z/ `' k, M$ Z. K- T, I/ J" ^
1 B( z4 z$ K/ @9 M+ U" ^1 i
### **4. 测试集成**
5 ~1 R. A! U0 I7 Q* h6 n#### 4.1 启动 NocoBase& W/ L& s2 H8 ^4 D6 P
- 启动 NocoBase:
- x" L% J' p, E1 P0 y ```bash
9 s& U5 d1 E+ @$ j) J3 a7 H W npm run start9 q* b/ @$ z1 \; O C) ]
```* ^( R. v: o& B' v* D. U: s
6 T- C; S: U5 E9 o8 z$ b6 F#### 4.2 测试 DeepSeek API
/ j2 @6 s9 V B9 q: |6 u8 w7 C- 使用 `curl` 或 Postman 测试 API:8 w! O" _% A- N; q* m
```bash
, r: f$ u p2 x7 [& X curl -X POST http://localhost:3000/api/deepseek/query \2 \0 m* L: M* ?1 g
-H "Content-Type: application/json" \3 g: {# }; R9 J" Z
-d '{"prompt": "显示最近三个月联系过的客户"}'
# `! `5 t# S9 t( { ```
2 f- \! C, p |" W5 K2 @6 X# f3 h* u. e: ?; r6 W' Y0 \
---4 y5 m$ }3 n% l, u% B' L2 L8 V
5 g2 [9 }# ^ c9 F, ^+ N. ?- i' ]
### **5. 部署到生产环境**
. j3 H' i T/ U, @/ V* A5 `#### 5.1 使用 Docker 部署
0 t! e# z9 F: f- _/ T- 创建 `docker-compose.yml` 文件:
: t5 z! C% Q9 a. `0 S! @* z ```yaml
: }2 f* R- s$ T# O version: '3'
b8 a" Y5 J3 @8 a# P3 A services:5 T4 ?# I* S( Z1 K8 c9 S
app:
$ q* C, E @+ q c; R l, n image: node:16% p5 w, p9 U- _' f/ u9 K
container_name: nocobase
) ]& e. E6 v( } working_dir: /app) Q0 D. \3 i9 c$ k
volumes:
+ t- A/ U. y/ I( ^ n3 Z - .:/app
& Y! G, \7 c; l- O8 x. @/ }' u. R. E7 w ports:1 l, i( S+ G& X0 u
- "3000:3000"
$ \5 B7 \- {5 P4 L, h/ h2 `( M; ] environment:
$ c2 `/ f* X0 G5 C - NODE_ENV=production9 q# `$ P2 V1 ]: I
command: npm run start
- h \4 V# B5 U: Y- i* L S2 ] db:
1 U% r1 L4 ]- K6 h- G% u image: postgres:13
0 ? y* _) s) [; g, f! H3 h/ [ container_name: nocobase_db
) S% I: t9 w3 N- _% V( t environment:$ K1 {! g3 D7 B F+ a. W
POSTGRES_USER: nocobase_user
$ L4 e8 C4 Y8 ] e S$ q) B POSTGRES_PASSWORD: your_password& r' ` k8 {9 e7 p; f2 e
POSTGRES_DB: nocobase/ f! B! _/ S$ x
ports:
) n" q: N! [: E, H' X. u - "5432:5432"
$ o9 w" {1 j; q. ~ ```# X; T: o! E5 b8 U( F1 y
- 启动 Docker 容器:' X( _6 h' W3 C4 E; v0 ?
```bash
' p* ]) v5 x. J8 L" A docker-compose up -d
7 J* j g3 S. T/ J* b- q ```
" t Z4 a* z0 L" D9 @: `* W! [; O* N$ C& {
#### 5.2 配置 HTTPS9 y: ]8 N& X" o8 A+ y
- 使用 Nginx 反向代理并配置 HTTPS:
" i8 A( j8 n K4 }4 g+ b ```nginx8 z, h9 c8 N/ ?% W/ N8 X
server {
9 A7 M+ t; k( Z E5 J+ c* g listen 80;2 W% @+ c5 D9 N% N& ? z S3 m
server_name yourdomain.com;3 }5 R: e& z/ ~0 F
return 301 https://$host$request_uri;# Y0 I3 D7 {( S& C. l. F" U% M" U
}" Q' s! r- A9 w* b
, T( Y; ?( b0 F0 L' v4 {" i& G
server {
/ R; {: c) M* O9 T listen 443 ssl;# B y) w: D. o7 b4 n
server_name yourdomain.com;
" |( y! |) ]" F* t$ D; _/ a( r0 y' Y o! `
ssl_certificate /path/to/your/certificate.crt;
, K, w2 l4 Y% J. ~4 i5 l4 O4 ~ ssl_certificate_key /path/to/your/private.key;
, B: L0 }* m# n' s# C5 l( [
& \/ e+ d/ M7 i# Z location / {! K3 F. U, S4 D2 z, L# W) i
proxy_pass http://localhost:3000;
" `! ]5 V) D! J4 e8 \" c proxy_set_header Host $host;1 Y# u+ H4 u b
proxy_set_header X-Real-IP $remote_addr;
: i- A+ f& j4 ?0 g }
' [- R; M% @7 a& _/ H; ~ M }/ _9 i/ X9 G9 \3 @! Z, ]
```
& D5 Q8 t: [: ]8 \8 \8 P6 ]8 }! [+ ]) n. W m
---
8 k2 \4 y3 P q' f2 Y K2 F! ?5 l) Y. n' J' f: `0 y
### **6. 总结**' X. ~9 t7 {4 ]% E
通过以上步骤,您已经成功将 DeepSeek 集成到 NocoBase 中,并可以通过 API 调用实现自然语言查询、智能建议等功能。这一集成显著提升了 NocoBase 的智能化水平,为用户提供了更高效的操作体验。 |
|