Skip to content

Commit 1a6e257

Browse files
authored
feat: add model health check functionality and improve model configuration (#30)
* refactor: rename artifactId and application name to 'datamate'; add model configuration and related services * refactor: simplify package scanning by using wildcard for mapper packages * feat: add model health check functionality and improve model configuration
1 parent acafe70 commit 1a6e257

File tree

6 files changed

+52
-10
lines changed

6 files changed

+52
-10
lines changed

backend/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@
207207
<artifactId>spring-boot-maven-plugin</artifactId>
208208
<version>${spring-boot.version}</version>
209209
</plugin>
210+
<plugin>
211+
<groupId>org.apache.maven.plugins</groupId>
212+
<artifactId>maven-compiler-plugin</artifactId>
213+
<version>3.11.0</version>
214+
<configuration>
215+
<source>${maven.compiler.source}</source>
216+
<target>${maven.compiler.target}</target>
217+
<parameters>true</parameters>
218+
<compilerArgs>
219+
<arg>-parameters</arg>
220+
</compilerArgs>
221+
</configuration>
222+
</plugin>
210223
</plugins>
211224
</build>
212225
</project>

backend/shared/domain-common/src/main/java/com/datamate/common/models/domain/entity/ModelConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.Builder;
66
import lombok.Getter;
77
import lombok.Setter;
8+
import lombok.ToString;
89

910
/**
1011
* 模型配置实体类
@@ -16,6 +17,7 @@
1617
@Setter
1718
@TableName("t_model_config")
1819
@Builder
20+
@ToString
1921
public class ModelConfig extends BaseEntity<String> {
2022
/**
2123
* 模型名称(如 qwen2)
Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.datamate.common.models.infrastructure.client;
22

3+
import com.datamate.common.infrastructure.exception.BusinessException;
34
import com.datamate.common.models.domain.entity.ModelConfig;
4-
import com.datamate.common.models.domain.entity.ModelType;
5+
import com.datamate.common.models.infrastructure.exception.ModelsErrorCode;
56
import dev.langchain4j.model.chat.ChatModel;
67
import dev.langchain4j.model.embedding.EmbeddingModel;
78
import dev.langchain4j.model.openai.OpenAiChatModel;
89
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
9-
10-
import java.util.function.Consumer;
10+
import lombok.extern.slf4j.Slf4j;
1111

1212
/**
1313
* 模型客户端接口
1414
*
1515
* @author dallas
1616
* @since 2025-10-27
1717
*/
18+
@Slf4j
1819
public class ModelClient {
1920
public static <T> T invokeModel(ModelConfig modelConfig, Class<T> modelInterface) {
2021
return switch (modelConfig.getType()) {
@@ -23,15 +24,15 @@ public static <T> T invokeModel(ModelConfig modelConfig, Class<T> modelInterface
2324
};
2425
}
2526

26-
private static EmbeddingModel invokeEmbeddingModel(ModelConfig modelConfig) {
27+
public static EmbeddingModel invokeEmbeddingModel(ModelConfig modelConfig) {
2728
return OpenAiEmbeddingModel.builder()
2829
.baseUrl(modelConfig.getBaseUrl())
2930
.apiKey(modelConfig.getApiKey())
3031
.modelName(modelConfig.getModelName())
3132
.build();
3233
}
3334

34-
private static ChatModel invokeChatModel(ModelConfig modelConfig) {
35+
public static ChatModel invokeChatModel(ModelConfig modelConfig) {
3536
return OpenAiChatModel.builder()
3637
.baseUrl(modelConfig.getBaseUrl())
3738
.apiKey(modelConfig.getApiKey())
@@ -40,5 +41,24 @@ private static ChatModel invokeChatModel(ModelConfig modelConfig) {
4041
}
4142

4243
public static void checkHealth(ModelConfig modelConfig) {
44+
try {
45+
switch (modelConfig.getType()) {
46+
case CHAT -> checkChatModelHealth(modelConfig);
47+
case EMBEDDING -> checkEmbeddingModelHealth(modelConfig);
48+
}
49+
} catch (Exception e) {
50+
log.error("Model health check failed for modelConfig: {}", modelConfig, e);
51+
throw BusinessException.of(ModelsErrorCode.MODEL_HEALTH_CHECK_FAILED);
52+
}
53+
}
54+
55+
private static void checkEmbeddingModelHealth(ModelConfig modelConfig) {
56+
EmbeddingModel embeddingModel = invokeEmbeddingModel(modelConfig);
57+
embeddingModel.embed("text");
58+
}
59+
60+
private static void checkChatModelHealth(ModelConfig modelConfig) {
61+
ChatModel chatModel = invokeChatModel(modelConfig);
62+
chatModel.chat("hello");
4363
}
4464
}

backend/shared/domain-common/src/main/java/com/datamate/common/models/infrastructure/exception/ModelsErrorCode.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ public enum ModelsErrorCode implements ErrorCode {
2020
/**
2121
* 模型配置已存在
2222
*/
23-
MODEL_CONFIG_ALREADY_EXISTS("model.0002", "模型配置已存在");
23+
MODEL_CONFIG_ALREADY_EXISTS("model.0002", "模型配置已存在"),
24+
25+
/**
26+
* 模型健康检查失败
27+
*/
28+
MODEL_HEALTH_CHECK_FAILED("model.0003", "模型健康检查失败");
2429

2530
private final String code;
2631
private final String message;

backend/shared/domain-common/src/main/java/com/datamate/common/models/interfaces/rest/ModelConfigController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @since 2025-10-27
2020
*/
2121
@RestController
22-
@RequestMapping("/api/models")
22+
@RequestMapping("/models")
2323
@RequiredArgsConstructor
2424
public class ModelConfigController {
2525
private final ModelConfigApplicationService modelConfigApplicationService;
@@ -40,7 +40,7 @@ public List<ModelConfig> getProviders() {
4040
* @return 模型列表
4141
*/
4242
@GetMapping("/list")
43-
public PagedResponse<ModelConfig> getModels(@RequestParam QueryModelRequest queryModelRequest) {
43+
public PagedResponse<ModelConfig> getModels(QueryModelRequest queryModelRequest) {
4444
return modelConfigApplicationService.getModels(queryModelRequest);
4545
}
4646

@@ -51,7 +51,7 @@ public PagedResponse<ModelConfig> getModels(@RequestParam QueryModelRequest quer
5151
* @return 模型详情
5252
*/
5353
@GetMapping("/{modelId}")
54-
public ModelConfig getModelDetail(@PathVariable String modelId) {
54+
public ModelConfig getModelDetail(@PathVariable("modelId") String modelId) {
5555
return modelConfigApplicationService.getModelDetail(modelId);
5656
}
5757

scripts/db/model-management-init.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
USE datamate;
2+
13
CREATE TABLE t_model_config
24
(
3-
id VARCHAR(36) AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID',
5+
id VARCHAR(36) PRIMARY KEY COMMENT '主键ID',
46
model_name VARCHAR(100) NOT NULL COMMENT '模型名称(如 qwen2)',
57
provider VARCHAR(50) NOT NULL COMMENT '模型提供商(如 Ollama、OpenAI、DeepSeek)',
68
base_url VARCHAR(255) NOT NULL COMMENT 'API 基础地址',

0 commit comments

Comments
 (0)