|
楼主 |
发表于 2025-1-24 13:35:19
来自手机
|
显示全部楼层
以下的操作步骤,帮助您将 DeepSeek 集成到 NocoBase 中。
8 |7 V% r0 `! n U7 F4 d
. }/ I3 q3 [* v; G) M我们将从环境准备、NocoBase 安装、DeepSeek API 集成、插件开发到测试部署,逐步展开。
; K1 X; {& K* `$ y0 a. J: `
& m4 D, ]+ e( h ^3 @6 h, d. t$ G---. p% p, T, S- X# c. d: T
8 l# d0 \4 h6 B5 Y- b### **1. 环境准备**9 G3 ^+ {0 h9 z7 K3 l5 O9 J
#### 1.1 服务器要求# [% _& {* A8 n3 P. u
- **操作系统**:Linux(Ubuntu 20.04 或 CentOS 7)或 macOS。5 x0 ^" l3 q' j# o2 z/ K
- **Node.js**:安装 Node.js v16.x 或更高版本。6 d9 C& Z2 h9 B. Q' m! s! s+ \0 O
- **数据库**:PostgreSQL(推荐)或 MySQL。( w! J- `3 H6 U1 `% J3 S" M
- **DeepSeek API 密钥**:注册 DeepSeek 账户并获取 API 密钥。
4 \' H1 [9 s+ [2 g6 b. Y5 z- ?4 a1 E, r$ j9 p- q, I4 C
#### 1.2 安装依赖& X2 j6 {/ Y* r% N
- 安装 Node.js 和 npm:- }9 p9 E# [% h; t. Y( g& K
```bash
; q0 C% u$ s. s curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
: e/ ~+ f6 f: F" v5 Y; n sudo apt-get install -y nodejs( D& `. t; c! W+ i, i& t
```
/ x, E3 o+ x0 `! R# _2 {$ j; q- 安装 PostgreSQL:
( Z# S0 e! D" w1 f% o2 | ```bash6 C+ K D+ {9 F: A% h. N, z
sudo apt-get install postgresql postgresql-contrib: Q' a1 y$ A6 M* T
sudo systemctl start postgresql2 Z L; Q ^8 J) r
sudo systemctl enable postgresql. X4 Q" l, I/ u8 s+ G
```( S% ^4 M& l( @- Q8 s* C
1 `9 c, b0 o7 q---8 q# K! n( X) ~
% r- _9 c, d, W9 ]
### **2. 安装 NocoBase**
6 i, f; u+ b0 J6 ?' r2 Q9 S#### 2.1 下载 NocoBase, U! |! l. h6 D- f2 C5 r: N
- 从 GitHub 克隆 NocoBase 仓库:
, b2 T! s* J" G% m) B+ [ ```bash
2 Z4 h9 g: ~$ _7 u- @) q" Z# t1 I git clone https://github.com/nocobase/nocobase.git$ \4 x% o& O- v
cd nocobase
1 b- S7 b) D0 M! z1 j8 {* }- S. u+ h# Q ```0 ?; ~- s) V1 O ?# v) F% C& m f
5 b! }; N C- P. a
#### 2.2 安装依赖* ~ T, H. h1 I4 e
- 安装项目依赖:
* p1 @9 Z5 [7 { ```bash
: g6 h j. F$ O; i6 L9 [0 S npm install
" S& e" D* t) ~9 C" d ```$ z _% A- K6 j. R$ p$ U2 S
5 y6 U6 q& w! l3 Y ^+ K#### 2.3 配置数据库 L* U; h( W) {9 J
- 创建 PostgreSQL 数据库:
$ A$ m3 V2 m3 H' ? ```bash5 T/ I5 O4 o, v: U! q
sudo -u postgres psql
! g& Q+ e7 ]- K" u3 E/ [3 y CREATE DATABASE nocobase;
2 u/ N+ E+ B& ~/ C2 W. G" [ CREATE USER nocobase_user WITH PASSWORD 'your_password';2 i/ f0 x4 S9 Z9 G# k, M" {$ Z. J5 |
GRANT ALL PRIVILEGES ON DATABASE nocobase TO nocobase_user;. ~+ @$ [% y: f- l, R5 e; ]" O
\q6 U/ X+ {7 _% C1 q: w6 [
```
0 M) F4 v, D3 k, R# b- 修改 NocoBase 配置文件:& q9 ]: ^) C' W1 r. a- a3 \
编辑 `config/config.json`,配置数据库连接信息:
2 e$ w* P0 E+ }2 c7 m7 n ```json J- V( _6 `5 m) {
{
: S8 w$ ]+ E0 _" z "database": {
* U6 F' s5 q- G. z W" o: j# P "dialect": "postgres",, R1 z2 U. H& n
"host": "localhost",0 m0 t1 A$ {7 o
"port": 5432,
. e9 N" G5 x u" ^. }8 S& g g "username": "nocobase_user",
% Y9 r! y( H' H% P2 E- {, ~& R "password": "your_password",
4 ]* a1 Y# ?6 F "database": "nocobase"
1 T. g! _" p. n9 W. F }; w, R6 t1 S) p; ]
}
7 b+ _3 U9 q8 B# h% T ```
6 X, R0 y6 }# v( A# e5 M) }0 z) R6 d, }$ K2 h( j
#### 2.4 初始化数据库
! n1 ]5 Z8 a: a( U- 运行数据库迁移脚本:
8 e$ { C& D3 ^3 B) I ```bash2 c0 i4 `0 J# c6 w5 h
npm run db:migrate. T% t8 R6 g, c# k; R
```* F- ?+ _* g! K; ?% L
3 A0 I) E+ V: A& G' V#### 2.5 启动 NocoBase
4 C- z7 M, _ n, u! O- 启动开发服务器:% a* }* y$ X& w/ W: p
```bash
' ?& ] l; P* O1 D9 M npm run start# V0 J3 n4 }4 M& Y8 f) f
```- e5 G, W( F( N; z5 |
- 访问 NocoBase:
) k& O; e2 U8 v, Z7 E! M# w1 `6 S 打开浏览器,访问 `http://localhost:3000`,完成初始设置。9 ^' T) z+ Y0 w+ L$ O
' u& ]5 H4 S1 a3 s, ]---
. p4 H% j1 {7 c1 ]) P a8 z& k2 P0 h: d% H* q4 e- ~" L
### **3. 集成 DeepSeek**
6 S0 [3 u4 [3 y2 |) {#### 3.1 创建 NocoBase 插件
. U5 g( z. b; N* v% K- 使用 NocoBase CLI 创建插件:4 _1 J$ A7 g9 w n$ @- H8 k3 F) x
```bash6 i$ {3 s0 V$ P7 h4 Y! \7 T; Q
npm run cli plugin:create deepseek-integration
' y! N) X' U1 l, V7 T cd plugins/deepseek-integration+ M: s& R; s- ^) e" D8 A
```: h+ I1 w, A$ z9 s0 w* X' p" I+ R! [
: T3 N, W& s5 c
#### 3.2 安装 DeepSeek SDK
( U0 x" u5 d2 X% l- F' V6 N- 安装 DeepSeek 的 Node.js SDK(假设 DeepSeek 提供 SDK):! ] B+ N1 m; P1 S. P3 d6 p
```bash; e5 H% y# v0 C2 l) V; R1 f
npm install deepseek-sdk" n( J" O6 O8 G- Y2 N* @+ \
```+ b$ D* G/ \7 K6 K" S+ v
" B0 N4 g; v" O: h; H#### 3.3 配置 DeepSeek API 密钥
; I' ~! H3 O# J& D1 |! c- 在插件配置文件中添加 DeepSeek API 密钥:" `3 x! R( g V6 S8 d1 K
编辑 `plugins/deepseek-integration/config/config.json`:
" b, h; G K- [* D. |1 s: ` ```json, Q! _ B0 E6 N4 c
{, \7 Q; W1 a* D! U+ k
"deepseek": {( i. C. o& z0 Y0 j* L
"apiKey": "your_deepseek_api_key"
* |5 ?" m0 D9 D- O3 m D }
0 N: a5 X' v( I' y }2 N) _3 w& n( v3 \
```2 f2 z# d' {' @1 w
: m( b, f$ \6 j/ y5 D3 X) G+ E#### 3.4 实现 DeepSeek 服务
8 A; I$ w" G. g& i o" w- 创建服务文件 `plugins/deepseek-integration/src/services/deepseekService.js`:
# X! O6 _! B- b; Z0 N; I ```javascript
; q; N' {& a% X/ b const DeepSeek = require('deepseek-sdk');
4 t3 |4 p \, N/ L
8 F2 [3 t/ ^. Y/ \8 u3 T. V# B class DeepSeekService {) }( |+ w. o+ |5 r1 u0 k
constructor(apiKey) {9 D. h; L" A3 j2 p+ u, s
this.deepseek = new DeepSeek(apiKey);
& |8 z9 `+ i) O/ A; x' v+ _* P } X+ w- A8 V4 u' r7 y5 ^
9 U5 g1 P) @: B9 b
async query(prompt) {# W& X1 l8 h! i
const response = await this.deepseek.complete({. z+ g: x3 y% E/ C% [% E
prompt: prompt,; P# }" ^$ V) B6 P9 U. O h
max_tokens: 150
$ m5 h& h. t- Z N5 c- \ });) N) ?; G3 ~& s, h3 n, b* p
return response.choices[0].text.trim();
% x9 b! ?5 }; N# ^2 f- W4 B }
* o6 a/ v" t* Y7 I }
1 _/ |' @, u6 X1 A& @& D# {
9 k. } S- ~" j; G module.exports = DeepSeekService;
0 \9 d8 V; d6 l- X) S% V ```' o4 c0 l' t6 k/ U2 \3 P% W% q' k
7 |1 o( a4 ?" c+ e- B) A# [" C
#### 3.5 创建 API 路由
& p# s% c& X! h) O2 l- 创建路由文件 `plugins/deepseek-integration/src/routes/deepseekRoutes.js`:! c6 p E# s3 |" d6 J9 x4 T0 z
```javascript
2 i' v7 Q: h9 a! i" v const express = require('express');/ G: ?( x6 S3 v6 y4 j
const DeepSeekService = require('../services/deepseekService');
% P2 O5 E, _; G" X const router = express.Router();9 g( ~0 g9 Z4 l7 K7 j {( [
const config = require('config');
7 K @7 N ^: `1 b+ q) x H% T5 h# y
const deepseekService = new DeepSeekService(config.deepseek.apiKey);
! X* @$ v0 f' O2 P+ q+ G/ ?- x5 T7 W, f* }. Y4 {2 E/ ~2 {
router.post('/query', async (req, res) => { ^4 D/ S5 K: f: y
const { prompt } = req.body;
2 h# D! ~' B8 @: @0 I5 ]& [ try {
' | k1 h5 G9 ]3 V k const result = await deepseekService.query(prompt);$ i8 b [( v8 A' S% L
res.json({ success: true, result });
# N6 F- Q/ z1 Q% y+ Z1 l } catch (error) {
5 W- \* {( I& i o* ?" \: _ res.status(500).json({ success: false, error: error.message });
4 P/ x4 b, x9 q8 K7 n: C4 ?* u }
0 K$ d$ A3 D/ P) m4 C: t3 t& C4 z) u });! q* D/ E: T9 A$ x8 R
# U* C* _2 e8 B) ?5 l module.exports = router;5 X6 I* {1 m# E5 n) |9 u+ W; }
```
, A1 |. d- b2 M. w" X- o: P( |$ V2 ]) S! b
#### 3.6 注册插件5 `+ [, Y8 `2 J) x8 _; y
- 编辑插件入口文件 `plugins/deepseek-integration/src/index.js`:
; \3 i$ C3 b- ?* |7 U, p9 Q0 t ```javascript, G# m6 {" @% P% w+ B: u6 g
const deepseekRoutes = require('./routes/deepseekRoutes');
% }6 c1 S+ A9 E0 x( W4 |3 g" x# n+ s, F! F6 J$ C
module.exports = function(app) {
( R0 g6 ^: n% Q I app.use('/api/deepseek', deepseekRoutes);" h; J$ Q$ @0 Z0 {- Q( ?
};
$ ]$ t4 C* d, n ```% a, [, o. Z) N) m6 C, g
1 q( d- i) [5 r9 g2 g8 Q5 V
---
1 K0 ~' ]# x6 ?+ h4 b
! j3 A9 Q5 e3 {# B9 l# J### **4. 测试集成**
' Z( v Y `# o#### 4.1 启动 NocoBase
4 H# c. h3 i) c" ]0 V1 F. D: F- 启动 NocoBase:
+ _: b: J- E" t( W9 T ```bash
8 K& g, l1 S# t) ?- R, i+ i% U9 d6 K npm run start
" w. {7 }- {3 y. l* V9 j ```( A S9 }$ ^$ \; v4 i0 e$ ^% f
; Q- K$ y2 b2 \4 J
#### 4.2 测试 DeepSeek API$ D; C- G% \0 }) h. a0 o7 x
- 使用 `curl` 或 Postman 测试 API:) i/ i+ t# S7 s0 e# n3 }
```bash0 o- S6 |" U) L
curl -X POST http://localhost:3000/api/deepseek/query \$ v- L) [! S8 @/ \. Y
-H "Content-Type: application/json" \& M+ j$ a" ]- B- ?" F# h! r
-d '{"prompt": "显示最近三个月联系过的客户"}'
p2 f3 b6 I2 b, p, M% W/ }+ o/ T ```
/ \6 I* v. h2 \5 I$ C; @
; K- _! k. n; i5 V0 C8 j---3 r' V* t( d- c
) b8 y, Y7 e7 S& f+ H### **5. 部署到生产环境**6 R) ]# j. ^& `: m; W7 e: X+ H
#### 5.1 使用 Docker 部署1 c% Q0 j3 V9 J3 v1 f6 b
- 创建 `docker-compose.yml` 文件:: _, U+ C+ T8 B1 ?& S) }
```yaml
/ k( G; F, i. E4 e3 V version: '3'
; l& X; \! F/ [ services:
* f' z4 u' x) K7 k+ `' K |' { app:0 x E1 w. x0 @6 f- }4 `
image: node:16
! R) N' W- h; u, z% V container_name: nocobase
" p: \1 r$ i7 c4 K* ~# U working_dir: /app0 ?! j' i/ F3 T: i* U& @3 i
volumes:8 ?; a* ~: \$ \) L! b0 Z
- .:/app+ Q- Q3 ~2 [8 {6 \$ l
ports:. F5 I; v% ]9 Q/ }" V+ M5 Q
- "3000:3000"
) j" G4 [3 {0 Z! s* u( j# Y: R* g environment:
6 e: w4 H/ _ k4 y! C - NODE_ENV=production: p* m" z: h/ D, U/ w; s$ t+ C
command: npm run start6 `( G( ?& g. H7 j/ M8 V
db:5 W6 T% O i5 m- U
image: postgres:13+ B/ L+ |" ^' Y: S6 h. m8 v" q
container_name: nocobase_db
* L- b4 d( ]8 E4 J- o environment:7 a; Y. y' ^0 n. p$ p3 w6 g
POSTGRES_USER: nocobase_user
$ O0 g* l D7 _* t- @5 S* A POSTGRES_PASSWORD: your_password. m5 F7 }+ ]3 \; N
POSTGRES_DB: nocobase" v2 Q: w, ^0 Q5 L9 T2 d7 |: I1 n
ports:0 m i8 ]: n" w% [
- "5432:5432"9 C# {& g( p: u3 C
```
7 _8 x- W; ]- I/ d! w6 L% m/ H- 启动 Docker 容器:2 {0 m4 z7 g' p6 i! | s5 Z: S
```bash& z3 h7 y+ \3 \9 ]0 P
docker-compose up -d; x: B" D2 b7 M: Q/ M
```
( u5 l) b) U( a( W0 e: w( s* C2 y3 \" c* K2 \2 |: a5 P* r' D- L
#### 5.2 配置 HTTPS& k9 M3 q+ _- }9 A
- 使用 Nginx 反向代理并配置 HTTPS:
' a+ d) ~5 K) U3 ~ ```nginx
8 c# ~0 M. N. \% G& B/ E server {
/ ~) v7 C, |; A5 I- m$ @0 P listen 80;% v$ |" O/ j2 F9 a# ]* v
server_name yourdomain.com;) h' h2 o4 d1 J
return 301 https://$host$request_uri;
/ \( s: r" S( v+ J) v! ?; n, N, D& Q2 ] }
' x* w, u" Y. V0 d7 y, n! X
% @4 |$ {8 q( X$ ? server {
. Q% N, S; S" p2 A$ E listen 443 ssl;8 Q' O5 Z9 R! Q: W% K Y
server_name yourdomain.com;
* Y0 h M- D7 X( O* N' j9 j( p/ H9 r# ~' i
ssl_certificate /path/to/your/certificate.crt;* g: b( J$ \3 m- _2 N5 o
ssl_certificate_key /path/to/your/private.key;9 w0 S7 c; l. k D
/ q. q: c. X5 F5 e
location / {
2 y: V$ W' Q q- _9 [. K proxy_pass http://localhost:3000;
# D- w' e- l, ^: Y4 c proxy_set_header Host $host;
1 s, r! R) M6 i1 D proxy_set_header X-Real-IP $remote_addr;3 b0 @' w7 Q! ]# _
}" C0 t6 a7 ?2 r
}
$ a0 T- Z2 c% W0 h; k ```
1 x2 g6 G9 V5 K! ^& m/ B
7 }$ \& ~. W& t0 V+ j; z+ j2 l+ V! M7 S---; c7 ~# @; p- P8 V
) L, D" \0 ?7 U- J
### **6. 总结**
! G" w, S. W) r' Q7 y& W4 J通过以上步骤,您已经成功将 DeepSeek 集成到 NocoBase 中,并可以通过 API 调用实现自然语言查询、智能建议等功能。这一集成显著提升了 NocoBase 的智能化水平,为用户提供了更高效的操作体验。 |
|