124 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			124 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Go
		
	
	
	
package config
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"time"
 | 
						|
 | 
						|
	"github.com/spf13/viper"
 | 
						|
)
 | 
						|
 | 
						|
// Config 应用配置
 | 
						|
type Config struct {
 | 
						|
	Server  ServerConfig  `mapstructure:"server"`
 | 
						|
	Ollama  OllamaConfig  `mapstructure:"ollama"`
 | 
						|
	Sys     SysConfig     `mapstructure:"sys"`
 | 
						|
	Tools   ToolsConfig   `mapstructure:"tools"`
 | 
						|
	Logging LoggingConfig `mapstructure:"logging"`
 | 
						|
	Redis   Redis         `mapstructure:"redis"`
 | 
						|
	DB      DB            `mapstructure:"db"`
 | 
						|
	//	LLM     *LLM          `mapstructure:"llm"`
 | 
						|
}
 | 
						|
 | 
						|
type LLM struct {
 | 
						|
	Model string `mapstructure:"model"`
 | 
						|
}
 | 
						|
 | 
						|
// SysConfig 系统配置
 | 
						|
type SysConfig struct {
 | 
						|
	SessionLen      int `mapstructure:"session_len"`
 | 
						|
	ChannelPoolLen  int `mapstructure:"channel_pool_len"`
 | 
						|
	ChannelPoolSize int `mapstructure:"channel_pool_size"`
 | 
						|
	LlmPoolLen      int `mapstructure:"llm_pool_len"`
 | 
						|
}
 | 
						|
 | 
						|
// ServerConfig 服务器配置
 | 
						|
type ServerConfig struct {
 | 
						|
	Port int    `mapstructure:"port"`
 | 
						|
	Host string `mapstructure:"host"`
 | 
						|
}
 | 
						|
 | 
						|
// OllamaConfig Ollama配置
 | 
						|
type OllamaConfig struct {
 | 
						|
	BaseURL       string        `mapstructure:"base_url"`
 | 
						|
	Model         string        `mapstructure:"model"`
 | 
						|
	GenerateModel string        `mapstructure:"generate_model"`
 | 
						|
	Timeout       time.Duration `mapstructure:"timeout"`
 | 
						|
}
 | 
						|
 | 
						|
type Redis struct {
 | 
						|
	Host        string `mapstructure:"host"`
 | 
						|
	Type        string `mapstructure:"type"`
 | 
						|
	Pass        string `mapstructure:"pass"`
 | 
						|
	Key         string `mapstructure:"key"`
 | 
						|
	Tls         int32  `mapstructure:"tls"`
 | 
						|
	Db          int32  `mapstructure:"db"`
 | 
						|
	MaxIdle     int32  `mapstructure:"maxIdle"`
 | 
						|
	PoolSize    int32  `mapstructure:"poolSize"`
 | 
						|
	MaxIdleTime int32  `mapstructure:"maxIdleTime"`
 | 
						|
}
 | 
						|
 | 
						|
type DB struct {
 | 
						|
	Driver      string `mapstructure:"driver"`
 | 
						|
	Source      string `mapstructure:"source"`
 | 
						|
	MaxIdle     int32  `mapstructure:"maxIdle"`
 | 
						|
	MaxOpen     int32  `mapstructure:"maxOpen"`
 | 
						|
	MaxLifetime int32  `mapstructure:"maxLifetime"`
 | 
						|
	IsDebug     bool   `mapstructure:"isDebug"`
 | 
						|
}
 | 
						|
 | 
						|
// ToolsConfig 工具配置
 | 
						|
type ToolsConfig struct {
 | 
						|
	Weather            ToolConfig `mapstructure:"weather"`
 | 
						|
	Calculator         ToolConfig `mapstructure:"calculator"`
 | 
						|
	ZltxOrderDetail    ToolConfig `mapstructure:"zltxOrderDetail"`
 | 
						|
	ZltxOrderDirectLog ToolConfig `mapstructure:"zltxOrderDirectLog"`
 | 
						|
	Knowledge          ToolConfig `mapstructure:"knowledge"`
 | 
						|
	//通过ID获取我们的商品信息
 | 
						|
	ZltxProduct ToolConfig `mapstructure:"zltxProduct"`
 | 
						|
	//通过账号获取订单统计信息
 | 
						|
	ZltxOrderStatistics ToolConfig `mapstructure:"zltxOrderStatistics"`
 | 
						|
}
 | 
						|
 | 
						|
// ToolConfig 单个工具配置
 | 
						|
type ToolConfig struct {
 | 
						|
	Enabled bool   `mapstructure:"enabled"`
 | 
						|
	BaseURL string `mapstructure:"base_url"`
 | 
						|
	APIKey  string `mapstructure:"api_key"`
 | 
						|
	//附加地址
 | 
						|
	AddURL string `mapstructure:"add_url"`
 | 
						|
}
 | 
						|
 | 
						|
// LoggingConfig 日志配置
 | 
						|
type LoggingConfig struct {
 | 
						|
	Level  string `mapstructure:"level"`
 | 
						|
	Format string `mapstructure:"format"`
 | 
						|
}
 | 
						|
 | 
						|
// LoadConfig 加载配置
 | 
						|
func LoadConfig(configPath string) (*Config, error) {
 | 
						|
	viper.SetConfigFile(configPath)
 | 
						|
	viper.SetConfigType("yaml")
 | 
						|
 | 
						|
	// 设置默认值
 | 
						|
	//viper.SetDefault("server.port", "8080")
 | 
						|
	//viper.SetDefault("server.host", "0.0.0.0")
 | 
						|
	//viper.SetDefault("ollama.base_url", "http://localhost:11434")
 | 
						|
	//viper.SetDefault("ollama.model", "llama2")
 | 
						|
	//viper.SetDefault("ollama.timeout", "30s")
 | 
						|
	//viper.SetDefault("logging.level", "info")
 | 
						|
	//viper.SetDefault("logging.format", "json")
 | 
						|
 | 
						|
	// 读取配置文件
 | 
						|
	if err := viper.ReadInConfig(); err != nil {
 | 
						|
		return nil, fmt.Errorf("failed to read config file: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	// 解析配置
 | 
						|
	var config Config
 | 
						|
	if err := viper.Unmarshal(&config); err != nil {
 | 
						|
		return nil, fmt.Errorf("failed to unmarshal config: %w", err)
 | 
						|
	}
 | 
						|
 | 
						|
	return &config, nil
 | 
						|
}
 |