package com.lili.aitest;

import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;

@Configuration
public class OpenAIConfig {
    private static final Logger logger = LoggerFactory.getLogger(OpenAIConfig.class);

    @Value("${spring.ai.openai.api-key}")
    private String apiKey;

    @Value("${spring.ai.openai.base-url}")
    private String baseUrl;

    @Value("${spring.ai.openai.chat.options.model}")
    private String model;

    @Bean
    public OpenAiChatClient openAiChatClient() {
        OpenAiApi openAiApi = new OpenAiApi(baseUrl, apiKey);

        OpenAiChatOptions options = new OpenAiChatOptions();
        options.setModel(model);
        options.setFrequencyPenalty(0.0f);
        options.setMaxTokens(2000);

        return new OpenAiChatClient(openAiApi, options);
    }

    @PostConstruct
    public void init() {
        logger.info("OpenAI Configuration:");
        logger.info("Base URL: {}", baseUrl);
        logger.info("API Key: {}", apiKey);
        logger.info("Model: {}", model);
    }
}