|
楼主 |
发表于 2025-1-24 13:35:19
来自手机
|
显示全部楼层
以下的操作步骤,帮助您将 DeepSeek 集成到 NocoBase 中。
3 x4 M" A$ U$ s D: T( R
- y5 y A5 |$ W6 S4 Q9 y: F我们将从环境准备、NocoBase 安装、DeepSeek API 集成、插件开发到测试部署,逐步展开。
7 y2 Q" I8 Y, W0 W: |6 c8 c& h+ A1 W2 _# x
---
7 S6 ?5 |" O. x2 b- j7 f
! X+ A0 ^0 w7 v4 `4 Y, ~* y, s### **1. 环境准备*** V$ ]; I: P" _) S z5 t. O
#### 1.1 服务器要求
" r( _( Y2 {" ]- @+ S9 ^- **操作系统**:Linux(Ubuntu 20.04 或 CentOS 7)或 macOS。
. W9 M) T4 k" ?4 W# A# T- **Node.js**:安装 Node.js v16.x 或更高版本。
8 F! R! x( }; Z- **数据库**:PostgreSQL(推荐)或 MySQL。+ t/ ` l. Y8 g: c7 M# x# ?
- **DeepSeek API 密钥**:注册 DeepSeek 账户并获取 API 密钥。6 y4 w1 w9 Z9 { P
; E' l! ?& I$ A, M7 v
#### 1.2 安装依赖
% h& o# \3 x6 X s. y- 安装 Node.js 和 npm:, t4 j4 C5 x) ]2 v R
```bash
* @3 O& L4 d% y& ^8 T curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -; y: s9 f; {0 ~# f
sudo apt-get install -y nodejs
9 L$ k/ H; I1 \1 F ```
' ~* Z* m/ ]8 `1 M- 安装 PostgreSQL:
& s6 W! W4 ^ I* @/ s- V# o ```bash
) V9 p1 V( S& i sudo apt-get install postgresql postgresql-contrib( S! \% V7 E4 c3 E" E8 V
sudo systemctl start postgresql
K0 _# B' B/ P- W5 |1 E sudo systemctl enable postgresql' R& M0 C- p) j0 b. w0 g! x
```# }; q% _! y* h# [& C# q+ b
& [6 h8 p* v$ {* A8 W" D; h
---
5 \, @0 |' s* a+ D* H) {4 I$ g- v, K+ o8 j5 O( Z9 {' ^
### **2. 安装 NocoBase**
( t5 J, N! ^, x8 l: |7 |: k. i#### 2.1 下载 NocoBase
3 d9 d. X8 `9 Q3 r. d- 从 GitHub 克隆 NocoBase 仓库:
% j( K% z! O, Q2 t, k ```bash7 N9 Q1 s, @+ g0 T; g* Y9 y/ y& @
git clone https://github.com/nocobase/nocobase.git1 d6 D$ ?- m) @# q' @; M8 y
cd nocobase+ v- T2 a/ t) |+ \1 @& ]' j( j( L
```
" j# U* \# E& n9 A7 G. G, I3 |' c* Z0 j3 [9 I4 `: a
#### 2.2 安装依赖
% O. q: l0 j# u: ]4 U6 I! @- 安装项目依赖:
/ _$ F7 l+ }5 t" s ```bash. C7 ^2 V/ r# `$ g# {* i! S, W
npm install
( n* H; ^) S2 G6 Q1 M9 C ```9 \. @! T% |* S# ^% |! y, \
) P$ j. `9 l1 l, @; ?, Y#### 2.3 配置数据库
) }: p/ P& B7 }$ G& K {- 创建 PostgreSQL 数据库:
, F& x4 Z- Y% C/ {. L ```bash6 O; A f0 k- w" P1 m* I e) Q! B/ {
sudo -u postgres psql
6 e4 u# J$ e2 r; X% `$ j0 X CREATE DATABASE nocobase;( c1 _' Q W# L
CREATE USER nocobase_user WITH PASSWORD 'your_password';
" C, f& r! H: o- {1 V GRANT ALL PRIVILEGES ON DATABASE nocobase TO nocobase_user;
9 P1 z1 \* S f0 i8 g9 N& P \q
* T% ~) {. ~* O$ ~ ```
" x' ?/ H: u1 y# R7 j- T- 修改 NocoBase 配置文件:3 @3 T/ Q2 h5 X1 o% d+ r* X
编辑 `config/config.json`,配置数据库连接信息:
7 |& l8 s/ Z9 {' t ```json
# _9 t8 ? N/ R! S6 B4 N {- ]/ V4 I$ E) C& ^9 `
"database": {5 _7 E4 [& C9 m, M j
"dialect": "postgres",
0 Q6 n& f( G/ k5 t5 ` "host": "localhost",. {& A2 Q1 c; h' Y3 v6 u6 D' u
"port": 5432,
0 H$ }3 w m2 Y5 c9 L+ N "username": "nocobase_user",
1 R) d& U( V( b! A- _) f+ J0 o "password": "your_password",
* F$ G( x8 T6 [# t/ W "database": "nocobase"
/ r# E2 d' s. ?+ I }
: Q# u& }8 P1 f F; \ }- y z* U. Z. |
```
' B5 ?! i" `. V/ L" j6 N
' T& S% u# ?/ v#### 2.4 初始化数据库: v) D7 }% k _$ Y
- 运行数据库迁移脚本:7 x7 K2 I, o9 f2 C; ]
```bash; ^ Y0 s! B* m! y a5 s% w
npm run db:migrate, A6 i l( k; d3 ~& H4 E" h- f0 o
```
: |+ w. K( F: |# S4 C; b' B
4 d1 p1 X/ L" D3 }- q. w' M#### 2.5 启动 NocoBase
$ P( i- O+ z b: v6 @* A- 启动开发服务器:4 [: [2 M, p: p! }% d
```bash
- h1 ^( l) t/ W' _# q npm run start5 L6 e6 J" a8 c( _! c$ [
```0 [% @# ^/ a: }
- 访问 NocoBase:5 q( G# w- i: P: H+ m) Y
打开浏览器,访问 `http://localhost:3000`,完成初始设置。. e* \8 e D1 s9 E. N/ j' P
9 W% P4 C* c) t5 V---
* f1 s! Y! [; |9 Z/ o
* L0 w& K! y- X$ l) B- n& Z### **3. 集成 DeepSeek**9 g1 j! @) y& c7 y
#### 3.1 创建 NocoBase 插件
+ Y5 M5 Q& E; i# a% k$ R$ W- 使用 NocoBase CLI 创建插件:& L, N4 F0 v, e8 ]6 `2 O
```bash6 l% q1 Q. D4 K; e/ j
npm run cli plugin:create deepseek-integration
# N* ]% m$ d( P; Z* ~* b4 c cd plugins/deepseek-integration
( `0 z, V8 A) o7 I( K- H; i ```: B, N3 X( T0 _0 K" g8 N6 p* U
* O, U% x, \" V+ q#### 3.2 安装 DeepSeek SDK
9 l8 t5 e+ F5 ]: g8 K: g- 安装 DeepSeek 的 Node.js SDK(假设 DeepSeek 提供 SDK):
* _ H5 p: R9 c7 t" }1 y6 e ```bash
9 w+ {* |5 c. ]: n8 l! Z/ U8 V npm install deepseek-sdk
* L( w0 _5 @& q1 z/ e ```5 O. t1 M2 a/ E j
- ]" X$ V# P+ |; D) ~ v#### 3.3 配置 DeepSeek API 密钥
+ h ?' e& U: j2 D5 V Q- H( D6 K- 在插件配置文件中添加 DeepSeek API 密钥:
" a7 b( X1 l; {4 J9 Q7 i3 w 编辑 `plugins/deepseek-integration/config/config.json`:6 Q% p, \5 i8 U! }0 _
```json/ N: Q( `3 B" `6 c. u8 [
{
0 y- k! K/ ^0 E' ~0 R "deepseek": { u+ I( }; ]$ D$ a- J& y
"apiKey": "your_deepseek_api_key"
9 J: r$ c- V( g8 G# V' I) Z }
' ~; d9 }* u" o }
6 m9 o9 [# |/ O% M% Y ```
4 Y+ n" @0 W; W/ z, @
( }2 B. x2 b7 u$ W% l. e% j#### 3.4 实现 DeepSeek 服务% V2 i$ F: N& _6 |/ x5 |# B# J
- 创建服务文件 `plugins/deepseek-integration/src/services/deepseekService.js`:) ^( _; y f+ L, f
```javascript
. L4 r1 k- l: u7 }9 E const DeepSeek = require('deepseek-sdk');
3 r& W5 L! \1 Z/ y; k d3 ]6 p; Y$ B; [! J
class DeepSeekService {
6 E$ G% j! V% E# f. j constructor(apiKey) {
! H0 J' e6 a: T( g2 q' d3 E this.deepseek = new DeepSeek(apiKey);
. d# @! \- y8 v. k }
! C/ _. @; Y' X/ V6 t2 w( w" E$ d1 D( W4 T! t
async query(prompt) {
; J* w5 @5 o& o# U const response = await this.deepseek.complete({
' f6 E* `! n; u7 ?" Z! O6 N prompt: prompt,3 T# e3 T/ I! B
max_tokens: 150
6 o$ F6 `9 E1 ? });
. N9 y& A; Y j) l7 w return response.choices[0].text.trim();
) p' k9 {) m" n }
- h3 a8 T0 ]* p; b7 o- P& h }
' L. N2 X8 X& r: B9 l) l8 x0 F f1 i' d% E& q G) C
module.exports = DeepSeekService;: w5 a/ a3 d* v3 ?& s
```$ G# ~2 g- K* E0 t' e1 S$ c; A4 l
6 c0 i' O( V+ W$ N% z7 H#### 3.5 创建 API 路由$ h8 C+ k" H( T; O
- 创建路由文件 `plugins/deepseek-integration/src/routes/deepseekRoutes.js`:/ L2 `8 E% \, ^$ n3 H
```javascript9 H" {0 @, G' m B Y) H# H
const express = require('express');" T6 W% F0 c1 F
const DeepSeekService = require('../services/deepseekService');% N, S# g1 Y' K! X! z0 o9 p( t
const router = express.Router();# s( _6 w$ f; F" q: E( z5 c
const config = require('config');9 ~! W/ D! z( `" ^. Y
# T$ n6 G) B" I
const deepseekService = new DeepSeekService(config.deepseek.apiKey); Q. z P3 z0 E* f* D1 ]+ K
" h' Z% P% |& m |' y' I; A router.post('/query', async (req, res) => {+ k6 `" N) z$ g
const { prompt } = req.body;& z3 S( R c7 y+ p& I" T" ~
try {
" y. M0 A; Q5 X5 H0 l5 C' [ const result = await deepseekService.query(prompt);+ m1 A4 i8 S3 }$ O
res.json({ success: true, result });" B$ d5 a* c/ e8 e1 x
} catch (error) {
h9 c) A+ F! g7 \* ] res.status(500).json({ success: false, error: error.message });- h" [- b7 C# `( T
}7 O' ^1 d& z* I# J L1 F
});
; j% a7 s* A4 L$ a' P# V3 x% G7 P- S/ v W
module.exports = router;
/ [ x. w% N7 \( t ```
; d" Z* N& @% _7 Y) w& R) o! J2 y2 ?. D k. `% P
#### 3.6 注册插件
& c2 T9 L, Q4 B; P0 ]- 编辑插件入口文件 `plugins/deepseek-integration/src/index.js`:2 L: D9 C$ g& i2 Y+ M3 x3 o& V3 C O
```javascript! ]8 z) A/ q9 {' L9 f3 K! w; ^0 J
const deepseekRoutes = require('./routes/deepseekRoutes');* T4 d z" k* A' x+ U
' Z( b- f) g6 V5 M6 O module.exports = function(app) {3 g+ @5 U! m( T" }) h
app.use('/api/deepseek', deepseekRoutes);
% w; y4 q& o2 @- X( w };
. d+ }! W- s6 Q, X) ~& { ```) n+ T% `9 x& l; Z
" p; u- q% ^6 U' x+ [! n+ l; D
---- _+ G E" a, A- }& s( {
# A) C3 C7 n+ p### **4. 测试集成**' A7 M% \% Z' L/ J7 o
#### 4.1 启动 NocoBase/ m, p3 k6 J2 M0 N/ w, a0 U
- 启动 NocoBase:
6 \+ {* p0 i& s! X% [& w ```bash
) i, z. V3 p# \9 [3 n4 M: r npm run start, Q( W7 @+ Z! `$ ^( l
```/ ?. i3 A: `6 }. K+ ^( ^2 `3 `
8 m8 K5 R8 _# V7 I- ~# Z' k#### 4.2 测试 DeepSeek API
# H7 d) r3 }+ g* _- 使用 `curl` 或 Postman 测试 API:
. S4 O, s3 ]- u2 N$ h$ t ```bash
g6 o2 P, o4 b1 h1 L1 g1 Z curl -X POST http://localhost:3000/api/deepseek/query \' {3 [, {; p0 y, L
-H "Content-Type: application/json" \
5 Q% R, z7 f$ R7 @' B l) H -d '{"prompt": "显示最近三个月联系过的客户"}'' |, c$ r! G% W! U7 T( L
```# G& O! ^2 j; r: L
: ?5 _6 _! q- H. v4 i
--- \6 C9 n1 ^$ V" M( k8 v! ]
% Y: |/ ~% G1 J7 X) v# r
### **5. 部署到生产环境**
" P6 F* n& M- E2 J#### 5.1 使用 Docker 部署
" q1 D$ }8 T5 J" [, |- 创建 `docker-compose.yml` 文件:
7 a/ |) @$ L* k+ T; t3 ~- T& T ```yaml
8 \) e* D% p; C* y( r9 U, W version: '3'
; d- \' B! P& [" E! _# ` services:: V& C* q- X) J( m
app:1 r j* {3 U t
image: node:16
, @4 K$ x$ j1 V2 E# g4 Z container_name: nocobase
9 M& C$ ?3 p5 F working_dir: /app
/ `7 o9 q' i0 O7 p9 R: t volumes:# g' Z0 r! A+ e! D4 L
- .:/app$ c8 j- ]. m$ s; p; e; h0 t- {
ports:
# _& v, \, X* n+ ?# y - "3000:3000"$ o7 y& s; L, ?. t* J
environment:
) I {4 M1 ?' c/ ]. ~ - NODE_ENV=production
: H* E! h8 I$ D command: npm run start
4 @9 y q# e- K7 g Y db:
" z v6 J, b ~ image: postgres:13
& D! c& ]" f* C: A container_name: nocobase_db+ i- K4 ^/ S' E# }- V
environment:
+ g! a# g4 ?8 B6 P C. c0 | POSTGRES_USER: nocobase_user
* X: m6 |8 P3 G+ J$ \; s, x POSTGRES_PASSWORD: your_password
# L/ R; c; ~7 h9 U POSTGRES_DB: nocobase
7 m0 ^- T' L( K; n* I: R ports:
8 F, L/ r# i' |7 |5 n# B1 m- I0 v - "5432:5432"
$ f1 @! u4 u6 g% g0 e# [4 \ ```# ?$ h2 N( N+ K/ y0 n$ E
- 启动 Docker 容器:
7 \/ ]: ?4 ^; Z( R5 b8 g9 h- J ```bash F$ q( w E' C: t/ ?0 V; W2 j
docker-compose up -d8 Z3 R- l0 l5 }. r
```- t# V( j \* B+ ]7 A5 o
$ Y) m7 Z! _8 t/ [* R) Z#### 5.2 配置 HTTPS
- c% F9 X' m) L$ r+ k. i- 使用 Nginx 反向代理并配置 HTTPS:$ q" J6 b7 ~3 ?% R- b
```nginx
4 G& W+ `8 c1 e' t7 @1 I server {
( H& z5 N2 v2 T6 A listen 80;$ |9 L4 Y# h- y
server_name yourdomain.com;
$ c, u! _! K8 ]! K( [- O return 301 https://$host$request_uri;
0 k# e: c, k' g) {8 v }
, U$ M4 n, z# i) C% P+ |! w$ v
& x& B1 ~# f6 |/ n( P server {$ e) `; I, B8 e t
listen 443 ssl;
! A5 J* A- y) |) D |! e server_name yourdomain.com;
9 A0 g# v2 {' u* B) I7 E
8 {; R5 z2 g' t. y, c- n* r: s% @( C ssl_certificate /path/to/your/certificate.crt;0 ~: g" A) z, W `
ssl_certificate_key /path/to/your/private.key;
; O" n" S5 ]8 e6 C) Z% H
2 V- @! u- ^% ^% s' m- L* V9 O$ I location / {
# g1 A9 [' W7 z" M! z _. F proxy_pass http://localhost:3000;
6 }: A; C" O0 s5 f+ s proxy_set_header Host $host; i! n; t( d- U) z
proxy_set_header X-Real-IP $remote_addr;1 T* o0 c8 [ V" A8 d
}" ^7 |7 G# i. K' s- C% [" j5 R. l2 }
}( z9 @/ |( R: q/ `
```7 T5 o' h& M" [. t! F
: O9 Z! F, \% D o9 @
---
7 L+ q5 L. N% u& G9 c, Z5 t/ p
( \9 i5 ^2 D2 s8 i### **6. 总结**1 i3 w8 S- Q. i/ v3 c% L' [ e$ R
通过以上步骤,您已经成功将 DeepSeek 集成到 NocoBase 中,并可以通过 API 调用实现自然语言查询、智能建议等功能。这一集成显著提升了 NocoBase 的智能化水平,为用户提供了更高效的操作体验。 |
|