|
|
楼主 |
发表于 2025-1-24 13:35:19
来自手机
|
显示全部楼层
以下的操作步骤,帮助您将 DeepSeek 集成到 NocoBase 中。5 R5 J( ^7 k1 I N8 d9 {2 \
7 a4 `8 T7 K# c5 M& H* @
我们将从环境准备、NocoBase 安装、DeepSeek API 集成、插件开发到测试部署,逐步展开。/ {. U& ? Z8 z* x* x
9 O2 J! w6 @$ R) R---
8 B1 V% ?- w" D. ]0 X
`! d$ O3 S3 X* s% h: f. x### **1. 环境准备**
* }2 C. o7 K; @4 R# P) w#### 1.1 服务器要求" n; {( L2 _ X/ q. Z
- **操作系统**:Linux(Ubuntu 20.04 或 CentOS 7)或 macOS。
v* d7 v4 n, E" Y- **Node.js**:安装 Node.js v16.x 或更高版本。
& B$ I. ~& t* L" {% |) A8 M) }- **数据库**:PostgreSQL(推荐)或 MySQL。
2 w" B3 {3 V: S6 J# v6 K6 i- P- **DeepSeek API 密钥**:注册 DeepSeek 账户并获取 API 密钥。% { @8 P2 [# K+ K4 D/ f& `
) c9 ]6 Q% j# y8 T
#### 1.2 安装依赖
/ d8 `- K0 p+ _% t2 t; w- 安装 Node.js 和 npm:) s' O; \: k4 a( [
```bash
" H' k# r1 i+ I- X( r* _9 { curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -( A, ^+ E, _# f" e) C( \, J8 r
sudo apt-get install -y nodejs4 d% k9 l* M1 ]- j5 L( `
```
; j0 L: J4 ~8 V% n& l0 |" J- 安装 PostgreSQL:- N: f: f! A$ y
```bash2 X+ S0 y7 U' C: M. K
sudo apt-get install postgresql postgresql-contrib
5 p( ]! ]# N! s/ j* M sudo systemctl start postgresql4 Y0 P2 ]& k# {" e
sudo systemctl enable postgresql
3 x' V" f9 x0 z' _6 S ```
/ N. h' D3 }: l3 x% t( A
' l; T0 G$ b# U---) ~) U/ s/ d( q$ M
, h8 k0 o9 ]! H( F2 Q' M: R, A### **2. 安装 NocoBase**
0 L* P/ _4 g {3 o8 i$ g- Q#### 2.1 下载 NocoBase2 u! v/ V5 I) y4 R' |! }
- 从 GitHub 克隆 NocoBase 仓库:+ T: a; m4 r4 d2 E6 V1 Y+ U4 ^
```bash
6 |1 B9 _+ \- t1 T9 r" a* l' T git clone https://github.com/nocobase/nocobase.git; ~% X& h ~0 }! X6 }
cd nocobase
) G6 X* b; n* l h" a# w( y* O+ ` ```- B& a' K& ]6 Y; C; \
% D* p' s) F3 e/ q1 B#### 2.2 安装依赖$ |- H Z, k1 q4 v, s& m
- 安装项目依赖:
) `. k) ?* a2 I; ` ```bash
5 N. L: J% Z8 @ npm install
2 h# E1 O o: w2 e' u$ v# Z ```
4 s3 r& G" F# [! s- m
' s9 y* e4 m, f5 W6 z#### 2.3 配置数据库6 E% T9 ]+ Z* O# Y* J+ g
- 创建 PostgreSQL 数据库:) m6 ]8 T' [$ O4 X8 z+ y! T
```bash
- q$ Y7 M$ o& p sudo -u postgres psql
/ G) s' b: t* l! U) ^; m1 p- H CREATE DATABASE nocobase;7 x7 L* p0 B# O! I
CREATE USER nocobase_user WITH PASSWORD 'your_password';
. e$ {0 }# B: q5 e$ x GRANT ALL PRIVILEGES ON DATABASE nocobase TO nocobase_user;
$ U3 K6 v/ b" R \q
7 G3 @5 b" a" I6 _, G$ r& D5 G ```3 O# \; A7 t# L# X. k/ d
- 修改 NocoBase 配置文件:# U% j0 D1 c; u, h- K5 D
编辑 `config/config.json`,配置数据库连接信息:
* `4 s' O3 y& w, w, a4 t2 e ```json: r: P2 \& P- R4 n
{+ d9 q0 A0 c2 r1 A
"database": {
( |9 H, M) o- G! } "dialect": "postgres",+ N$ `, k: Y7 Z4 h) g
"host": "localhost",
! u4 a( N0 o: } "port": 5432,# M! O! D* c1 Q6 x' Q! O4 e" n
"username": "nocobase_user",: N) _5 m6 x3 U2 N# r
"password": "your_password",
8 [' e8 ` @4 C* i" H! c "database": "nocobase"+ D! O3 e, O6 S) B8 ~
}
8 s8 ^! n1 n! t' v }$ e/ Q- W0 @1 X0 D, D
```" i: s; J0 K2 F% j
0 E9 \- n, F; u' V! U
#### 2.4 初始化数据库( w( |7 m& P; U! ]; K$ t0 J
- 运行数据库迁移脚本:" ?- m" T" H& L; y
```bash
: G2 [" g; z, e# i( H( w) p npm run db:migrate: ^$ s; d) S) a* y5 r
```
) o$ b/ M* [! c, t) h1 y; Z7 w/ ^5 m4 n8 K- |! H. M
#### 2.5 启动 NocoBase
' O. \4 z# a/ ^- U- 启动开发服务器:
! B& m0 H# j; }/ x% R ```bash1 Z$ g& E% y1 Q4 j, @8 F
npm run start
0 o- ^* w B) w: g) g4 b ```
8 e$ y" G" F% z& T' t- 访问 NocoBase:/ C! T1 e. j' [4 C
打开浏览器,访问 `http://localhost:3000`,完成初始设置。
- X! d, x6 {0 q
& H9 w/ g4 |$ ?! _' x---) t! @4 F0 e6 q" \9 @
2 k- G: b0 I; s! C### **3. 集成 DeepSeek**4 y; Q4 Z6 A8 O& N+ R4 u2 `' g
#### 3.1 创建 NocoBase 插件! C8 v4 f1 \. J6 V
- 使用 NocoBase CLI 创建插件:( [2 Z/ |/ X# t
```bash8 j: y8 h# l5 @- g
npm run cli plugin:create deepseek-integration
, _, H" @' [. }6 B7 x cd plugins/deepseek-integration
- {9 \( N1 l$ ~, Y ```
1 H9 Q; Q7 h _6 k) r# E0 t8 c
b4 s0 U- B h& K" g' W6 v#### 3.2 安装 DeepSeek SDK
/ B3 T1 y0 R$ |+ I( u& f6 H% @- 安装 DeepSeek 的 Node.js SDK(假设 DeepSeek 提供 SDK):$ h" R! f1 J7 Y5 e
```bash) p8 |! O& X. Q, c/ E/ Q
npm install deepseek-sdk- Z! C1 Q. r: A* I/ e! I( c
```6 c% S9 m: `2 K5 W. z
3 C+ d2 h1 W9 U3 c
#### 3.3 配置 DeepSeek API 密钥
M @, T: T4 P' i' G0 ]% B/ [# z- 在插件配置文件中添加 DeepSeek API 密钥:
1 I3 L- i& E. G0 y" W3 l 编辑 `plugins/deepseek-integration/config/config.json`:
5 j- B1 n) N% E( ?8 k ```json: c+ u' Y3 r% A( b
{
/ d7 r2 [( b) {2 e% h "deepseek": {" z+ x2 U5 q( V8 E. K1 h
"apiKey": "your_deepseek_api_key"
# I- s* Y+ h# X) i* t }
1 r6 G5 X# Y% |/ E9 ?6 Y }" o& ^; W! h8 O: A9 e0 E( H
```4 P* }" z7 M4 l$ ?' V5 F
5 d, F! b* ?4 _3 F" I- D$ h% E#### 3.4 实现 DeepSeek 服务
( |* C& G9 y) j6 v2 C- n1 Z- 创建服务文件 `plugins/deepseek-integration/src/services/deepseekService.js`:, Y Y$ P: T% |7 P# o4 d# {# n' r! t
```javascript
|" h) g9 P3 O# } const DeepSeek = require('deepseek-sdk');! X" T9 s4 O2 X
- x, H, K$ u! X9 I! B class DeepSeekService {8 @7 @: e2 o; k, _9 P0 }) h
constructor(apiKey) {
6 ]$ w' K% R1 l6 W1 f this.deepseek = new DeepSeek(apiKey);
5 k* Y& h6 h6 G# t: F6 _* N }
1 I4 _5 A) r) f" _7 C3 K6 m
, a6 |$ h1 Y f; p6 p$ `/ ^ async query(prompt) {
/ W. Z- H% b" D/ |( q( K+ P const response = await this.deepseek.complete({
7 _, D; g/ F4 I( ?' @4 T prompt: prompt,
0 c- o* W5 L7 V0 B6 l6 Q: h max_tokens: 150
" q4 W& U, s$ N9 l });$ u5 p0 J0 h2 Z
return response.choices[0].text.trim();( S" e2 L/ }. M8 J- n& V5 l# _2 H
}
# j* L4 Z& |5 `- c s }
1 W, r, H5 j: x/ N: q3 K+ O: Y2 z" t8 W
module.exports = DeepSeekService;4 H7 A) ^( x( Y# K
```$ H0 h- T/ H; y% Z( o7 b' h
5 m& z7 b% D) K8 {7 m% @. [8 U
#### 3.5 创建 API 路由
9 l2 U+ O6 Q6 o$ Y% u1 U- 创建路由文件 `plugins/deepseek-integration/src/routes/deepseekRoutes.js`:
7 C k4 s$ Y4 K% P$ R( n" c+ i. y ```javascript
W0 x% O; i* P% M const express = require('express');
' l/ W9 U4 ^. v) n8 t) Z. J, X7 V const DeepSeekService = require('../services/deepseekService');+ \) Q, [( Q5 h9 H# T4 x/ h( p
const router = express.Router();
! O- t8 @( ~7 C const config = require('config');, Y" T9 \: Q2 @2 Z
! V2 r- {; ~3 n0 o/ b const deepseekService = new DeepSeekService(config.deepseek.apiKey);
5 d/ _4 I7 z/ s0 V9 u3 s! i- c, B5 ~+ I" F- e
router.post('/query', async (req, res) => {6 Y: _. p* I3 Y8 C7 ^
const { prompt } = req.body;& Y/ c L/ p+ N8 K, I5 |
try {
1 I1 F- a* p; Q: `5 F% I const result = await deepseekService.query(prompt);
, N- f; a& c( O+ s% [* V3 w& w. m" h res.json({ success: true, result });
- L }, Z& e; H } catch (error) {8 q9 ?% L; b1 C. h0 c
res.status(500).json({ success: false, error: error.message });
$ _3 L! T' L. D) D+ Q }
) y* ?$ y- E: B });, A q$ @2 a; Q
; I1 C7 D" W0 l2 d
module.exports = router;
+ Q3 Q3 t+ c7 m( l7 y6 N ```3 o/ P2 R3 I5 O+ ^1 g/ V) j
' a$ [7 W. Z8 Q* b3 B8 j) J& M/ ^2 b#### 3.6 注册插件7 J! a( M" C. t1 _) O
- 编辑插件入口文件 `plugins/deepseek-integration/src/index.js`:
/ Z8 D- b3 w2 Q1 o( D% x* K" U ```javascript
. \; _" ?, o8 f; S" @, N+ u5 B const deepseekRoutes = require('./routes/deepseekRoutes');
' W$ J; t: p) `, p7 k4 K% d" Y# s4 P1 Y( Z
module.exports = function(app) {8 G* l% J7 G5 ~" D8 x/ Q
app.use('/api/deepseek', deepseekRoutes);% Q5 k1 a' n" p
};" d: X5 C% r6 Y% V% i4 A- _4 I
```
5 o k5 j4 H, ^6 k: Q$ G4 J/ {7 r1 @( W
. R0 \7 i2 {% X6 z6 L" {" F---
# _: |. H+ X8 s) u5 `, w& p
4 z% G6 S6 c7 y/ q- f### **4. 测试集成**3 d+ z8 f& P1 U
#### 4.1 启动 NocoBase: F+ |( i& t5 b$ B. c( z5 @, y
- 启动 NocoBase:% Y( c6 u( u6 [7 O6 O, W; M, _" K4 W
```bash
5 ~: C; d& V3 r0 Z- L+ w npm run start
2 q, g; M' S. c/ F3 K4 H ```
: v0 b" x) C9 r. l' E
, o; Q' [& y( a; L8 S; i6 }#### 4.2 测试 DeepSeek API% I! Q5 W8 j/ t: I
- 使用 `curl` 或 Postman 测试 API:
( z, I8 S) N3 W- Y2 c! v' \8 {0 O ```bash
8 p3 W" t8 K% h! y curl -X POST http://localhost:3000/api/deepseek/query \% C2 Q& d$ \. @' I% J/ p
-H "Content-Type: application/json" \
8 [; v: {" O3 M% j9 ^8 F- g -d '{"prompt": "显示最近三个月联系过的客户"}'$ p3 v- V; q2 p9 r R7 c
```
2 t; G/ Y/ @" U" [
+ c# F0 m; ]6 \3 x" O/ M---
$ F" h" b* k; e' b/ Z7 s/ T
. T/ q3 @) X9 q7 V& [### **5. 部署到生产环境**/ j( V5 i6 w5 ~% E9 I
#### 5.1 使用 Docker 部署+ e) G0 s3 o$ `
- 创建 `docker-compose.yml` 文件:8 v, i/ k D0 o# d# Y1 P
```yaml3 e9 f: E7 q3 [ X7 {& W4 k! {- k; ?* w
version: '3'4 j$ q4 S- h/ e1 J
services:) t; Q4 s4 S# Z [9 E
app:2 H; [" E% {+ z" B0 Q1 z
image: node:16% e5 v" H$ q7 G( s" _" o% G
container_name: nocobase5 j3 b b8 ?+ x" }. W
working_dir: /app
8 }( {3 o2 f ~8 O! n volumes:9 o2 k: F6 `: D, q2 w' B
- .:/app2 g- d3 T- v& |) ~8 x
ports:+ j1 l; B; E8 T* q F; }+ C' p
- "3000:3000"
$ M/ o4 Q5 {4 p: Z" h environment:
- s. n: W1 J8 v, `3 M( p1 X% E7 ]; ] - NODE_ENV=production8 M8 [; i* g0 t+ T& i; I7 r
command: npm run start
( ~ @* d6 \( c4 [& M db:
! R4 r- S- z9 O& \% L image: postgres:136 ]/ a& O7 B D, c8 U9 R, M7 Q
container_name: nocobase_db+ o( ~. H( d/ b7 Q! D9 ?
environment:4 Y4 W, p/ P8 L* @3 h/ j1 o
POSTGRES_USER: nocobase_user
( s+ E( `' g8 g" Q3 A3 c POSTGRES_PASSWORD: your_password
+ p% C* O/ S' i: v) u" {8 C POSTGRES_DB: nocobase3 ]9 q: ?0 X$ o* y% l) q
ports:
k! c ]8 R* D5 g, Z - "5432:5432"! V' p, y' P+ q
```
2 U6 ^/ s' l* B1 E" b4 n7 b- 启动 Docker 容器:
# k9 O+ B( ~4 O! \ ```bash3 Q. {! [+ v0 P' i9 K) g K
docker-compose up -d
$ i+ L# i! U) |- j# U! G ```( D" ` N- U/ a9 R3 f1 G9 i
& }& E, m" Y8 K( ?7 h; G4 C) ?
#### 5.2 配置 HTTPS8 E1 g" l# Z9 L h) |# Y" i
- 使用 Nginx 反向代理并配置 HTTPS:; a, D% |4 }8 t: y* y. x
```nginx' p. x0 m6 {% p4 e' k0 R$ T
server {
! x4 M0 p& q* e+ n listen 80;
1 c* t3 M' w5 ^1 I' n! s, C+ B server_name yourdomain.com;% C3 D5 ?4 n" q
return 301 https://$host$request_uri;& ~* G& z# S! N: r& ?) R2 H
}
- G" r0 X4 a* i$ Y+ {6 O; k
; U& {8 `9 H- i1 E8 V9 G. H) { server {
# E/ k# O P: E6 C% @! G listen 443 ssl;
& k% N/ L$ x4 F: | server_name yourdomain.com;
% ^# G, Q& [8 ]: g* i$ D) y4 S q* T! Q: B; q9 K! h
ssl_certificate /path/to/your/certificate.crt;0 S8 a0 i+ I8 z/ v8 [# ~
ssl_certificate_key /path/to/your/private.key;- L! W, k2 c, k
2 F1 j/ ]8 z p9 @/ x6 h4 {% ?, I
location / {
# m" |8 x( n! s9 }7 _' W3 g6 ~ proxy_pass http://localhost:3000;% X/ w; |5 ]3 f& \1 o' i
proxy_set_header Host $host;
: `+ h8 \/ _2 L) q proxy_set_header X-Real-IP $remote_addr;2 d5 A: p3 D1 `% B1 F5 @1 w
} [0 k3 ?" N( U& D ]
}
8 g5 @" a; b+ \5 t ```0 o$ v- V. }2 B e- h; A# }
* C7 C+ I1 L, u _) }' t" n
---0 V' ]/ i1 S. x
; P, l& }0 y( G n F4 F### **6. 总结**
+ @8 q( b) D6 \0 S6 `8 s% c5 P通过以上步骤,您已经成功将 DeepSeek 集成到 NocoBase 中,并可以通过 API 调用实现自然语言查询、智能建议等功能。这一集成显著提升了 NocoBase 的智能化水平,为用户提供了更高效的操作体验。 |
|