|
楼主 |
发表于 2025-1-24 13:35:19
来自手机
|
显示全部楼层
以下的操作步骤,帮助您将 DeepSeek 集成到 NocoBase 中。( G: g6 ~0 g$ F4 O' x, N$ e2 m% q$ j
% t- o7 L2 m: p- Y6 U% ^/ n2 Y我们将从环境准备、NocoBase 安装、DeepSeek API 集成、插件开发到测试部署,逐步展开。; c- v8 P$ i9 Q* x: {4 \
3 O6 N- e/ ?7 p) n$ t+ ?8 T) G
---
& _* [1 I# Q+ j' m8 x; o- g) [/ s# B' o, l: z) K6 \
### **1. 环境准备**5 K( U2 K( j6 U/ Y4 R6 L' i8 ]
#### 1.1 服务器要求
/ @* Q/ q( I( R$ V+ k! \' K- **操作系统**:Linux(Ubuntu 20.04 或 CentOS 7)或 macOS。# u8 M$ t6 d3 H, w% n; g* U
- **Node.js**:安装 Node.js v16.x 或更高版本。
) N9 c4 \5 D. R/ ~9 h0 ]- **数据库**:PostgreSQL(推荐)或 MySQL。& W6 e0 L, `6 R4 C# A0 u& H
- **DeepSeek API 密钥**:注册 DeepSeek 账户并获取 API 密钥。
! c0 C- c* ~- h5 S5 {9 B" m+ D3 C/ o" q# I E
#### 1.2 安装依赖
& t v2 T; U1 [7 j- 安装 Node.js 和 npm:' q) a( ]! O) p1 y8 t6 s
```bash3 o! j' n/ W6 R) e% `- L; P
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -3 l0 P: j% z6 b* c+ u
sudo apt-get install -y nodejs4 a; o9 h8 r3 y$ h
```3 l! p. E( K# E- e! C4 \
- 安装 PostgreSQL:
. Z6 a3 \9 X3 _3 ]! B7 M* v ```bash
# o/ ?. ?/ W' N+ a* h sudo apt-get install postgresql postgresql-contrib+ o9 S+ |( Z8 B5 D. L
sudo systemctl start postgresql- h) c# ~) \- i- m0 @0 E6 n
sudo systemctl enable postgresql
- C8 m1 S+ K% {2 Y% Q" ? ```
# w k: O2 s1 g( b; E% z
0 E) x( [. b& d) a/ d/ _---) M3 \: B# M/ P) k& q% B1 \9 }
U( U& a1 S& h9 Y" g! Z$ @### **2. 安装 NocoBase**
& j9 Z, ]/ u8 l1 H. B#### 2.1 下载 NocoBase( w$ [" d1 k9 n% s) y: u: V
- 从 GitHub 克隆 NocoBase 仓库:
/ U3 j9 T, H& d4 [; K ```bash
9 ~/ e3 P. a0 H2 k4 e- @ z2 F git clone https://github.com/nocobase/nocobase.git/ z' w! j9 F, J- W- _* l
cd nocobase
; E% ]8 \) ^* C; I: w: [ ```
- K( ~1 K o! @. ]) W, \
4 e5 [6 D9 e: x" U#### 2.2 安装依赖
$ D( ^& `( P u+ F) i( ^- 安装项目依赖:! W4 Y& \- V' d- }+ _! }- {+ ^
```bash
$ `$ P9 K) y# C' e" b npm install
5 Z1 R# u- a( K l ```) z/ d) w2 T: t \+ ?& Q
* ?- p3 N6 p, L" y& s#### 2.3 配置数据库0 s# V* P7 p ]
- 创建 PostgreSQL 数据库: r5 {$ P! ^$ S% w
```bash2 X7 g" r2 h, P! a$ ]" ~3 ]6 x
sudo -u postgres psql
; o- h6 z" y4 N P! o) J CREATE DATABASE nocobase;- H' J" ^( n( g7 N; {
CREATE USER nocobase_user WITH PASSWORD 'your_password';1 u3 y3 k/ V: i
GRANT ALL PRIVILEGES ON DATABASE nocobase TO nocobase_user;
% G7 Y% u8 N6 b6 _- z \q9 I! }" E* b- B5 Y6 j: [
```; i2 j& z" I0 Q8 n9 `" R
- 修改 NocoBase 配置文件:
c5 J$ n3 Y1 U7 ~ 编辑 `config/config.json`,配置数据库连接信息:' ~* w$ ]) {& h! e, \) G, |3 _
```json+ b: P8 E( k; h, @: ^6 a* X
{
( Z* C7 R& ~6 m& k "database": {/ |7 Y5 h; ?( x7 j' f
"dialect": "postgres",
: V9 f9 J. _0 n4 W% k "host": "localhost",
; U2 { A; m% U0 n "port": 5432,& X, i5 E/ \3 T0 b2 }
"username": "nocobase_user",
; H9 C: A) O7 V$ {5 i! [ "password": "your_password",
- K' E# K2 f1 f' v0 M "database": "nocobase"
# N! I2 V2 E1 e5 n/ {1 W; U: B }
& z7 h$ M; ^' L- \; e h D }
# N0 j S4 a. K% w ```! {* e. k4 W$ I3 I! z
) ^$ ~- p7 R4 e
#### 2.4 初始化数据库
4 A8 [! H4 o9 n9 X- 运行数据库迁移脚本:
, ?' O6 V H8 a7 c5 R8 b ```bash4 H( e" Y1 o" C/ Q5 |
npm run db:migrate
% n. F$ n- {7 w ```. q) K1 l8 c" ~0 p/ V
. w S. V& R+ s+ _+ R5 {. ^#### 2.5 启动 NocoBase
7 h$ d: G& Q; k+ U* u+ J8 p! D- 启动开发服务器:
) Q) F! F- S2 S! w9 p ```bash
- Y3 o5 ?3 x! k1 Z) Y) x npm run start J* [) A0 n7 {) l1 L, c5 h
```
0 `4 a; U" @, N1 i: M- 访问 NocoBase:
/ O5 f8 S/ f" }' K4 f8 \ 打开浏览器,访问 `http://localhost:3000`,完成初始设置。' Y: D2 m% r/ _3 J* C& V5 s
& b3 F9 i5 K* R9 [ p2 ~! I. T& ?: G---# s$ y6 Q# a5 i7 M. q' W5 x
" X) f! c9 v3 n- k% E: ]8 S; g
### **3. 集成 DeepSeek**
# B# o' F6 P3 X0 H+ T/ Z9 c#### 3.1 创建 NocoBase 插件: A/ v5 a/ }8 D' k, q
- 使用 NocoBase CLI 创建插件:
9 w; @& _3 }$ Z0 l ```bash
8 m4 @" _' p U: G+ G) ] npm run cli plugin:create deepseek-integration- H2 T/ g" N& z9 m( c- d5 _0 K" W
cd plugins/deepseek-integration5 a+ N' X; P- j" l
```
( R1 N$ w: E: I* \. Z6 X: e
. t0 L+ _3 H5 @3 m+ N- I4 p#### 3.2 安装 DeepSeek SDK$ } \: I$ r% d' S2 \ m
- 安装 DeepSeek 的 Node.js SDK(假设 DeepSeek 提供 SDK):$ y/ [& N, Q7 `" q1 l
```bash+ f, P" ?. N$ x; g% F. q
npm install deepseek-sdk" ^" X1 p0 Z) p* R+ p* l6 F3 T
```. a! G2 j+ _* N6 A1 f/ @
/ k& t0 r; z1 K% `6 i
#### 3.3 配置 DeepSeek API 密钥
. L4 P. V. a; @5 f) F: {+ x9 m- 在插件配置文件中添加 DeepSeek API 密钥:# G! H; @% h' w; v- b7 ?& A- O. [
编辑 `plugins/deepseek-integration/config/config.json`:
/ Q' u" t$ H; @( y; w. @ ```json
& J$ J T& g: F7 C5 s {
: s* r4 ?: m' s "deepseek": {
0 t o A# G+ k9 y* f "apiKey": "your_deepseek_api_key"8 S0 {# O+ U5 {5 A0 a7 I6 F
}1 ~+ S9 V3 j* H" y: o6 D
}6 P0 y r5 W7 {; Y
```: m" e0 l) P" m; |7 \
z ^' S+ {" z7 r; v* O" ~* e0 }9 \
#### 3.4 实现 DeepSeek 服务1 m a8 ~* A. F: S
- 创建服务文件 `plugins/deepseek-integration/src/services/deepseekService.js`:, _% v- U" r9 @) ~
```javascript" k5 o. U0 O, [1 c2 \! ~" W
const DeepSeek = require('deepseek-sdk');% _4 J( l+ _( x! R
1 i- z) ?) M% G! f* z+ |" }* e6 r
class DeepSeekService {# f; |# d+ S; c1 ^" g
constructor(apiKey) {: m" r! f: y0 ~
this.deepseek = new DeepSeek(apiKey);
5 L5 c8 ^0 r: y8 v) Y8 Z }; P, z; X8 A. x0 h- I% S
* [1 F1 P, Z" |1 g3 F
async query(prompt) {, ]: z( r* K2 H; C& p- D
const response = await this.deepseek.complete({- N! e3 ?* m. h* E2 l
prompt: prompt,) {5 l" v# Z, q% \6 Y- a" I, P
max_tokens: 150. [# H" Z3 o$ Q$ Z" w% W
});
3 E/ U, t. L& l7 m3 A+ v return response.choices[0].text.trim();
8 n8 U8 K; r# u0 M, K* V- _ }
) P) o/ S9 s) Y }( B3 ]2 v4 n5 x2 `8 q8 [6 v+ T2 W
3 E- P6 e7 ~, Y( K" {4 p4 u. D
module.exports = DeepSeekService;
* R) M% ]. x5 [% L% O ```/ q; t" K& M9 a
, \, o2 I+ U, l#### 3.5 创建 API 路由
Z4 E! S! s% t X* a1 Z# _- 创建路由文件 `plugins/deepseek-integration/src/routes/deepseekRoutes.js`:
4 B5 m6 ?: P5 \ ```javascript4 m! V. B; t/ I! ^& f
const express = require('express');
5 F5 [/ X( z2 F! o const DeepSeekService = require('../services/deepseekService');& i: U8 V. Y3 _+ ~3 F( p: p% p
const router = express.Router();
+ o/ w3 a$ q4 {/ D) {4 u+ m7 Y const config = require('config');
' o9 ?% G e) q
# }6 j3 B: |5 y const deepseekService = new DeepSeekService(config.deepseek.apiKey);1 E% m/ ], f0 S0 i @8 K
$ p B5 ]! Q6 {/ s' G router.post('/query', async (req, res) => {
6 _2 Y" T: }" | const { prompt } = req.body;
& B" ~& \* `- Z try {
& i9 ^# e6 V( C5 y; z! q const result = await deepseekService.query(prompt);
, s/ d5 y5 J/ Q2 U9 G P3 V; W1 N res.json({ success: true, result });( c4 @( I! b' q' G" @
} catch (error) {% ~$ C8 l k/ E$ p
res.status(500).json({ success: false, error: error.message });* d6 ]! l! n' \: N
}
+ i" d" ]4 y, k5 Z });
+ V% O* M$ v+ @1 L$ Z+ G
8 B- Q0 ~; v5 l0 ]6 q2 C module.exports = router;* C8 J/ O# m8 p( q
```
) O# `" B3 F3 j/ k5 P. |0 l7 Q4 Y. b
#### 3.6 注册插件
1 R1 v4 q7 L' Q+ P% D) u- p- 编辑插件入口文件 `plugins/deepseek-integration/src/index.js`:
" a u \! y/ e4 \0 l) {% k% D ```javascript
O1 B! r+ H# P const deepseekRoutes = require('./routes/deepseekRoutes');
; q& c2 b/ S0 c3 B: J# W/ F; n3 K- b2 s" j8 I
module.exports = function(app) {/ \8 y& K( I; a' f
app.use('/api/deepseek', deepseekRoutes);. ?, n1 s2 X9 D
};
0 I3 A2 K1 X8 w5 i6 t& \6 y! C: ^ ```
3 P0 S& @* X- j3 Q' E' p& h/ E0 E8 Y/ y4 \
---& C. c+ `5 R; G5 J
4 D5 d5 y+ {9 ]( a# I. T6 @1 O3 Y### **4. 测试集成**' b: p1 l+ s' o w5 ^ U
#### 4.1 启动 NocoBase
* _, w' Q3 Y' u' Q/ o- 启动 NocoBase:/ z6 f8 m! b5 ~! |7 J. N4 m
```bash
" ~( l( t# I% t% r3 L* x npm run start
# {$ R- D; \: |7 S6 s' _* P* w ```
' ]! h; V* j! `
0 D/ ?; H( y* h+ _* p& R2 Q2 N: x#### 4.2 测试 DeepSeek API
- f7 s% N1 Z; f8 }- 使用 `curl` 或 Postman 测试 API:8 u. e x9 H; R% T7 Q: H% Z8 S
```bash
7 l+ J4 J( p7 U1 {* Z& D/ X curl -X POST http://localhost:3000/api/deepseek/query \ b- u' p9 W# @, R5 d& a
-H "Content-Type: application/json" \1 u" H( x3 K5 b6 H) S
-d '{"prompt": "显示最近三个月联系过的客户"}'
+ U1 ]8 e# t D5 W, h9 I# l* c ```
" H# @7 b' E$ }1 Y @; M% b+ a& K! O x* T7 R% s: H
---0 T: H; i4 U2 r3 H J. k* O
% L1 { }. Z1 ^( T6 B5 t### **5. 部署到生产环境**
; x. G, |: ?' Q#### 5.1 使用 Docker 部署- r* o! @! P" q
- 创建 `docker-compose.yml` 文件:
0 K. }/ v& ?4 G# A b7 n! p/ T: } ```yaml
" ?, L/ A. H4 _& J+ J version: '3'
) S, g1 n0 _6 D; ?- x services:4 Q: E2 |8 d! Y
app:
& V, J) h/ i5 ^$ B+ T image: node:16
2 A4 Q' t! ]1 }! B' T5 |$ r+ j Q container_name: nocobase5 T+ ~: H1 Y+ i4 B5 Q3 M; }4 o
working_dir: /app1 f$ { E! D& k' b9 x
volumes:
L' } _; n' H6 X6 r - .:/app) d) a8 @7 _4 r2 ^
ports:
0 R2 T) P5 N* K& W3 U - "3000:3000"( g& |; P4 X3 ]
environment:
3 ~% Q( V9 n# h0 O - NODE_ENV=production& X- [4 ~( E* F8 h# X& S( a5 m) |
command: npm run start2 `( l' W/ M n; K' t5 {
db:6 r+ s, w6 @/ l* `6 [, z3 X' t' ^
image: postgres:13
5 y2 [* A" f2 y* ^ container_name: nocobase_db
' V F5 f; Q" C8 \, F+ N environment:, s: B, M. m& L6 q2 I
POSTGRES_USER: nocobase_user
& r N' j: E& P+ `9 ^" r1 u8 Q X POSTGRES_PASSWORD: your_password
- E9 h# L3 J/ L5 X POSTGRES_DB: nocobase
5 c- Z% R+ m4 }8 I# H! w ports:
# W8 k4 F1 m1 D - "5432:5432"
) }1 v4 U, w* l* e% K) c ```# E7 j8 O X$ q% j
- 启动 Docker 容器:
/ H! T& Z% O: X ```bash
2 ^( A: I5 V1 p3 R docker-compose up -d
' w5 _3 `) y! c; d' L, O ```
5 z& F8 Q/ m! F/ H5 K- `+ N( b- k8 A, W
( U( [7 M) n) [8 v: z2 d# q#### 5.2 配置 HTTPS' f' ?9 {2 s7 o% S Q
- 使用 Nginx 反向代理并配置 HTTPS:( H7 {- A* @+ D G- E
```nginx
' O& ]1 W2 K$ `& W server {. k& R8 h- x9 W( d; B U1 f8 O# O
listen 80;
2 w0 j4 |1 Y4 G- B- z' z; Y. ^# j server_name yourdomain.com;. q% ^1 D# i$ s3 a& d
return 301 https://$host$request_uri;
" e: I; D/ c- G! G) a! l/ L }$ g* ~5 L: U9 h& `( ~! c
% @+ o8 @ q+ F( ~ u" ^ server {9 p9 J& O8 [4 k) g, v
listen 443 ssl;0 c1 y0 T/ J# [' ~/ z7 k1 N5 \
server_name yourdomain.com;5 s/ V4 q/ \! y( x! J
9 T/ A6 G# P3 Z- I1 }: G- t1 S# N2 u ssl_certificate /path/to/your/certificate.crt;
1 [2 |+ m$ H0 b0 M/ l7 F ssl_certificate_key /path/to/your/private.key;
2 V9 J6 ~5 m; g; ~
- q0 }! Z/ L; K+ L& Z location / {
3 `7 D5 A0 O4 f' g7 G proxy_pass http://localhost:3000;! [0 J9 M/ B2 D8 t3 G. I6 q
proxy_set_header Host $host;) q1 ?( E' i- F: Z! v$ G( ]9 f
proxy_set_header X-Real-IP $remote_addr;
, D3 P: b3 d R' `6 u5 f }
& T/ w& O& _6 v1 i# N }
# W- O( Z/ V+ K8 X ```- t }; Q+ q2 R$ l, @
# V3 X0 s5 R4 V: v- @; o
---) a* P( b& x9 `+ M6 v. \: t
X- g) M w9 X* b4 u
### **6. 总结** N$ K; Q" a* [1 |, c# k
通过以上步骤,您已经成功将 DeepSeek 集成到 NocoBase 中,并可以通过 API 调用实现自然语言查询、智能建议等功能。这一集成显著提升了 NocoBase 的智能化水平,为用户提供了更高效的操作体验。 |
|