You've already forked DataMate
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
This commit is contained in:
@@ -5,6 +5,7 @@ import com.datamate.common.domain.model.base.BaseEntity;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* 模型配置实体类
|
||||
@@ -16,6 +17,7 @@ import lombok.Setter;
|
||||
@Setter
|
||||
@TableName("t_model_config")
|
||||
@Builder
|
||||
@ToString
|
||||
public class ModelConfig extends BaseEntity<String> {
|
||||
/**
|
||||
* 模型名称(如 qwen2)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.datamate.common.models.infrastructure.client;
|
||||
|
||||
import com.datamate.common.infrastructure.exception.BusinessException;
|
||||
import com.datamate.common.models.domain.entity.ModelConfig;
|
||||
import com.datamate.common.models.domain.entity.ModelType;
|
||||
import com.datamate.common.models.infrastructure.exception.ModelsErrorCode;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.embedding.EmbeddingModel;
|
||||
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 模型客户端接口
|
||||
@@ -15,6 +15,7 @@ import java.util.function.Consumer;
|
||||
* @author dallas
|
||||
* @since 2025-10-27
|
||||
*/
|
||||
@Slf4j
|
||||
public class ModelClient {
|
||||
public static <T> T invokeModel(ModelConfig modelConfig, Class<T> modelInterface) {
|
||||
return switch (modelConfig.getType()) {
|
||||
@@ -23,7 +24,7 @@ public class ModelClient {
|
||||
};
|
||||
}
|
||||
|
||||
private static EmbeddingModel invokeEmbeddingModel(ModelConfig modelConfig) {
|
||||
public static EmbeddingModel invokeEmbeddingModel(ModelConfig modelConfig) {
|
||||
return OpenAiEmbeddingModel.builder()
|
||||
.baseUrl(modelConfig.getBaseUrl())
|
||||
.apiKey(modelConfig.getApiKey())
|
||||
@@ -31,7 +32,7 @@ public class ModelClient {
|
||||
.build();
|
||||
}
|
||||
|
||||
private static ChatModel invokeChatModel(ModelConfig modelConfig) {
|
||||
public static ChatModel invokeChatModel(ModelConfig modelConfig) {
|
||||
return OpenAiChatModel.builder()
|
||||
.baseUrl(modelConfig.getBaseUrl())
|
||||
.apiKey(modelConfig.getApiKey())
|
||||
@@ -40,5 +41,24 @@ public class ModelClient {
|
||||
}
|
||||
|
||||
public static void checkHealth(ModelConfig modelConfig) {
|
||||
try {
|
||||
switch (modelConfig.getType()) {
|
||||
case CHAT -> checkChatModelHealth(modelConfig);
|
||||
case EMBEDDING -> checkEmbeddingModelHealth(modelConfig);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Model health check failed for modelConfig: {}", modelConfig, e);
|
||||
throw BusinessException.of(ModelsErrorCode.MODEL_HEALTH_CHECK_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkEmbeddingModelHealth(ModelConfig modelConfig) {
|
||||
EmbeddingModel embeddingModel = invokeEmbeddingModel(modelConfig);
|
||||
embeddingModel.embed("text");
|
||||
}
|
||||
|
||||
private static void checkChatModelHealth(ModelConfig modelConfig) {
|
||||
ChatModel chatModel = invokeChatModel(modelConfig);
|
||||
chatModel.chat("hello");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,12 @@ public enum ModelsErrorCode implements ErrorCode {
|
||||
/**
|
||||
* 模型配置已存在
|
||||
*/
|
||||
MODEL_CONFIG_ALREADY_EXISTS("model.0002", "模型配置已存在");
|
||||
MODEL_CONFIG_ALREADY_EXISTS("model.0002", "模型配置已存在"),
|
||||
|
||||
/**
|
||||
* 模型健康检查失败
|
||||
*/
|
||||
MODEL_HEALTH_CHECK_FAILED("model.0003", "模型健康检查失败");
|
||||
|
||||
private final String code;
|
||||
private final String message;
|
||||
|
||||
@@ -19,7 +19,7 @@ import java.util.List;
|
||||
* @since 2025-10-27
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/models")
|
||||
@RequestMapping("/models")
|
||||
@RequiredArgsConstructor
|
||||
public class ModelConfigController {
|
||||
private final ModelConfigApplicationService modelConfigApplicationService;
|
||||
@@ -40,7 +40,7 @@ public class ModelConfigController {
|
||||
* @return 模型列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public PagedResponse<ModelConfig> getModels(@RequestParam QueryModelRequest queryModelRequest) {
|
||||
public PagedResponse<ModelConfig> getModels(QueryModelRequest queryModelRequest) {
|
||||
return modelConfigApplicationService.getModels(queryModelRequest);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class ModelConfigController {
|
||||
* @return 模型详情
|
||||
*/
|
||||
@GetMapping("/{modelId}")
|
||||
public ModelConfig getModelDetail(@PathVariable String modelId) {
|
||||
public ModelConfig getModelDetail(@PathVariable("modelId") String modelId) {
|
||||
return modelConfigApplicationService.getModelDetail(modelId);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user