|
楼主 |
发表于 2025-1-24 13:35:19
来自手机
|
显示全部楼层
以下的操作步骤,帮助您将 DeepSeek 集成到 NocoBase 中。
- k/ [/ b& J' I F& C D) A
6 H7 G0 X" B8 e( |我们将从环境准备、NocoBase 安装、DeepSeek API 集成、插件开发到测试部署,逐步展开。
2 `# V) U( e/ {& n( }& f
, M" y2 R) } x3 ~$ ]---
- R* J% k; _/ Z( j+ l
- j* f. c J2 e5 Y### **1. 环境准备**
: ~# W+ X: z8 D8 M* y#### 1.1 服务器要求
. R: K, Y1 ^# Z- L1 e- **操作系统**:Linux(Ubuntu 20.04 或 CentOS 7)或 macOS。 C% ]2 Y8 S$ x+ S
- **Node.js**:安装 Node.js v16.x 或更高版本。6 b" t4 e" S. M0 H' ]0 N
- **数据库**:PostgreSQL(推荐)或 MySQL。
6 P4 I& t- I5 u2 _% w$ o! R" I- **DeepSeek API 密钥**:注册 DeepSeek 账户并获取 API 密钥。: _" o" j6 U# U, M9 E8 J4 T, p6 a9 @
8 T u0 }; @. P& Z: d#### 1.2 安装依赖
. a5 C, }0 ~9 `- 安装 Node.js 和 npm:: t9 n* v" f7 M5 v% K) I
```bash
7 ~& n7 v$ `2 T curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -4 P6 q% o9 e8 J; E$ C
sudo apt-get install -y nodejs; Z# p* ] \: [
```: u! p9 }4 t/ J( Q5 A
- 安装 PostgreSQL:9 I& w2 {. N* f# W- G6 r
```bash
- g- r! J' ]3 }6 d& j; O& G# X3 u sudo apt-get install postgresql postgresql-contrib6 E$ N( F" v9 M5 s' [
sudo systemctl start postgresql
* ?4 G+ j+ r: R% b& i; N sudo systemctl enable postgresql3 ?6 Z- e: [9 D% o
```
7 n2 v8 b8 i% m T
7 c, y5 i$ I+ L8 v0 n" P---/ q% X. p, v( S3 i
& t0 P7 [; {( M3 ]* f
### **2. 安装 NocoBase**
5 n! G! W ?" L0 i0 `1 F#### 2.1 下载 NocoBase; Q* X5 s {) K1 x- V1 b
- 从 GitHub 克隆 NocoBase 仓库:
; \* M; ^; c" W% D: I- f- ?% c ```bash
! Y; x' q% Y! K: s" H git clone https://github.com/nocobase/nocobase.git
& S) h5 |6 u& t8 R" B cd nocobase
% P D, Q4 S; |2 O. L0 U$ l ```( V1 r5 g9 J" W7 [/ c
# e. h/ Q$ a% l7 K0 r
#### 2.2 安装依赖- y0 E8 N+ I$ L+ q% k
- 安装项目依赖:
3 t- N. b9 l3 A6 \ ```bash% g" ?" e) @, T
npm install. b9 O: [2 _; u& }# k! ^* Q
```
! D0 S }1 T, Q' l* Z. x/ x" o, r5 L0 w, u% q ~
#### 2.3 配置数据库
3 p) j% ~8 n5 g1 |! C' a3 z- 创建 PostgreSQL 数据库: d9 R' V3 M% M, f
```bash3 j, \! F- h# W$ v7 B
sudo -u postgres psql) c; ]$ {* [ e/ q" T
CREATE DATABASE nocobase;+ I9 F: b: N( f0 s2 E! n+ Y
CREATE USER nocobase_user WITH PASSWORD 'your_password';- `) E5 R1 q. n" L
GRANT ALL PRIVILEGES ON DATABASE nocobase TO nocobase_user;) \9 w; l) {& A1 j, u
\q$ ^; q) g0 M4 q
```
0 z" J; p9 |& U2 K( G- 修改 NocoBase 配置文件:3 H8 |0 r ^% I" C% A0 {: |
编辑 `config/config.json`,配置数据库连接信息:
+ \% p. e$ I# ^9 [3 q3 ?& G# o ```json
$ d }) L. e8 B {: M; H4 f2 j- {- G5 ^7 `, S
"database": {
r, G- E9 C* r "dialect": "postgres",
. o" w6 _5 _" e, _ "host": "localhost",1 n/ e& D/ F" u
"port": 5432,
. [: A# Z( V( E- K/ E" a' T "username": "nocobase_user",
& C4 \/ z+ z( c( F% f* t "password": "your_password",1 V& q' y& I+ Y: D
"database": "nocobase"
7 I; M* c0 |4 R* J }2 x) J q8 q! o% \
}
s1 Z6 T6 s+ g9 r( v' t ```0 @- N: [ |4 _) ]
+ Z! `1 P8 z2 m" M0 ]1 c
#### 2.4 初始化数据库0 c; I; P+ l( N' U8 n% l6 d
- 运行数据库迁移脚本:) ]" @( A% Z1 ]4 l
```bash
9 }9 A+ j# P! s: W! M) C5 h npm run db:migrate0 l0 L! V. L1 |9 q
```6 m: J, j9 }/ o% Q
4 x: S( L0 K% y& O
#### 2.5 启动 NocoBase' B- ^7 T) r I# Q% p! ~, R# ^
- 启动开发服务器:: i5 x+ c' I1 y5 |: i* g! l
```bash
8 d2 y* f X! Z( Q9 e npm run start
& B% ^( t1 s( K' K: {8 P) M ```6 v& U4 H. b* z H% E
- 访问 NocoBase:# l. Y) N' I+ C1 q j& V+ r
打开浏览器,访问 `http://localhost:3000`,完成初始设置。: {4 v9 h6 E2 z( W/ ?; g2 u
* O! P1 Z! z" M4 H4 m+ {# Y
---2 G2 R/ d, H5 U: r- Q* a
/ w4 Y' \ K8 F! j0 y* | _### **3. 集成 DeepSeek**
6 i" ]+ ?# u& i" d, j- ?#### 3.1 创建 NocoBase 插件
1 U3 L* [4 G9 S( W8 s' `- 使用 NocoBase CLI 创建插件:
; T/ T; a) x' `1 n4 n ```bash Y6 t$ K; w" \
npm run cli plugin:create deepseek-integration' @9 B: ~/ E) e' @& V' K7 ]1 N2 H
cd plugins/deepseek-integration
: N, b6 S% K" g* s: Z ```
( {4 U1 |* |) q( ]
$ s3 z, U' Y) q( Z) A" I& g _#### 3.2 安装 DeepSeek SDK8 ^4 |$ Y% X/ [/ j: [/ V
- 安装 DeepSeek 的 Node.js SDK(假设 DeepSeek 提供 SDK):
9 Y- d3 S, o: c* Q8 y0 P ```bash
1 d5 C0 B" d+ m4 B8 x( q7 @. S npm install deepseek-sdk" r% M; U( ^9 I8 h
```5 c) B& W2 R$ G( M" d+ ]) V! r
. D7 v% @- l* F- ]8 w5 s9 l
#### 3.3 配置 DeepSeek API 密钥
' Q1 ^' U& z: k# s4 o5 m- 在插件配置文件中添加 DeepSeek API 密钥:* K7 ?+ `, G% a! i$ y
编辑 `plugins/deepseek-integration/config/config.json`:* K+ [6 E% z; X4 F$ I3 j+ X- n
```json
: L `2 V$ o! y& q/ o0 W3 G {
( B3 O) |$ w: ^! \. A3 b! | "deepseek": {; D4 C8 w j* R: `) S7 W) B0 }
"apiKey": "your_deepseek_api_key"" m" ^! G( {7 G' `2 Z1 n {
}: {5 \1 Y2 D' E
}
! }( f' ^& r$ H1 j% K% c ```' E% m) |( C3 J: f. W; R
; i8 W, H4 H4 q( V
#### 3.4 实现 DeepSeek 服务
+ B7 r7 m# q) a0 Z* c- j4 c- 创建服务文件 `plugins/deepseek-integration/src/services/deepseekService.js`:
# Y* H# ~- D5 \$ G& ]$ C ```javascript7 h9 V1 c7 Z7 g
const DeepSeek = require('deepseek-sdk');& i9 k) R$ a$ V H
, o j6 `% x1 b5 E
class DeepSeekService {
1 x$ M( R* {! Z a- X% E constructor(apiKey) {
) Q k( P- h8 [- V, ~7 A this.deepseek = new DeepSeek(apiKey);9 R2 Q6 _6 M$ O' e
}
/ _* l1 r, q, k, c% O+ ]$ [
" T7 g X4 z3 p& | async query(prompt) {
1 A$ h2 Q) b2 X! `* b const response = await this.deepseek.complete({
" _- y: R0 L9 M+ G prompt: prompt,
7 ?; G+ K7 ^' w/ C max_tokens: 150) h! q9 c4 I% I
});
3 x5 N% h# L7 Y' n3 @ return response.choices[0].text.trim();; `2 Q; B; f, _, ?4 I3 @ @' M
}6 L \1 p; ?! D# h. C7 f, n
}4 A% M# J% [- R5 i) m; q
# s# Q% l( }; n9 q( ]0 L( {
module.exports = DeepSeekService;3 P4 U4 y5 @( A8 X+ g
```
0 @$ G& Q7 R" g3 c8 e
2 D+ j( Q2 C% ]: R6 q/ ]. B; L* ` P#### 3.5 创建 API 路由
9 X* R5 V5 [4 P$ A+ N) i; [4 n. T- 创建路由文件 `plugins/deepseek-integration/src/routes/deepseekRoutes.js`:
/ s8 c5 y2 C& I ```javascript
- s% B7 ~% c+ l/ o const express = require('express');( U- z- M# T! m/ k" d8 h
const DeepSeekService = require('../services/deepseekService');
: _1 l* A; _$ O8 h const router = express.Router();
6 w/ C8 ]* s( B5 \0 ?& ^: K const config = require('config');
) [6 C/ W# | q, Q# A V1 t& j T4 l2 L( ~+ J2 c4 t) [. Y, ~) p
const deepseekService = new DeepSeekService(config.deepseek.apiKey);
% l+ Z8 `! @0 |! k- ] N
5 c0 w* \# I* B, p router.post('/query', async (req, res) => {6 m5 e! w5 { C& L/ F
const { prompt } = req.body;# s' h3 \3 j; U
try {
: B/ L% Z3 y7 ]3 F const result = await deepseekService.query(prompt);
4 }5 e/ Y/ r6 v+ d6 R* m4 z/ k8 S res.json({ success: true, result });4 ]# |3 E& z$ T) w4 t5 |
} catch (error) {! E% g& ?; N* _+ L
res.status(500).json({ success: false, error: error.message });2 c0 M) ^, G W% Z6 g, u9 X+ i
}+ D! G3 w8 H3 l9 J4 r
});
/ h3 }& @) E* z* C/ F% ~2 _4 f% s" M2 ~! m* I0 \2 R
module.exports = router;
4 k% {4 c# C* V ```
5 c' f& m: u' Z) ?" c8 D, M# a# f2 x( W9 e7 u; ^ n- F
#### 3.6 注册插件
6 I. }$ U+ D& u, @. _- 编辑插件入口文件 `plugins/deepseek-integration/src/index.js`:7 f3 s+ R+ E& Y9 @: o
```javascript
* K3 T1 V2 T. o+ X; U# S const deepseekRoutes = require('./routes/deepseekRoutes');, H$ A1 {5 |' R- w7 Y
* F; I- }6 C8 Y% O+ }
module.exports = function(app) {4 |6 l1 x" ]6 {
app.use('/api/deepseek', deepseekRoutes);
4 z; d. t6 |+ A: B. F };
& {( C5 e, T# x* P ```$ l- D! @& V, H! \
8 Q% |; n4 `2 \; Y3 t---; y- ^* f' S3 J+ G6 M
/ t* U+ k; n/ D& H6 K### **4. 测试集成**
6 D% a# ~" f& ?* ~$ A#### 4.1 启动 NocoBase6 K, @$ |1 M" i2 S# F* a
- 启动 NocoBase:4 V9 P4 ~# M) E+ l, u$ I: [0 {- H8 n5 _% u
```bash
& e4 {! M! W" u; V9 e! u6 Y1 s npm run start
, d8 `4 U. ~$ S) i3 R) N ```% i, y1 G/ ], ~
. e* [1 ^3 {8 V+ b' t
#### 4.2 测试 DeepSeek API
4 l2 `. {( I4 p8 t! Y: i- 使用 `curl` 或 Postman 测试 API:. [: c# _/ M) s1 m/ C
```bash: Q; g" ]% a( r7 C
curl -X POST http://localhost:3000/api/deepseek/query \3 ]5 h. K+ O/ R d
-H "Content-Type: application/json" \' o v" }* T2 X
-d '{"prompt": "显示最近三个月联系过的客户"}'6 T9 W w9 M: Y( Q. Y
```. L5 u$ v+ Y' } ~. E Z8 E
9 m) p( w+ u) |* F, q! j' E
---
% I- ]$ A& L2 G* N! I) Z
, e8 u; v+ M% [3 J: Q! d5 \### **5. 部署到生产环境**
* i% A: b, a8 t |" Z" n#### 5.1 使用 Docker 部署
" X4 a e" R+ }9 u( C6 N- 创建 `docker-compose.yml` 文件:
- n- \: r" l4 {9 y' V1 _$ W ```yaml! e4 G* |6 ]+ B9 V' J
version: '3'
( b: p, r0 _- p& K4 z services:
* h: w7 b# Z/ P& n app:
; a- S: C, N* m l8 D image: node:16 N* v& q9 w& E! J2 i
container_name: nocobase
0 H: w0 l6 |* S: E+ S working_dir: /app0 S: w/ Y& W0 b+ P+ _) a
volumes:* B5 l+ `1 v% f0 }+ c9 Z1 b+ ^
- .:/app7 C* e& l$ J% n1 q; j$ b
ports:; ]7 c/ M# F( q* \* O; o
- "3000:3000"1 _% Z1 T) V- G7 k' e) D6 V' L) d! a
environment:
4 }" |. y) p- L# a6 ] - NODE_ENV=production O/ |! N: p- h/ m# [; ~+ d
command: npm run start
) P U( _( U2 f+ ? db:3 s6 ?7 K! L! y: ^6 ~9 z( P
image: postgres:13
' N* S1 h* _2 C& E# S# W) d container_name: nocobase_db
5 Y; `0 O# T* x environment:
' ~ n2 c d5 a7 V POSTGRES_USER: nocobase_user
1 M' V( X) [- L3 c+ P, { POSTGRES_PASSWORD: your_password
. L Z* `$ K$ L8 x8 z; { POSTGRES_DB: nocobase
* T/ l/ {2 U) D% x6 A& h& R$ b ports:
3 B2 s8 S s w" _* k - "5432:5432"
" N8 C6 @8 ?" B ```/ P% W! m+ \& q0 A0 u
- 启动 Docker 容器:
% j- X5 S# f" b$ y, v# M ```bash! {2 J+ e) P$ C8 l$ Y
docker-compose up -d' d& B0 b' Q3 g/ a8 m) x& a# c
```
# }! C9 j$ I; x3 i5 J, T+ n4 J! d' T3 c
#### 5.2 配置 HTTPS
; g9 C7 |: ]2 d/ J. m8 B- 使用 Nginx 反向代理并配置 HTTPS:' k) q( W, v+ |% M
```nginx+ Y/ w4 g6 h7 T
server {) |0 ~8 f: U* B1 F; _
listen 80;
: H1 r) g9 ~2 v& y; b. | server_name yourdomain.com;
& R, ?7 C: d, n9 | return 301 https://$host$request_uri;9 }$ m2 h$ a9 ~! d+ `' J9 A
}3 B: d/ o5 R$ H- P' O: c3 Z+ W
# }5 X% n% ]) @3 N5 q- E" Y server {
% n$ U6 u7 E. u* y0 H listen 443 ssl;% k- K5 p6 {8 p3 N6 J
server_name yourdomain.com;
' _5 R" }6 i( h! C0 N' Z c2 c# P* b L
ssl_certificate /path/to/your/certificate.crt;% `9 ?$ }) {* P! h# _
ssl_certificate_key /path/to/your/private.key;
' F0 c# U4 g s- G4 S
% y% E/ c# ^- s' m" p location / {2 n$ a8 W* G6 z: g
proxy_pass http://localhost:3000;
: y4 U$ x' o0 I2 p! h proxy_set_header Host $host;
: z& L6 D) P, t proxy_set_header X-Real-IP $remote_addr;
1 W3 U8 a3 U- ]) { }
& l. G4 f+ \: C1 j. U3 k }
+ b& O) ]* w! n" ^' E$ g5 T ```6 [1 p6 s5 u9 o
5 n# U) B i7 j9 q---
3 }! b* u: q4 F$ x$ V+ p( Y% [
### **6. 总结**, @5 i; y2 G' Q3 M V$ n1 X
通过以上步骤,您已经成功将 DeepSeek 集成到 NocoBase 中,并可以通过 API 调用实现自然语言查询、智能建议等功能。这一集成显著提升了 NocoBase 的智能化水平,为用户提供了更高效的操作体验。 |
|