package com.webchat.service.ai;

import com.webchat.common.enums.LlmModelEnum;
import com.webchat.common.exception.BusinessException;
import com.webchat.common.util.SpringContextUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 程序员王七七 https://www.coderutil.com 网站作者
 * @date 2024/10/29 22:47
 *
 * 抽象大模型对话工厂服务
 */
@Component
public class LLMServiceFactory {

    @Autowired
    private WebApplicationContext webApplicationContext;

    private static final Map<String, AbstractLLMChatService> serviceMap = new HashMap<>();

    /**
     * 初始化模型服务
     */
    private void initServiceMap() {
        /**
         * kimi
         */
        serviceMap.put(LlmModelEnum.KIMI.getModel(), SpringContextUtil.getBean(KimiAIService.class));
        /**
         * deepseek
         */
        serviceMap.put(LlmModelEnum.DEEPSEEK.getModel(), SpringContextUtil.getBean(DeepSeekAIService.class));
    }

    public static AbstractLLMChatService getLLMService(String model) {
        AbstractLLMChatService llmChatService = serviceMap.get(model);
        if (llmChatService == null) {
            throw new BusinessException("不支持的模型");
        }
        return llmChatService;
    }

    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        initServiceMap();
    }

}
