提前定义好模型名称,供后续使用

/**
 * 模型名称
 */
public final class AiModelName {

    public static final String XING_HUO = "xinghuo";

    public static final String DOUBAO = "doubao";

    public static final String TONGYI = "tongyi";

    public static final String MINIMAX = "mini";

    public static final String KIMI = "kimi";

    public static final String DEEPSEEK = "deepseek";

    public static final String ZHIPU = "zhipu";
}

定义AI枚举类

public enum AiModelEnums {
    DOU_BAO,//豆包
    XING_HUO,//星火
    TONG_YI,//通义
    DEEP_SEEK,
    YI_YAN,//文心一言
    KI_MI,
}

创建一个统一的业务接口

public interface AiModelHandler {
    void handle(IntegrateLargeModeReqVo vo, HttpServletResponse response, Boolean sse, HttpServletRequest request);
}

创建业务实现类(以DeepSeek为例)

@AiModelType(AiModelEnums.DEEP_SEEK)
@Component
public class DeepSeekHandler implements AiModelHandler {
    @Autowired
    private DeepSeekService deepSeekService;

    @Override
    public void handle(IntegrateLargeModeReqVo vo, HttpServletResponse response, Boolean sse, HttpServletRequest request){
        System.err.println("DEEPSEEK");
        if(sse){
            deepSeekService.deepSeekSSE(vo, request,response);
        }else{
            deepSeekService.deepseek(vo, request,response);
        }
    }
}

创建自定义注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AiModelType {
    AiModelEnums value();
}

创建业务工厂

@Component
public class ModelHandlerRegistry implements ApplicationContextAware {
    //首先定义一个静态Map用于存储不同的业务实现类。
    //key通过枚举类可对存入的数据进行初步限制
    //value则通过第二个方法进行赋值
    private static final Map<AiModelEnums, AiModelHandler> HANDLER_MAP = new HashMap<>();


    //ApplicationContext 在 Spring 框架中是一个接口。它的核心本质是 Spring 的 IoC 容器,负责管理应用中所有的 Bean
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //通过业务接口AiModelHandler获取所有实现类
        Map<String, AiModelHandler> beans = applicationContext.getBeansOfType(AiModelHandler.class);
        //遍历实现类
        for(AiModelHandler handler : beans.values()){
            //通过反射获取处理器类上的 @AiModelType 注解。与实现类的注解:@AiModelType(AiModelEnums.DEEP_SEEK),形成闭环
            AiModelType annotation = handler.getClass().getAnnotation(AiModelType.class);
            //不为空的情况下,将@AiModelType里的枚举值提取出来作为key,业务类作为value,完成整个策略+工厂的实现
            if(annotation != null){
                HANDLER_MAP.put(annotation.value(), handler);
            }
        }
    }

    //定义方法,通过枚举提供对外的实现类调用
    public static AiModelHandler getHandler(AiModelEnums modelType){
        return HANDLER_MAP.get(modelType);
    }
}

最终的调用举例

            try {
                AiModelEnums modelType = AiModelEnums.valueOf(reqModelType);
                AiModelHandler handler = ModelHandlerRegistry.getHandler(modelType);
                handler.handle(vo,response,flag,request);
            } catch (IllegalArgumentException e) {
                AiModelNotExistExceptionInfo(response);
            } catch (NullPointerException e) {
                AiModelUnderDevelopmentExceptionInfo(response);
            }

更多推荐