|
|
楼主 |
发表于 2025-1-24 13:35:19
来自手机
|
显示全部楼层
以下的操作步骤,帮助您将 DeepSeek 集成到 NocoBase 中。+ Y, A: y# V; r
( ?6 v0 Z9 l' Q. E
我们将从环境准备、NocoBase 安装、DeepSeek API 集成、插件开发到测试部署,逐步展开。% A. w# x) K; z3 d* _
. |+ \9 ^. m) K6 ` D* g---
7 t* s5 @8 t6 D B, V& ]3 `5 R* p) s) L# s; m9 n" i& I
### **1. 环境准备** R# l1 z( v, z" M
#### 1.1 服务器要求
2 |! b5 L4 k& i1 S( _6 j+ _: ?1 t- **操作系统**:Linux(Ubuntu 20.04 或 CentOS 7)或 macOS。
# P3 s5 r0 D" m ~. k5 \& @- **Node.js**:安装 Node.js v16.x 或更高版本。9 H: K, R I+ L) O
- **数据库**:PostgreSQL(推荐)或 MySQL。
8 h& d3 b* \4 h4 c5 ?- **DeepSeek API 密钥**:注册 DeepSeek 账户并获取 API 密钥。. H& c' x3 W( J- Y
* e# k8 N/ c# A+ w7 X; _; W9 N#### 1.2 安装依赖' J4 r4 ^6 w& z: r; c
- 安装 Node.js 和 npm:
; q3 I8 E) @% H5 Q) O/ B+ V7 u ```bash8 r7 D4 H& z4 B
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
3 l* i+ r+ c& _2 Z sudo apt-get install -y nodejs# i5 x. n/ Y8 \" q5 x* W$ R
```
) W b# T1 c# y1 o- 安装 PostgreSQL:! W- {+ y f r x4 x
```bash
! q% T; g- D9 W6 F; w& B sudo apt-get install postgresql postgresql-contrib
- \4 d4 v2 W( B" Q t sudo systemctl start postgresql
8 X6 l' O& W- t; N* t& i8 G sudo systemctl enable postgresql9 \) G0 X! z3 W7 O# t
```$ K6 r1 N; i$ B. D. b# {
4 P0 W$ U3 d: N* Y- Z, r---
6 h8 Z0 H L$ u
7 x4 P/ B; _; H& d- X### **2. 安装 NocoBase**
}) y. E' l! b# k$ L# \! q#### 2.1 下载 NocoBase2 `+ `2 {) ?2 K' O/ W7 y
- 从 GitHub 克隆 NocoBase 仓库:
4 [6 x$ K1 j$ ~2 c/ V& v. S( i0 Z ```bash
) d7 ~* r" ]0 {% {1 w7 Z git clone https://github.com/nocobase/nocobase.git
% C* F0 d h# b! S5 q! u cd nocobase2 a1 }3 `+ G0 t4 m
```! I% P4 J+ [. ?0 y& U! P) O
4 J! }; _5 p) \( u9 ~5 `, z#### 2.2 安装依赖
+ O4 x7 R4 o1 Y9 I- C; o' Y- 安装项目依赖:
" C+ ~( }0 d' Z/ H" D- A ```bash$ m3 ]2 {) _$ z. k5 [
npm install- j2 S# c6 I9 c+ X% z- g, d
```
' B7 K. k0 m4 p2 d6 J0 ^+ K+ {- g+ e$ H7 H. y4 O' Q* k
#### 2.3 配置数据库
$ e0 K& d% P/ W- 创建 PostgreSQL 数据库:! q2 j z8 m) ]6 v' L% m0 m3 A6 y
```bash
, e. W" U% W! [8 E* v1 w sudo -u postgres psql
4 z! i" L7 f3 q a1 L CREATE DATABASE nocobase;
8 C; v& H. e% b6 n CREATE USER nocobase_user WITH PASSWORD 'your_password';' N ~5 X, L X, b2 L3 l
GRANT ALL PRIVILEGES ON DATABASE nocobase TO nocobase_user;) x8 {0 V5 o2 N3 t0 G6 A
\q8 O3 p4 F8 e, T) w' s+ |
```
0 U& `* e) V6 K- 修改 NocoBase 配置文件:5 W& W* R) F* w( r6 `
编辑 `config/config.json`,配置数据库连接信息:! E# V0 w6 F/ n9 L* S. P/ _
```json1 p5 S- L9 c. M8 e
{
+ Y% q" ^+ T2 u "database": {* y1 W9 ]! H3 p
"dialect": "postgres",
" o5 c0 {+ W/ q. R: |1 G" E; r9 e. E( l "host": "localhost",7 @5 K- |* | O! @# N& U0 u$ c
"port": 5432,7 q/ A2 N) r4 ^& z; o) i
"username": "nocobase_user",; S( `3 } G' C
"password": "your_password",; F$ e7 A9 v# e3 j9 Z
"database": "nocobase"' n9 \5 C6 ^' f$ S- Q% E
}6 I1 M( T: D, f5 t& P1 c6 \6 c
}* h+ ^' [) o2 `$ p g
```' N& |8 V2 E* c- F' q+ y+ j
3 A. d" y$ G% B
#### 2.4 初始化数据库" P# b+ b- c& d* _" |3 q' H
- 运行数据库迁移脚本:
# f1 Z4 x% ? Z7 | ```bash! N) {1 w& [7 O5 \; @0 i
npm run db:migrate
4 c& G' j3 r' H7 u/ U/ f ```8 p2 n1 o, g4 p( v& W" d
; H! f6 z7 V: R- |, s% p" p#### 2.5 启动 NocoBase4 G0 O) F9 d5 R6 D$ z8 g- j# ^
- 启动开发服务器:
0 ]' a+ j1 k% t+ r2 ] I0 p( ` ```bash
" K. x9 M9 u. i ` npm run start
. Y: J6 j+ y2 k! O) V3 d6 D6 Q ```# x3 ~8 C) c9 n6 b
- 访问 NocoBase:
4 m l2 Q/ E$ |* n3 Z 打开浏览器,访问 `http://localhost:3000`,完成初始设置。
, X, A( V0 g" S# c+ u- G
+ Z" P! \% H2 O9 M. O---; }, Q" Y g! O* \# {1 n! a- s9 _# \. A, f3 C
7 W4 ~$ I: {2 e5 c### **3. 集成 DeepSeek**
7 B* y0 I% a, Y1 z7 T/ I$ B$ z#### 3.1 创建 NocoBase 插件
' |3 w: u2 m2 P/ e/ G7 T6 t- 使用 NocoBase CLI 创建插件:3 w' w& t" l7 h, U- b1 j% B V( M
```bash$ M5 _, `" r9 x7 i
npm run cli plugin:create deepseek-integration M8 O8 }5 x; m# d
cd plugins/deepseek-integration
+ O6 B) a5 o3 T1 v ```% g6 N: p4 K4 [/ P4 ^3 X4 `0 \
9 I& H% g2 p* K) s2 Y+ u. v5 U#### 3.2 安装 DeepSeek SDK
; P( h7 \0 C5 _1 i" T- 安装 DeepSeek 的 Node.js SDK(假设 DeepSeek 提供 SDK):
* |- p8 g' z( Q; t! ~# B ```bash
# [. C$ d% b7 \' L4 D1 ? npm install deepseek-sdk
; ]. }$ x- N: O" j0 [7 ^* r ```
% a2 H, U: V' V9 \, c p3 s! o& \) a1 G/ ^/ M6 I9 L; f
#### 3.3 配置 DeepSeek API 密钥( ?! x; @, A9 t5 T
- 在插件配置文件中添加 DeepSeek API 密钥:8 d# H# {% b3 Y2 e$ b) L
编辑 `plugins/deepseek-integration/config/config.json`:! ]7 i9 \6 m% Q7 S/ r u$ n
```json
2 E) |) l4 {7 Y) R# [' C. V& ^ {
) l4 _9 q5 ?# ]7 T# P1 s. o "deepseek": {$ s% x( I" H1 V: |
"apiKey": "your_deepseek_api_key". V! d2 Q1 L) _. L' e" E
}
* U% _% I I! } N, v$ @ }$ x% {, n9 d. Y F' {0 f
``` e6 |+ F$ w7 h7 E4 X: c( P
- U' _3 A/ x" a0 y
#### 3.4 实现 DeepSeek 服务% Y& x) W. r( H
- 创建服务文件 `plugins/deepseek-integration/src/services/deepseekService.js`:, \0 z" ^2 U' \4 z2 H' X
```javascript
1 y9 i" B7 k7 G# R6 ]0 h" q* m const DeepSeek = require('deepseek-sdk');
* S* S0 `: |5 Y2 b+ n w: w9 a. T- E/ j* `) O* w
class DeepSeekService {
5 W# C( G: z: P" e constructor(apiKey) {/ B6 ^3 X& b {4 z
this.deepseek = new DeepSeek(apiKey);8 K" I0 P8 v% ?8 c' L& K1 \
}1 }- K. N3 f5 @, K2 ~
# E# x+ q* a7 l0 Y2 n. S- j
async query(prompt) {
8 ]) m/ q) [( V1 k& I- a const response = await this.deepseek.complete({
. z+ F5 M( x- |5 r prompt: prompt,
! \$ q# L& X; t9 k! a max_tokens: 150
1 q, x$ Z1 U- f. T/ V/ v: N# e });
# h L7 n1 v. B @8 F: F$ x' h# T return response.choices[0].text.trim();
) R- o0 H( T. {/ v% s" I }
8 w" o; k5 b5 N/ V/ z& k }
( S! t8 g- Z% K6 u0 e( R
- _9 }6 p0 R o+ |9 j, K' _ module.exports = DeepSeekService;
2 z# S! L8 R" I$ M3 ^' b ```% X; ~, u" P A5 C4 P
' L* E7 Z# L1 c- H7 W. f6 m. K#### 3.5 创建 API 路由
& p, P0 D/ C3 v# T" I4 l- 创建路由文件 `plugins/deepseek-integration/src/routes/deepseekRoutes.js`:+ S9 c- M- S! F# d+ G
```javascript
* r9 [4 u) [$ e8 z! d; z5 n- N: ?1 } const express = require('express');* a7 X. r& s4 K/ b* u- L
const DeepSeekService = require('../services/deepseekService');- Q [! _5 m3 X' N
const router = express.Router();& }$ |" e( o1 D! n) c: n% z# y2 A v
const config = require('config');
" |. q( M. E& H( y" W
2 `3 K8 U. l6 m9 U m const deepseekService = new DeepSeekService(config.deepseek.apiKey);
7 W6 Z5 K3 d( L
& y8 X! @/ E, W( t router.post('/query', async (req, res) => {
; }6 s* v2 o$ y4 t8 z+ Q const { prompt } = req.body;; p) u5 J1 N/ Q" B7 N
try {3 q% R7 ^7 g& j
const result = await deepseekService.query(prompt);
5 ]4 j! t9 o/ ~9 c+ L9 v res.json({ success: true, result });
2 w: i% W- ]- N4 r$ V8 z } catch (error) {
, @6 n1 i) z# T) R! W, C/ T res.status(500).json({ success: false, error: error.message });
) _7 h @4 L9 X9 s9 `1 { }" y% K: y6 |6 h2 u+ O
});3 R. v6 s; S: {8 ?
2 d$ r8 O7 t; T- ^# X" N9 Q module.exports = router;. C1 e# A/ `1 k1 K- n: Z3 A
```
$ L* g a. T! \( g6 Y- t9 p8 M* k T
) H% ~1 K8 l2 x! E T) O7 i+ G#### 3.6 注册插件
6 w7 j0 T$ [ m) S- ^9 P$ e0 @- 编辑插件入口文件 `plugins/deepseek-integration/src/index.js`: [( A& f8 V m! W# b3 {9 h9 N
```javascript& T1 a2 D" F$ |0 S
const deepseekRoutes = require('./routes/deepseekRoutes');
2 p5 J" D! L- Q# A9 ]; Y# ?8 f( w
module.exports = function(app) {
6 \; P+ E; i; A. i j+ ]) s app.use('/api/deepseek', deepseekRoutes);+ {5 J4 k" r3 O+ @# |
};
% u3 S3 A8 ?$ X, ^. x( [ ```0 m: j9 K) U2 N7 |$ h- g/ N1 b0 {. E
1 }; Q' v$ |# B, R7 K- t& B
---
8 I. E! g5 { j; l8 B' R7 G3 d6 F0 ]$ `) m
### **4. 测试集成**
0 [# r0 p2 Q* J3 ^6 w x#### 4.1 启动 NocoBase
& T) @! t, b% U) u- `" \ G0 j- 启动 NocoBase: d8 h, G! g. R0 ]- Q3 u( b% {1 X' \: P
```bash
1 ]% i" h; @# D9 { npm run start- ~* ~9 m; G. T& a6 y
```
, l+ R) U- D6 q4 r. l) _8 T
: Z w3 }' Z( z- b#### 4.2 测试 DeepSeek API3 t5 F" k* j4 U$ p j [* w
- 使用 `curl` 或 Postman 测试 API:6 n7 u/ M5 W2 q& _
```bash
. ^8 u' {+ ~* a/ _; n2 a curl -X POST http://localhost:3000/api/deepseek/query \
! \& I) p! s8 B, u: ^" x% J -H "Content-Type: application/json" \
/ a, H' B1 V+ [* l" l4 x- r -d '{"prompt": "显示最近三个月联系过的客户"}'
8 E" F7 `- }/ x" E1 x, V ```
$ ~9 @( P( B, Z! S+ ^3 z6 q: k {
---$ ~1 j/ l9 G9 {. l6 z( r
, U: W6 [0 [- H; m3 F
### **5. 部署到生产环境**
$ w- X( i0 P% R8 ]$ z+ V#### 5.1 使用 Docker 部署
& Z: N& r- n* o- 创建 `docker-compose.yml` 文件:
7 y0 b) ~+ a' f ```yaml1 y/ v: ~$ a, H2 m% G
version: '3'
+ E8 R( p, z9 ?" k0 u4 V services:1 b5 z. C S( {+ }/ P& h% I$ X
app:& H8 ^# Z* q3 v u% ]: M8 A
image: node:16
. r* g L: }) Q, z2 ` container_name: nocobase2 P6 g" Z; ^& s* R& r0 L0 k1 @
working_dir: /app9 j) r2 |, R4 Z, z2 l. X/ |
volumes:$ Y5 T, y4 u5 v' P& Q" @
- .:/app
; b& X5 y& a1 m% ~ ports:9 q1 _' }! W1 V% s$ i: q0 ~2 v) B. k
- "3000:3000"
2 c: e4 \. g$ { environment:
1 g# X8 v" G8 l9 y- v) G: M - NODE_ENV=production k2 D1 \; W9 M! b G, y) i4 G1 I
command: npm run start) `* E8 F3 F1 _ h
db:5 Q0 j8 C, t6 e/ w; l
image: postgres:136 J& W7 ?8 q, o1 `! }. k
container_name: nocobase_db
' Z+ J. Q- g5 G: l: A7 s environment:
2 x: O: S0 a V- x POSTGRES_USER: nocobase_user' ~, t; e- @3 |; s) g; ?& Y
POSTGRES_PASSWORD: your_password* j" I% Y/ ]9 H" w
POSTGRES_DB: nocobase
% P% O: k3 E |. _4 D( _5 l9 ^$ L# R ports:
& ^) T3 D2 ]; s - "5432:5432"
' G5 }. R2 a4 f2 g ```
0 N/ B Z& I* N- a. P. @& d9 g- 启动 Docker 容器:
. _7 B- e3 s0 w' } ```bash
3 ?; O! A0 u* }' F' c1 ]4 ]/ }) h docker-compose up -d
: R/ J M6 ~5 Z0 n ```
$ } o. x) p$ {* o# `! u$ f
+ U/ u" N4 O4 M, q#### 5.2 配置 HTTPS4 |4 i0 L& J& \" j4 L; j; A1 i- C
- 使用 Nginx 反向代理并配置 HTTPS:, W" {, [- Z- D3 u" m* C7 O/ X
```nginx1 E1 \$ L# j F
server {8 ^- R! i# J l$ |+ _
listen 80;
0 h/ E' i7 G Y% s% y5 M server_name yourdomain.com;0 ]0 {/ d/ f7 t9 T; m5 _( i8 z8 S! U
return 301 https://$host$request_uri;5 Q. a8 e0 v8 p
}% L- _( l7 C, R$ k( h
! W6 L/ N9 L2 h9 h server {
/ G' q5 {- ~# }4 ^+ ] listen 443 ssl;4 a3 L$ S$ r/ X
server_name yourdomain.com;
3 A$ s' Y) ]6 D( |; ]5 V5 @" N6 M
ssl_certificate /path/to/your/certificate.crt;) i5 n( U4 J$ s- W. k$ M# z0 O
ssl_certificate_key /path/to/your/private.key;* P/ O" B- M: ^; ^) p
8 G. K* j' u" G* V& q
location / {+ w5 v& \! {$ Y' N) W; C: b5 C
proxy_pass http://localhost:3000;# f, m! f, n! E* H1 f! @ {& T
proxy_set_header Host $host;9 \! v* b6 K! N: ^" h
proxy_set_header X-Real-IP $remote_addr;2 a6 z8 P7 C9 x2 j) n" Z% \; U O; D
}! d: q$ I4 v: M; u2 z: ]( _0 t1 C
}
+ {" o& O' L: O ```8 h% a6 N# c7 D9 t2 D
* f' Y4 ~$ o0 `3 `3 N% v1 A---
3 I$ H* y3 }1 }7 y% l6 A0 B0 J7 X K& H" |
### **6. 总结**
- U$ S3 f* Q: X+ r# V0 V. E- k通过以上步骤,您已经成功将 DeepSeek 集成到 NocoBase 中,并可以通过 API 调用实现自然语言查询、智能建议等功能。这一集成显著提升了 NocoBase 的智能化水平,为用户提供了更高效的操作体验。 |
|