ai_scheduler/internal/config/config.go

152 lines
4.8 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"`
DefaultPrompt SysPrompt `mapstructure:"default_prompt"`
PermissionConfig PermissionConfig `mapstructure:"permissionConfig"`
// LLM *LLM `mapstructure:"llm"`
}
type SysPrompt struct {
ImgRecognize DefaultPrompt `mapstructure:"img_recognize"`
}
type DefaultPrompt struct {
SystemPrompt string `mapstructure:"system_prompt"`
UserPrompt string `mapstructure:"user_prompt"`
}
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"`
VlModel string `mapstructure:"vl_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"`
DingTalkBot ToolConfig `mapstructure:"dingTalkBot"`
//上游售后订单流水详情
ZltxOrderAfterSaleDetail ToolConfig `mapstructure:"zltxOrderAfterSaleDetail"`
//下游订单预检
ZltxOrderAfterSalePreCheck ToolConfig `mapstructure:"zltxOrderAfterSalePreCheck"`
// 上游订单售后
ZltxOrderAfterSaleSupplier ToolConfig `mapstructure:"zltxOrderAfterSaleSupplier"`
// 下游订单售后
ZltxOrderAfterSaleReseller ToolConfig `mapstructure:"zltxOrderAfterSaleReseller"`
}
// ToolConfig 单个工具配置
type ToolConfig struct {
Enabled bool `mapstructure:"enabled"`
BaseURL string `mapstructure:"base_url"`
APIKey string `mapstructure:"api_key"`
APISecret string `mapstructure:"api_secret"`
//附加地址
AddURL string `mapstructure:"add_url"`
}
// LoggingConfig 日志配置
type LoggingConfig struct {
Level string `mapstructure:"level"`
Format string `mapstructure:"format"`
}
// PermissionConfig 权限校验配置
type PermissionConfig struct {
// 获取权限的地址
PermissionURL string `mapstructure:"permission_url"`
}
// 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
}