ModelConfig.java 1.54 KB
package com.xly.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import dev.langchain4j.model.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.time.Duration;

@Configuration
public class ModelConfig {

    @Value("${langchain4j.ollama.base-url}")
    private String chatModelUrl;

    @Value("${langchain4j.ollama.sql-model-name}")
    private String sqlModelName;

    /** NL2SQL 专用模型(QueryTool 经 AgentFactory 注入)。 */
    @Bean("sqlChatModel")
    public OllamaChatModel sqlChatModel() {
        return OllamaChatModel.builder()
                .baseUrl(chatModelUrl)
                .modelName(sqlModelName)
                .temperature(0.0)
                .topP(0.95)
                .numPredict(4096)
                .timeout(Duration.ofSeconds(120))
                .maxRetries(3)
                .build();
    }

    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return mapper;
    }
}