Skip to content

Commit

Permalink
增加支持通义千问
Browse files Browse the repository at this point in the history
  • Loading branch information
moyangzhan committed Feb 27, 2024
1 parent 0a2f2c1 commit ae26772
Show file tree
Hide file tree
Showing 48 changed files with 816 additions and 472 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
* 图片生成(文生图、修图、图生图)
* 提示词
* 额度控制
* 自定义openai secret_key
* 基于大模型的知识库(RAG)

![1691585301627](image/README/1691585301627.png "登录注册")
* 多模型随意切换

**AI聊天:**
![1691583184761](image/README/1691583184761.png)
Expand All @@ -31,14 +29,18 @@

![kb03](image/README/kb03.png)

体验网址:[http://www.aideepin.com](http://www.aideepin.com/)

接入的模型:ChatGPT 3.5,DALL-E 2
### 体验网址
[http://www.aideepin.com](http://www.aideepin.com/)

该仓库为后端服务,前端项目见[langchain4j-aideepin-web](https://github.com/moyangzhan/langchain4j-aideepin-web)
### 接入的模型:
* ChatGPT 3.5
* 通义千问
* DALL-E 2

### 技术

该仓库为后端服务,前端项目见[langchain4j-aideepin-web](https://github.com/moyangzhan/langchain4j-aideepin-web)

后端:

jdk17
Expand All @@ -61,10 +63,11 @@ vue3+typescript+pnpm

* 创建数据库aideepin
* 执行docs/create.sql
* 填充openai的secret\_key
* 填充openai的secretKey 或者 灵积模型的apiKey

```plaintext
update adi_sys_config set value = 'my_chatgpt_secret_key' where name = 'secret_key'
update adi_sys_config set value = '{"secret_key":"my_openai_secret_key"}' where name = 'openai_setting';
update adi_sys_config set value = '{"api_key":"my_dashcope_api_key"}' where name = 'dashscope_setting';
```

* 修改配置文件
Expand Down
9 changes: 3 additions & 6 deletions adi-bootstrap/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ logging:
file:
path: D:/data/logs

openai:
adi:
frontend-url: http://localhost:1002
backend-url: http://localhost:1002/api
proxy:
enable: true
host: 127.0.0.1
http-port: 1087

adi:
frontend-url: http://localhost:1002
backend-url: http://localhost:1002/api


local:
files: D:/data/files/
images: D:/data/images/
Expand Down
4 changes: 4 additions & 0 deletions adi-bootstrap/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ logging:
adi:
frontend-url: http://www.aideepin.com
backend-url: http://www.aideepin.com/api
proxy:
enable: false
host: 127.0.0.1
http-port: 1087

local:
files: /data/files/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public KnowledgeBaseItem info(@PathVariable String uuid) {
.one();
}

/**
* 知识点向量化
*
* @param uuid 知识点uuid
* @return
*/
@PostMapping("/embedding/{uuid}")
public boolean embedding(@PathVariable String uuid) {
return knowledgeBaseItemService.checkAndEmbedding(uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class KnowledgeBaseQAController {

@PostMapping("/ask/{kbUuid}")
public KnowledgeBaseQaRecord ask(@PathVariable String kbUuid, @RequestBody @Validated QAReq req) {
return knowledgeBaseService.answerAndRecord(kbUuid, req.getQuestion());
return knowledgeBaseService.answerAndRecord(kbUuid, req.getQuestion(), req.getModelName());
}

@GetMapping("/record/search")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.moyz.adi.chat.controller;

import com.moyz.adi.common.helper.ImageModelContext;
import com.moyz.adi.common.helper.LLMContext;
import com.moyz.adi.common.vo.ImageModelInfo;
import com.moyz.adi.common.vo.LLMModelInfo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/model")
public class ModelController {
@Operation(summary = "支持的大语言模型列表")
@GetMapping(value = "/llms")
public List<LLMModelInfo> llms() {
return LLMContext.NAME_TO_MODEL.values().stream().collect(Collectors.toList());
}

@Operation(summary = "支持的图片模型列表")
@GetMapping(value = "/imageModels")
public List<ImageModelInfo> imageModels() {
return ImageModelContext.NAME_TO_MODEL.values().stream().collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public static class GenerateImage {
}

public static class SysConfigKey {
public static final String SECRET_KEY = "secret_key";
public static final String OPENAI_SETTING = "openai_setting";
public static final String DASHSCOPE_SETTING = "dashscope_setting";
public static final String REQUEST_TEXT_RATE_LIMIT = "request_text_rate_limit";
public static final String REQUEST_IMAGE_RATE_LIMIT = "request_image_rate_limit";
public static final String CONVERSATION_MAX_NUM = "conversation_max_num";
Expand Down
2 changes: 2 additions & 0 deletions adi-common/src/main/java/com/moyz/adi/common/dto/AskReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public class AskReq {
* If not empty, it means will request AI with the exist prompt, param {@code prompt} is ignored
*/
private String regenerateQuestionUuid;

private String modelName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public class EditImageReq {
@Min(1)
@Max(10)
private int number;

private String modelName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public class GenerateImageReq {
@Min(1)
@Max(10)
private int number;

private String modelName;
}
2 changes: 2 additions & 0 deletions adi-common/src/main/java/com/moyz/adi/common/dto/QAReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public class QAReq {

@NotBlank
private String question;

private String modelName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public class VariationImageReq {
@Min(1)
@Max(10)
private int number;

private String modelName;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.moyz.adi.common.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.pgvector.PGvector;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -9,10 +11,10 @@
@Data
@TableName("adi_knowledge_base_embedding")
@Schema(title = "知识库-嵌入实体", description = "知识库嵌入表")
public class KnowledgeBaseEmbedding extends BaseEntity {
public class KnowledgeBaseEmbedding{

@Schema(title = "embedding uuid")
@TableField("embedding")
@Schema(title = "embedding_id")
@TableId(value = "embedding_id", type = IdType.AUTO)
private String embeddingId;

@Schema(title = "embedding")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public enum ErrorEnum {
A_IMAGE_SIZE_ERROR("A0010", "图片尺寸不对"),
A_FILE_NOT_EXIST("A0011", "文件不存在"),
A_DRAWING("A0012", "作图还未完成"),
A_REGISTER_USER_EXIST("A0013", "账号已经存在,请使用账号密码登录"),
A_USER_EXIST("A0013", "账号已经存在,请使用账号密码登录"),
A_FIND_PASSWORD_CODE_ERROR("A0014", "重置码已过期或不存在"),
A_USER_WAIT_CONFIRM("A0015", "用户未激活"),
A_USER_NOT_AUTH("A0016", "用户无权限"),
Expand All @@ -29,7 +29,8 @@ public enum ErrorEnum {
B_FIND_IMAGE_404("B0005", "无法找到图片"),
B_DAILY_QUOTA_USED("B0006", "今天额度已经用完"),
B_MONTHLY_QUOTA_USED("B0007", "当月额度已经用完"),

B_LLM_NOT_SUPPORT("B0008", "LLM不支持该功能"),
B_LLM_SECRET_KEY_NOT_SET("B0009", "LLM的secret key没设置"),
B_MESSAGE_NOT_FOUND("B0008", "消息不存在");

private String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
public class TokenFilter extends OncePerRequestFilter {

public static final String[] EXCLUDE_API = {
"/auth/"
"/auth/",
"/model/"
};

@Resource
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.moyz.adi.common.helper;

import com.moyz.adi.common.interfaces.AbstractImageModelService;
import com.moyz.adi.common.vo.ImageModelInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

import static dev.langchain4j.model.openai.OpenAiModelName.DALL_E_2;

/**
* image model service上下文类(策略模式)
*/
@Slf4j
public class ImageModelContext {

/**
* AI图片模型
*/
public static final Map<String, ImageModelInfo> NAME_TO_MODEL = new HashMap<>();

private AbstractImageModelService modelService;

public ImageModelContext() {
modelService = NAME_TO_MODEL.get(DALL_E_2).getModelService();
}

public ImageModelContext(String modelName) {
if (null == NAME_TO_MODEL.get(modelName)) {
log.warn("︿︿︿ Can not find {}, use the default model DALL_E_2 ︿︿︿", modelName);
modelService = NAME_TO_MODEL.get(DALL_E_2).getModelService();
} else {
modelService = NAME_TO_MODEL.get(modelName).getModelService();
}
}

public static void addImageModelService(String modelName, AbstractImageModelService modelService) {
ImageModelInfo imageModelInfo = new ImageModelInfo();
imageModelInfo.setModelService(modelService);
imageModelInfo.setModelName(modelName);
imageModelInfo.setEnable(modelService.isEnabled());
NAME_TO_MODEL.put(modelName, imageModelInfo);
}

public AbstractImageModelService getModelService() {
return modelService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.moyz.adi.common.helper;

import com.moyz.adi.common.interfaces.AbstractLLMService;
import com.moyz.adi.common.vo.LLMModelInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

import static dev.langchain4j.model.openai.OpenAiModelName.GPT_3_5_TURBO;

/**
* llmService上下文类(策略模式)
*/
@Slf4j
public class LLMContext {
public static final Map<String, LLMModelInfo> NAME_TO_MODEL = new HashMap<>();
private AbstractLLMService llmService;

public LLMContext() {
llmService = NAME_TO_MODEL.get(GPT_3_5_TURBO).getLlmService();
}

public LLMContext(String modelName) {
if (null == NAME_TO_MODEL.get(modelName)) {
log.warn("︿︿︿ Can not find {}, use the default model GPT_3_5_TURBO ︿︿︿", modelName);
llmService = NAME_TO_MODEL.get(GPT_3_5_TURBO).getLlmService();
} else {
llmService = NAME_TO_MODEL.get(modelName).getLlmService();
}
}

public static void addLLMService(String modelName, AbstractLLMService llmService) {
LLMModelInfo llmModelInfo = new LLMModelInfo();
llmModelInfo.setModelName(modelName);
llmModelInfo.setEnable(llmService.isEnabled());
llmModelInfo.setLlmService(llmService);
NAME_TO_MODEL.put(modelName, llmModelInfo);
}

public AbstractLLMService getLLMService() {
return llmService;
}
}
Loading

0 comments on commit ae26772

Please sign in to comment.