|
|
楼主 |
发表于 2025-1-24 13:35:19
来自手机
|
显示全部楼层
以下的操作步骤,帮助您将 DeepSeek 集成到 NocoBase 中。
$ J9 v" K k1 A) T0 T9 ^, f' x A* H# u: K0 M
我们将从环境准备、NocoBase 安装、DeepSeek API 集成、插件开发到测试部署,逐步展开。- Q4 j( Y, l- @4 a& R, [6 a
* b% P- Q+ u" x
---" J0 j% Q0 ?6 \7 g j" `
, L1 w4 ]# s- w# [3 O0 D; t
### **1. 环境准备**
9 P' h3 a4 U' M7 G W#### 1.1 服务器要求
* X. U( t4 p# i7 c( |2 B7 V( m- **操作系统**:Linux(Ubuntu 20.04 或 CentOS 7)或 macOS。' T6 P2 _* s$ e) R2 V3 G: }
- **Node.js**:安装 Node.js v16.x 或更高版本。7 K) r8 l7 ]* G
- **数据库**:PostgreSQL(推荐)或 MySQL。9 e8 ^% o; N/ D2 ?3 \
- **DeepSeek API 密钥**:注册 DeepSeek 账户并获取 API 密钥。
# H$ f4 T5 c* c* q* y8 W5 q+ E1 Q
#### 1.2 安装依赖
4 ? J* q) R7 r" f2 d% H/ e, X- 安装 Node.js 和 npm:! F% U* H) ?3 S! h) K! i2 {
```bash
( ]( F) c3 |, l" m8 t$ w. N! D curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -$ q2 B. b) j" L2 A+ A/ M; t
sudo apt-get install -y nodejs
U: q7 l6 b6 R3 f& Z ```
6 {! v0 b8 D8 c0 F' b4 l- 安装 PostgreSQL:
% e# k( `, b. t- _ ```bash
% t# |% Y* ], Z! J ~ sudo apt-get install postgresql postgresql-contrib
" M, L; h+ t% M4 i5 [9 S ^* g1 o9 g _ sudo systemctl start postgresql8 y- Q& }0 s8 s, U" }
sudo systemctl enable postgresql
9 A& i& e. o& K ```/ h' p. m6 O: a
& s j5 m; i2 Z& K2 A" w& q---4 L0 P$ h. b6 N \, |, X
9 T' D% L0 t4 x. Y" o( y
### **2. 安装 NocoBase**" s, R5 g- x, x. i! U! q
#### 2.1 下载 NocoBase# x* u, N! \$ V E! T$ Q
- 从 GitHub 克隆 NocoBase 仓库:
" E4 Q& t" ~+ S# A7 J ```bash* @ S1 V, L" ^
git clone https://github.com/nocobase/nocobase.git
t9 T( v6 f, o, v6 F% G. j0 M cd nocobase
. U* J8 }; D9 h( m- S# h ```* E! s8 a1 u' }! W; N& W9 j. d
; k" C) A' x* B
#### 2.2 安装依赖) W; B" M) \0 u, v$ w1 s
- 安装项目依赖:9 d4 ] D: n$ I: t
```bash
' i4 O) C1 C* s! Y+ Y npm install
2 I1 _; [3 h$ ^3 v8 N# j% K7 | ```
2 ~4 Q" L1 [* W' p% j- I* f% Q) g% `. c" _
#### 2.3 配置数据库
* d( [. `) _ k2 J: c6 R5 I- 创建 PostgreSQL 数据库:) c+ n" t- j5 r' {% [% X7 D/ E# k
```bash3 M; i% Y1 B2 l; ~0 p+ ^1 E* h, D6 f
sudo -u postgres psql
, Y8 k/ F& B6 \, |* p CREATE DATABASE nocobase;' a$ p; H2 u N% ]: u$ o- z
CREATE USER nocobase_user WITH PASSWORD 'your_password';1 g T/ @! V3 S9 b0 r( d2 [) Y
GRANT ALL PRIVILEGES ON DATABASE nocobase TO nocobase_user;7 A H2 x/ r: e( N( S9 F. |
\q
2 o6 v5 r J! V- O. j' p ```8 W' j- X% L+ L# g4 J& z
- 修改 NocoBase 配置文件:) p; s4 i- f& I% {7 d# R b% @
编辑 `config/config.json`,配置数据库连接信息:' p! h+ e0 H! n5 @4 d
```json( r& d" q" q& X6 e' l9 ?* e
{
' ]# b* L0 _" W: @ "database": {
7 b- S% p1 n/ V7 |- k "dialect": "postgres",
6 @# |) u7 _* h: { "host": "localhost",
+ Y1 }- ?) u4 D. ` "port": 5432,
: f; ^ Z5 R2 n& I "username": "nocobase_user",. P+ E/ T+ r0 w( v
"password": "your_password",
1 C4 a/ b8 [6 w/ | "database": "nocobase"/ d% g8 G$ ]8 r: ~2 D
}/ [" n- \' J& G/ _+ n1 s
}/ i: m0 z& w6 x+ n% I3 \; L+ |
```, W$ o9 D3 w& i9 b6 m( U1 n% v
' g; i& @* L3 H |#### 2.4 初始化数据库. s' Y% Z: Y- ?3 K
- 运行数据库迁移脚本:
' }2 H4 _- G2 ?' U ```bash* Z3 ?5 e* s U* Z: L
npm run db:migrate' s f7 ]4 N% T! g" t$ ^! l) g# X
```! q' N; }- D/ G% N
) e! a6 y) _ {" m* \
#### 2.5 启动 NocoBase
7 o8 n$ s1 q$ E* M- 启动开发服务器:
% Q4 A6 B+ O7 R0 ` ```bash
3 V8 q# B/ g. z" Y* H0 w( E npm run start
- X$ K9 _0 W3 D. O ```: n& }- a$ s# f/ o1 N
- 访问 NocoBase:
/ h: u6 N4 F& C6 R, Q: I) v! c- b 打开浏览器,访问 `http://localhost:3000`,完成初始设置。
- T: N+ v1 a- Q, z3 w7 D# i; v& U8 }: u; X
---
* l2 J2 D t; {) k# v6 A
z# h' w( s1 T( Z9 a+ T& f### **3. 集成 DeepSeek**
+ w# _0 `# B K/ b#### 3.1 创建 NocoBase 插件
/ g6 ?9 D9 |! H* A2 J9 S- 使用 NocoBase CLI 创建插件:
3 c8 p2 O0 Q, o ```bash9 c4 @' n. A0 N
npm run cli plugin:create deepseek-integration/ L8 u* J% l6 r7 n& q
cd plugins/deepseek-integration) |5 ]5 s0 a7 W: a' m
```
" a3 ~/ N s! ]) B. F7 |$ D: i8 H/ \
#### 3.2 安装 DeepSeek SDK
! _$ u) H5 g% v. W2 D" v- 安装 DeepSeek 的 Node.js SDK(假设 DeepSeek 提供 SDK):
- O. y* ^$ I: E j! T) R' n ```bash
3 U( O1 D; I# m: m* W* n l4 _$ T npm install deepseek-sdk
$ H. b2 N! Q, j! @ ```( ^! ]+ j$ q. O! f# c- J4 M
( Z2 b' {( _# P! g3 a
#### 3.3 配置 DeepSeek API 密钥6 `& K& |9 z$ R1 [% h4 v
- 在插件配置文件中添加 DeepSeek API 密钥:4 _! p$ \# v0 G5 I- z; A+ e
编辑 `plugins/deepseek-integration/config/config.json`:& H8 S X" E2 |+ J4 R0 G
```json$ y, V! M- R0 ]% O: i0 j0 H& e/ {
{
+ A/ i. V1 d! A8 `1 { "deepseek": {
/ f! l' @4 |4 l- } "apiKey": "your_deepseek_api_key"* d! t( l1 j( r) y H
}
h' L# r7 u7 S" {+ j& _ }
( [/ M' W7 e' t2 B5 V. v ```! j% f& ?9 y) x C
7 e' H' O1 X2 r- y) G0 p0 r2 V
#### 3.4 实现 DeepSeek 服务
' ]' s6 B. u" E* `- 创建服务文件 `plugins/deepseek-integration/src/services/deepseekService.js`:' z X/ E: B' C! m7 h, H8 W( b
```javascript
3 h% P! N* V7 }9 s' T4 @9 K const DeepSeek = require('deepseek-sdk');4 |8 l7 }5 z. H
4 u" p5 q. L [2 n
class DeepSeekService {& A" \- x Z5 G. p8 S$ ~
constructor(apiKey) {. k$ d; e v5 |8 Q
this.deepseek = new DeepSeek(apiKey);/ Y6 F/ i0 O( q k* {
}
5 ~* f1 u1 n" [! a+ x
s2 d5 Z' f0 G: c1 E( \ async query(prompt) {
% g' D2 A [5 a& H A: T const response = await this.deepseek.complete({
$ G# `8 w/ i; ` a2 q6 s5 U prompt: prompt,
; |: T9 M1 S; s- e max_tokens: 1507 ~" q, _& ]- j g# \; m( J
});% d4 b. y$ q( ~+ _: j
return response.choices[0].text.trim();
0 Z4 H0 f( z! V, b# T+ n' a3 u }7 }8 h- |- d/ W) [; d7 x- d' Y- E! F0 L
}; s, X/ J" r7 }) e n* R* `
7 D% ?6 D# A7 Y" S
module.exports = DeepSeekService;
$ _ @6 A0 _% q' {0 }8 A5 ? ```
+ l3 ?9 J' A) p
$ X! p9 ~4 A, |% U+ A, C6 e5 E#### 3.5 创建 API 路由9 b! j: v3 |3 e7 F* d
- 创建路由文件 `plugins/deepseek-integration/src/routes/deepseekRoutes.js`:3 h3 _* r1 ?# G/ t- B
```javascript0 `8 X. [" w7 q9 @ N( B
const express = require('express');6 k3 \) K) R) ^9 N0 Z Q, d
const DeepSeekService = require('../services/deepseekService');- l! p# b- f0 [& J; n# o- f9 a: Q
const router = express.Router();1 V; E) X( b4 {6 N, z
const config = require('config');. J" m9 @: H( Y: c, _7 z
1 Z( u# m4 Q7 I( B/ G5 c$ k7 s
const deepseekService = new DeepSeekService(config.deepseek.apiKey);
9 j, w2 K; T! o* ~4 i7 N1 e# Z
k, t0 B4 B* f, z router.post('/query', async (req, res) => {
) [- H+ t9 A. U& R" M; ^ const { prompt } = req.body;
* c- U+ c+ a* r( ^3 s+ ? e try {/ K N5 L% O6 ]8 Y
const result = await deepseekService.query(prompt);
8 i8 l: n# D- V res.json({ success: true, result });
2 i* ?0 H7 C' U! V7 u } catch (error) {
! w# ~9 d5 O4 Y- y; H# a% \& r6 G res.status(500).json({ success: false, error: error.message });# S6 h+ n/ A. J6 \$ {# ]9 B& e
}
3 }- m- M1 n5 P8 ? });
% n' C$ w) a- H {& y$ j& n
_( R$ |$ e! o% j. ~( s7 _9 o x module.exports = router;9 Q2 w/ a7 ]) g3 S1 l2 f; r" D1 y
```. B% s+ i4 _6 Y4 Z( m
* }" |9 c# ^4 _7 h
#### 3.6 注册插件
5 i; m) f) d( r! f" K F. I8 R t- 编辑插件入口文件 `plugins/deepseek-integration/src/index.js`:* ~* I3 r2 O9 A
```javascript
" b1 W9 U1 U- z9 k6 W, a% ~ const deepseekRoutes = require('./routes/deepseekRoutes');3 A0 C4 D3 M9 u F- G
8 {, h- p/ y9 T* e) t module.exports = function(app) {
, u* v* J, B6 g& i, ^ app.use('/api/deepseek', deepseekRoutes);
5 b2 W! D9 J$ ^! y# D5 z* R: x };
3 V4 n' Q2 n( ]0 I" e ```
3 i& Q7 B l- V1 t4 ^$ p. S0 B- I& @8 o" _" M8 M. d
---
1 r, H3 o0 _8 r+ A3 j4 c$ i9 g" r4 [2 p0 o& a' s
### **4. 测试集成**. W2 l: B! }# T
#### 4.1 启动 NocoBase. S k) B+ E( ]" ]6 L0 f) J
- 启动 NocoBase:9 t& ?( x1 z. u- I, p" \2 m
```bash
* s; H; U8 b3 u: F8 k% @ npm run start- z& s' B7 ~* N2 w0 b% q. ]
```
: n8 `9 s4 u# ^8 l/ U1 P" w
6 Y3 L `0 s$ ?" {* o; o#### 4.2 测试 DeepSeek API4 p$ i8 N' h$ R5 Q# r# A% `
- 使用 `curl` 或 Postman 测试 API:! r8 O" c0 Z9 R0 c- U
```bash0 a6 W3 I |( m
curl -X POST http://localhost:3000/api/deepseek/query \
% E/ ^$ x' Q5 `5 Q6 m. e4 j -H "Content-Type: application/json" \
: H( a$ w& R' e+ q( z -d '{"prompt": "显示最近三个月联系过的客户"}'
& l# t/ g* q; M ```: z" C0 A6 n5 ?
0 r, r8 f# X# F---3 ?' @* j# T& T; f' c
$ i6 K$ \. g5 `: {
### **5. 部署到生产环境** I* z$ x$ [! D0 \$ {
#### 5.1 使用 Docker 部署/ h! D+ |& x" N; n3 M: ~) e1 H8 F# C
- 创建 `docker-compose.yml` 文件:4 w, b+ d0 U. Y& z) y' P
```yaml! v3 u" J/ K. o+ o
version: '3'
+ x% h& g1 _1 L# L/ T8 O2 C* ? services:& J V) Q* O1 H& S
app:
: {* e$ _9 L8 N) u0 c2 O image: node:16+ o% ?* l7 k6 V& C6 \
container_name: nocobase
& B: C# \1 ?5 X working_dir: /app
. h6 T; e- p+ ^2 @6 S# o2 Y volumes:
7 H/ L$ \8 n# m9 `8 G - .:/app
9 @- ~2 g* c) O% i! l% Z ports:
% l# ~, [7 ^, ]/ I+ W' n8 Q- L - "3000:3000"
5 q8 J& Y7 H3 H+ ]0 _6 s environment:8 v: M9 {. K4 K, i. j
- NODE_ENV=production' l5 E5 M9 z; E$ ]- J
command: npm run start& @! c. u7 ?. a5 V* G
db:3 v1 g9 p s8 @6 p" j5 `
image: postgres:13
8 P! Y& H/ w; v; W3 J e# \" [ container_name: nocobase_db9 p: J# c+ |5 ^
environment:8 M$ n% ?# b' }0 Z2 Q' A5 ]
POSTGRES_USER: nocobase_user" f% P% n# P7 q$ c! _# o- ?( g
POSTGRES_PASSWORD: your_password$ |: {$ A3 S2 Z! `% L; J9 O9 x! o9 v
POSTGRES_DB: nocobase
! N7 _# j* m$ T* R- C) J7 A ports:
* E" ^% w) Y( E - "5432:5432", X3 B% N0 y$ `0 w- F; O0 V
```$ h% J6 C. L9 j2 C
- 启动 Docker 容器:
6 z) D$ E0 o: n$ I ```bash
3 T( M* P$ t4 i$ V9 Y- @ docker-compose up -d9 A: Y7 [3 q0 Q2 f( Z( {/ V& m
```$ f! N% k6 C O9 B6 U) v3 X
: s# W1 b. E( Q; F% Y1 t, v+ q#### 5.2 配置 HTTPS
. P* N) ~ j B; C0 E- 使用 Nginx 反向代理并配置 HTTPS:- `( H3 Y5 x0 j2 ^
```nginx
8 o0 m: V0 K. H3 m server {
: L! H2 F3 c- K" u* D, i listen 80;
' ?2 {# Z$ }* G t server_name yourdomain.com;8 U- o+ B" m# s; c. Y
return 301 https://$host$request_uri;* l4 v' Y0 K2 |
}
2 ?4 L5 N1 w# J6 p# g! A5 r0 n4 Z
( J) W5 K: P$ z1 k6 c1 g* w/ j+ a server { \0 o4 E( @+ K( a. D& W2 o
listen 443 ssl;
7 |3 W* D `4 _9 I Q# ] server_name yourdomain.com;
. m n6 V- [, J1 J4 N
4 { B) @, }5 b8 _, P+ C: N ssl_certificate /path/to/your/certificate.crt;
5 g: B0 F; U L: {8 @8 L ssl_certificate_key /path/to/your/private.key;9 I7 c3 g: U9 y$ g$ g1 h
& _; D' K. R- w9 }( T" p
location / {
3 z! r( C9 a. V2 ?4 J5 r proxy_pass http://localhost:3000;
4 r$ v; G8 l$ ?8 [: }- }- r proxy_set_header Host $host;
' k$ N$ `, ]: Q( ^4 B) v proxy_set_header X-Real-IP $remote_addr;+ I$ ^) x; X6 g3 q! B, R
}; y% |6 j0 Q+ p, _: R
}
, \/ L( p& c4 J ```& H0 S* d6 `) J1 g; T8 B F
- x. r* M& p7 `0 e' j. `: G---4 D- o+ C# i% t* a
# Z; A. _, q' d# P) b0 t
### **6. 总结**" L. E" H+ b+ _7 V1 X
通过以上步骤,您已经成功将 DeepSeek 集成到 NocoBase 中,并可以通过 API 调用实现自然语言查询、智能建议等功能。这一集成显著提升了 NocoBase 的智能化水平,为用户提供了更高效的操作体验。 |
|