This commit is contained in:
parent
19662bdb27
commit
e5b8542529
270
README.md
270
README.md
@ -1,167 +1,161 @@
|
|||||||
<p align="center">
|
# 后端项目架构分析报告
|
||||||
<a href="https://midwayjs.org/" target="blank"><img src="https://cool-show.oss-cn-shanghai.aliyuncs.com/admin/logo.png" width="200" alt="Midway Logo" /></a>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p align="center">cool-admin(midway版)一个很酷的后台权限管理系统,开源免费,模块化、插件化、极速开发CRUD,方便快速构建迭代后台管理系统,支持serverless、docker、普通服务器等多种方式部署
|
|
||||||
到 <a href="https://cool-js.com" target="_blank">官网</a> 进一步了解。
|
|
||||||
<p align="center">
|
|
||||||
<a href="https://github.com/cool-team-official/cool-admin-midway/blob/master/LICENSE" target="_blank"><img src="https://img.shields.io/badge/license-MIT-green?style=flat-square" alt="GitHub license" />
|
|
||||||
<a href=""><img src="https://img.shields.io/github/package-json/v/cool-team-official/cool-admin-midway?style=flat-square" alt="GitHub tag"></a>
|
|
||||||
<img src="https://img.shields.io/github/last-commit/cool-team-official/cool-admin-midway?style=flat-square" alt="GitHub tag"></a>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
## 技术栈
|
## 技术栈
|
||||||
|
|
||||||
- 后端:**`node.js` `midway.js` `koa.js` `typescript`**
|
- **框架**: Midway.js 3.x (基于Node.js的企业级框架)
|
||||||
- 前端:**`vue.js` `element-plus` `jsx` `pinia` `vue-router`**
|
- **数据库**: MySQL + TypeORM
|
||||||
- 数据库:**`mysql` `postgresql` `sqlite`**
|
- **缓存**: Redis
|
||||||
|
- **AI集成**: LangChain
|
||||||
|
- **WebSocket**: Socket.IO
|
||||||
|
- **TypeScript**: 全栈TypeScript支持
|
||||||
|
|
||||||
如果你是前端,后端的这些技术选型对你是特别友好的,前端开发者可以较快速地上手。
|
## 系统架构
|
||||||
如果你是后端,Typescript 的语法又跟 java、php 等特别类似,一切看起来也是那么得熟悉。
|
|
||||||
|
|
||||||
如果你想使用java版本后端,请移步[cool-admin-java](https://cool-js.com/admin/java/introduce.html)
|
### 1. 目录结构
|
||||||
|
|
||||||
#### 官网
|
|
||||||
|
|
||||||
[https://cool-js.com](https://cool-js.com)
|
|
||||||
|
|
||||||
<!-- 在此次添加使用文档 -->
|
|
||||||
## 演示
|
|
||||||
|
|
||||||
[AI极速编码](https://cool-js.com/ai/introduce.html)
|
|
||||||
|
|
||||||
[https://show.cool-admin.com](https://show.cool-admin.com)
|
|
||||||
|
|
||||||
- 账户:admin
|
|
||||||
- 密码:123456
|
|
||||||
|
|
||||||
<img src="https://cool-show.oss-cn-shanghai.aliyuncs.com/admin/home-mini.png" alt="Admin Home"></a>
|
|
||||||
|
|
||||||
|
|
||||||
#### 项目前端
|
|
||||||
|
|
||||||
[https://github.com/cool-team-official/cool-admin-vue](https://github.com/cool-team-official/cool-admin-vue)
|
|
||||||
|
|
||||||
## 微信群
|
|
||||||
|
|
||||||
<img width="260" src="https://cool-show.oss-cn-shanghai.aliyuncs.com/admin/wechat.jpeg?v=1" alt="Admin Wechat"></a>
|
|
||||||
|
|
||||||
## 运行
|
|
||||||
|
|
||||||
#### 修改数据库配置,配置文件位于`src/config/config.local.ts`
|
|
||||||
|
|
||||||
以Mysql为例,其他数据库请参考[数据库配置文档](https://cool-js.com/admin/node/quick.html#%E6%95%B0%E6%8D%AE%E5%BA%93%E9%85%8D%E7%BD%AE)
|
|
||||||
|
|
||||||
Mysql(`>=5.7版本`),建议 8.0,node 版本(`>=16.x`),建议 18.x,首次启动会自动初始化并导入数据
|
|
||||||
|
|
||||||
```ts
|
|
||||||
// mysql,驱动已经内置,无需安装
|
|
||||||
typeorm: {
|
|
||||||
dataSource: {
|
|
||||||
default: {
|
|
||||||
type: 'mysql',
|
|
||||||
host: '127.0.0.1',
|
|
||||||
port: 3306,
|
|
||||||
username: 'root',
|
|
||||||
password: '123456',
|
|
||||||
database: 'cool',
|
|
||||||
// 自动建表 注意:线上部署的时候不要使用,有可能导致数据丢失
|
|
||||||
synchronize: true,
|
|
||||||
// 打印日志
|
|
||||||
logging: false,
|
|
||||||
// 字符集
|
|
||||||
charset: 'utf8mb4',
|
|
||||||
// 是否开启缓存
|
|
||||||
cache: true,
|
|
||||||
// 实体路径
|
|
||||||
entities: ['**/modules/*/entity'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
```
|
```
|
||||||
#### 安装依赖并运行
|
src/
|
||||||
|
├── config/ # 配置文件
|
||||||
```bash
|
├── modules/ # 业务模块
|
||||||
$ npm i
|
├── comm/ # 通用工具
|
||||||
$ npm run dev
|
└── plugins/ # 插件系统
|
||||||
$ open http://localhost:8001/
|
|
||||||
```
|
```
|
||||||
|
|
||||||
注: `npm i`如果安装失败可以尝试使用[cnpm](https://developer.aliyun.com/mirror/NPM?from=tnpm),或者切换您的镜像源,推荐使用[pnpm](https://pnpm.io/)
|
### 2. 核心模块
|
||||||
|
- 基础服务 (base)
|
||||||
|
- 用户管理 (user)
|
||||||
|
- 任务调度 (task)
|
||||||
|
- 消息服务 (chat)
|
||||||
|
- 文件处理 (space)
|
||||||
|
- 订单系统 (order)
|
||||||
|
|
||||||
## CURD(快速增删改查)
|
### 3. 中间件集成
|
||||||
|
- 跨域处理 (@midwayjs/cross-domain)
|
||||||
|
- 文件上传 (@midwayjs/upload)
|
||||||
|
- 数据验证 (@midwayjs/validate)
|
||||||
|
- 静态文件服务 (@midwayjs/static-file)
|
||||||
|
- 视图渲染 (@midwayjs/view-ejs)
|
||||||
|
|
||||||
大部分的后台管理系统,或者 API 服务都是对数据进行管理,所以可以看到大量的 CRUD 场景(增删改查),cool-admin 对此进行了大量地封装,让这块的编码量变得极其地少。
|
## 数据层设计
|
||||||
|
|
||||||
#### 新建一个数据表
|
1. **数据库设计**
|
||||||
|
- TypeORM实体映射
|
||||||
|
- 数据库连接池
|
||||||
|
- 事务管理
|
||||||
|
- 软删除支持
|
||||||
|
|
||||||
`src/modules/demo/entity/goods.ts`,项目启动数据库会自动创建该表,无需手动创建
|
2. **缓存策略**
|
||||||
|
- Redis缓存集成
|
||||||
|
- 多级缓存架构
|
||||||
|
- 缓存失效策略
|
||||||
|
|
||||||
```ts
|
3. **文件存储**
|
||||||
import { BaseEntity } from '@cool-midway/core';
|
- 本地文件存储
|
||||||
import { Column, Entity, Index } from 'typeorm';
|
- 支持云存储扩展
|
||||||
|
- 文件处理中间件
|
||||||
|
|
||||||
/**
|
## API设计
|
||||||
* 商品
|
|
||||||
*/
|
|
||||||
@Entity('demo_app_goods')
|
|
||||||
export class DemoAppGoodsEntity extends BaseEntity {
|
|
||||||
@Column({ comment: '标题' })
|
|
||||||
title: string;
|
|
||||||
|
|
||||||
@Column({ comment: '图片' })
|
1. **REST API**
|
||||||
pic: string;
|
- 标准HTTP方法
|
||||||
|
- 统一响应格式
|
||||||
|
- 版本控制
|
||||||
|
- 参数验证
|
||||||
|
|
||||||
@Column({ comment: '价格', type: 'decimal', precision: 5, scale: 2 })
|
2. **WebSocket**
|
||||||
price: number;
|
- 实时消息推送
|
||||||
}
|
- 房间管理
|
||||||
```
|
- 心跳检测
|
||||||
|
- 断线重连
|
||||||
|
|
||||||
#### 编写 api 接口
|
3. **AI服务集成**
|
||||||
|
- LangChain框架集成
|
||||||
|
- OpenAI API对接
|
||||||
|
- ChromaDB向量数据库
|
||||||
|
|
||||||
`src/modules/demo/controller/app/goods.ts`,快速编写 6 个 api 接口
|
## 安全特性
|
||||||
|
|
||||||
```ts
|
1. **认证授权**
|
||||||
import { CoolController, BaseController } from '@cool-midway/core';
|
- JWT认证
|
||||||
import { DemoAppGoodsEntity } from '../../entity/goods';
|
- 权限管理
|
||||||
|
- 角色控制
|
||||||
|
- 访问控制列表
|
||||||
|
|
||||||
/**
|
2. **数据安全**
|
||||||
* 商品
|
- 参数验证
|
||||||
*/
|
- SQL注入防护
|
||||||
@CoolController({
|
- XSS防护
|
||||||
api: ['add', 'delete', 'update', 'info', 'list', 'page'],
|
- CSRF防护
|
||||||
entity: DemoAppGoodsEntity,
|
|
||||||
})
|
|
||||||
export class DemoAppGoodsController extends BaseController {
|
|
||||||
/**
|
|
||||||
* 其他接口
|
|
||||||
*/
|
|
||||||
@Get('/other')
|
|
||||||
async other() {
|
|
||||||
return this.ok('hello, cool-admin!!!');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
这样我们就完成了 6 个接口的编写,对应的接口如下:
|
3. **系统安全**
|
||||||
|
- 请求限流
|
||||||
|
- 日志审计
|
||||||
|
- 敏感信息加密
|
||||||
|
- IP黑名单
|
||||||
|
|
||||||
- `POST /app/demo/goods/add` 新增
|
## 性能优化
|
||||||
- `POST /app/demo/goods/delete` 删除
|
|
||||||
- `POST /app/demo/goods/update` 更新
|
|
||||||
- `GET /app/demo/goods/info` 单个信息
|
|
||||||
- `POST /app/demo/goods/list` 列表信息
|
|
||||||
- `POST /app/demo/goods/page` 分页查询(包含模糊查询、字段全匹配等)
|
|
||||||
|
|
||||||
### 部署
|
1. **数据库优化**
|
||||||
|
- 连接池管理
|
||||||
|
- 查询优化
|
||||||
|
- 索引优化
|
||||||
|
- 缓存策略
|
||||||
|
|
||||||
[部署教程](https://cool-js.com/admin/node/other/deploy.html)
|
2. **应用优化**
|
||||||
|
- 异步处理
|
||||||
|
- 并发控制
|
||||||
|
- 内存管理
|
||||||
|
- 响应压缩
|
||||||
|
|
||||||
### 内置指令
|
## 运维特性
|
||||||
|
|
||||||
- 使用 `npm run lint` 来做代码风格检查。
|
1. **部署方案**
|
||||||
|
- PM2进程管理
|
||||||
|
- Docker容器化
|
||||||
|
- 环境配置
|
||||||
|
- 负载均衡
|
||||||
|
|
||||||
[midway]: https://midwayjs.org
|
2. **监控系统**
|
||||||
|
- 性能监控
|
||||||
|
- 错误追踪
|
||||||
|
- 日志收集
|
||||||
|
- 告警机制
|
||||||
|
|
||||||
### 低价服务器
|
## 待优化点
|
||||||
|
|
||||||
[阿里云、腾讯云、华为云低价云服务器,不限新老](https://cool-js.com/ad/server.html)
|
1. **架构优化**
|
||||||
|
- 服务拆分
|
||||||
|
- 微服务改造
|
||||||
|
- 队列系统
|
||||||
|
- 分布式架构
|
||||||
|
|
||||||
|
2. **性能提升**
|
||||||
|
- 缓存策略优化
|
||||||
|
- 数据库性能
|
||||||
|
- 并发处理
|
||||||
|
- 响应时间
|
||||||
|
|
||||||
|
3. **可维护性**
|
||||||
|
- 单元测试覆盖
|
||||||
|
- 文档完善
|
||||||
|
- 代码规范
|
||||||
|
- 错误处理
|
||||||
|
|
||||||
|
## 建议改进
|
||||||
|
|
||||||
|
1. **技术升级**
|
||||||
|
- 引入消息队列
|
||||||
|
- 完善监控系统
|
||||||
|
- 优化日志体系
|
||||||
|
- 增强安全机制
|
||||||
|
|
||||||
|
2. **架构改进**
|
||||||
|
- 服务解耦
|
||||||
|
- API网关
|
||||||
|
- 服务发现
|
||||||
|
- 容器编排
|
||||||
|
|
||||||
|
3. **开发流程**
|
||||||
|
- 自动化测试
|
||||||
|
- CI/CD完善
|
||||||
|
- 代码审查
|
||||||
|
- 版本管理
|
@ -171,4 +171,45 @@ export const ConfigLLM: { [key in LLMModelType]?: any } = {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
// deepseek
|
||||||
|
deepseek: {
|
||||||
|
// 通用配置
|
||||||
|
comm: {
|
||||||
|
apiKey: 'API密钥',
|
||||||
|
baseURL: 'https://api.deepseek.com/v1',
|
||||||
|
},
|
||||||
|
// 专有配置
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
field: 'model',
|
||||||
|
title: 'Deepseek',
|
||||||
|
select: [
|
||||||
|
'deepseek-chat',
|
||||||
|
'deepseek-coder',
|
||||||
|
'deepseek-chat-v1',
|
||||||
|
],
|
||||||
|
default: 'deepseek-chat',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'temperature',
|
||||||
|
type: 'number',
|
||||||
|
title: '温度',
|
||||||
|
default: 0.7,
|
||||||
|
enable: true,
|
||||||
|
max: 1,
|
||||||
|
min: 0.1,
|
||||||
|
supports: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'maxTokens',
|
||||||
|
type: 'number',
|
||||||
|
title: '最大token',
|
||||||
|
default: 2048,
|
||||||
|
enable: true,
|
||||||
|
max: 8192,
|
||||||
|
min: 1,
|
||||||
|
supports: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
@ -4,6 +4,7 @@ import { ChatZhipuAI } from '@langchain/community/chat_models/zhipuai';
|
|||||||
import { Provide } from '@midwayjs/core';
|
import { Provide } from '@midwayjs/core';
|
||||||
import { ChatOpenAI } from '@langchain/openai';
|
import { ChatOpenAI } from '@langchain/openai';
|
||||||
import { ChatOllama } from '@langchain/community/chat_models/ollama';
|
import { ChatOllama } from '@langchain/community/chat_models/ollama';
|
||||||
|
import { ChatDeepseek } from './models/deepseek';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 模型类型
|
* 模型类型
|
||||||
@ -19,6 +20,8 @@ export const CommModel = {
|
|||||||
zhipu: ChatZhipuAI,
|
zhipu: ChatZhipuAI,
|
||||||
// ollama 本地大模型
|
// ollama 本地大模型
|
||||||
ollama: ChatOllama,
|
ollama: ChatOllama,
|
||||||
|
// deepseek
|
||||||
|
deepseek: ChatDeepseek,
|
||||||
};
|
};
|
||||||
|
|
||||||
// LLM类型键
|
// LLM类型键
|
||||||
@ -34,9 +37,7 @@ export class NodeLLMModel {
|
|||||||
* @param name
|
* @param name
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async getModel(
|
async getModel(name: string) {
|
||||||
name: string
|
|
||||||
): Promise<ChatOpenAI | ChatMinimax | ChatAlibabaTongyi> {
|
|
||||||
return CommModel[name];
|
return CommModel[name];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
83
src/modules/flow/nodes/llm/models/deepseek.ts
Normal file
83
src/modules/flow/nodes/llm/models/deepseek.ts
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
import { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
||||||
|
import { BaseChatModelParams } from "@langchain/core/language_models/chat_models";
|
||||||
|
import { BaseMessage, AIMessage } from "@langchain/core/messages";
|
||||||
|
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
|
||||||
|
import { ChatCompletionResponse } from "./types";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deepseek模型参数接口
|
||||||
|
*/
|
||||||
|
export interface DeepseekInput extends BaseChatModelParams {
|
||||||
|
apiKey: string;
|
||||||
|
baseURL?: string;
|
||||||
|
temperature?: number;
|
||||||
|
maxTokens?: number;
|
||||||
|
modelName?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deepseek聊天模型实现
|
||||||
|
*/
|
||||||
|
export class ChatDeepseek extends BaseChatModel {
|
||||||
|
apiKey: string;
|
||||||
|
baseURL: string;
|
||||||
|
temperature: number;
|
||||||
|
maxTokens: number;
|
||||||
|
modelName: string;
|
||||||
|
|
||||||
|
constructor(fields?: Partial<DeepseekInput>) {
|
||||||
|
super(fields ?? {});
|
||||||
|
|
||||||
|
this.apiKey = fields?.apiKey ?? "";
|
||||||
|
this.baseURL = fields?.baseURL ?? "https://api.deepseek.com/v1";
|
||||||
|
this.temperature = fields?.temperature ?? 0.7;
|
||||||
|
this.maxTokens = fields?.maxTokens ?? 2048;
|
||||||
|
this.modelName = fields?.modelName ?? "deepseek-chat";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成聊天补全
|
||||||
|
*/
|
||||||
|
async _generate(
|
||||||
|
messages: BaseMessage[],
|
||||||
|
options: this["ParsedCallOptions"],
|
||||||
|
runManager?: CallbackManagerForLLMRun
|
||||||
|
) {
|
||||||
|
const response = await fetch(`${this.baseURL}/chat/completions`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Authorization": `Bearer ${this.apiKey}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: this.modelName,
|
||||||
|
messages: messages.map(message => ({
|
||||||
|
role: message._getType() === "human" ? "user" : "assistant",
|
||||||
|
content: message.content,
|
||||||
|
})),
|
||||||
|
temperature: this.temperature,
|
||||||
|
max_tokens: this.maxTokens,
|
||||||
|
stream: false,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Deepseek API 请求失败: ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json() as ChatCompletionResponse;
|
||||||
|
const text = data.choices[0].message.content;
|
||||||
|
|
||||||
|
return {
|
||||||
|
generations: [{
|
||||||
|
text,
|
||||||
|
message: new AIMessage(text),
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 获取默认参数 */
|
||||||
|
_llmType(): string {
|
||||||
|
return "deepseek";
|
||||||
|
}
|
||||||
|
}
|
19
src/modules/flow/nodes/llm/models/types.ts
Normal file
19
src/modules/flow/nodes/llm/models/types.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
export interface ChatCompletionResponse {
|
||||||
|
id: string;
|
||||||
|
object: string;
|
||||||
|
created: number;
|
||||||
|
model: string;
|
||||||
|
choices: {
|
||||||
|
message: {
|
||||||
|
role: string;
|
||||||
|
content: string;
|
||||||
|
};
|
||||||
|
finish_reason: string;
|
||||||
|
index: number;
|
||||||
|
}[];
|
||||||
|
usage: {
|
||||||
|
prompt_tokens: number;
|
||||||
|
completion_tokens: number;
|
||||||
|
total_tokens: number;
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user