package com.infoepoch.pms.agent.aiMode;

import com.infoepoch.pms.agent.properties.AiLocalProperties;
import com.infoepoch.pms.agent.properties.AiSiliconFlowProperties;
import com.infoepoch.pms.agent.properties.AiZhiYuProperties;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

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

/**
 * @author jiangyz
 * @date 2026/4/2 10:38
 * 硅基流动模型
 */
@Configuration
public class AiSiliconFlowConfiguration {

    @Bean("SiliconFlowOpenAiApi")
    OpenAiApi siliconFlowOpenAiApi(AiSiliconFlowProperties properties) {
        return OpenAiApi.builder()
                .baseUrl(properties.getBaseUrl())
                .apiKey(resolveApiKey(properties))
                .build();
    }

    @Bean("SiliconFlowChatModel")
    ChatModel SiliconFlowChatModel(@Qualifier("SiliconFlowOpenAiApi") OpenAiApi siliconFlowOpenAiApi,
                                   AiSiliconFlowProperties properties) {
        Map<String, Object> extraBody = new HashMap<>();
        extraBody.put("enable_thinking", false);//关闭思考
        return OpenAiChatModel.builder()
                .openAiApi(siliconFlowOpenAiApi)
                .defaultOptions(OpenAiChatOptions.builder()
                        .model(properties.getModel())
                        .extraBody(extraBody)
                        .temperature(properties.getTemperature())
                        .maxTokens(properties.getMaxTokens())
                        .build())
                .build();
    }

    private String resolveApiKey(AiSiliconFlowProperties properties) {
        return StringUtils.hasText(properties.getApiKey()) ? properties.getApiKey() : "EMPTY";
    }
}
