ai_scheduler/internal/config/config.go

217 lines
7.3 KiB
Go

package config
import (
"fmt"
"time"
"github.com/spf13/viper"
)
// Config 应用配置
type Config struct {
Server ServerConfig `mapstructure:"server"`
Ollama OllamaConfig `mapstructure:"ollama"`
Vllm VllmConfig `mapstructure:"vllm"`
Coze CozeConfig `mapstructure:"coze"`
Sys SysConfig `mapstructure:"sys"`
Tools ToolsConfig `mapstructure:"tools"`
EinoTools EinoToolsConfig `mapstructure:"eino_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"`
// DingTalkBots map[string]*DingTalkBot `mapstructure:"ding_talk_bots"`
}
type SysPrompt struct {
ImgRecognize DefaultPrompt `mapstructure:"img_recognize"`
}
type DefaultPrompt struct {
SystemPrompt string `mapstructure:"system_prompt"`
UserPrompt string `mapstructure:"user_prompt"`
}
type LLM struct {
Providers map[string]LLMProviderConfig `mapstructure:"providers"`
Capabilities map[string]LLMCapabilityConfig `mapstructure:"capabilities"`
}
type LLMProviderConfig struct {
Endpoint string `mapstructure:"endpoint"`
Timeout string `mapstructure:"timeout"`
Models []LLMModle `mapstructure:"models"`
}
type LLMModle struct {
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
Streaming bool `mapstructure:"streaming"`
Modalities []string `mapstructure:"modalities"`
MaxTokens int `mapstructure:"max_tokens"`
}
type LLMParameters struct {
Temperature float64 `mapstructure:"temperature"`
MaxTokens int `mapstructure:"max_tokens"`
Stream bool `mapstructure:"stream"`
}
type LLMCapabilityConfig struct {
Provider string `mapstructure:"provider"`
Model string `mapstructure:"model"`
Parameters LLMParameters `mapstructure:"parameters"`
}
// 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"`
HeartbeatInterval int `mapstructure:"heartbeat_interval"`
}
// 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"`
MappingModel string `mapstructure:"mapping_model"`
VlModel string `mapstructure:"vl_model"`
Timeout time.Duration `mapstructure:"timeout"`
}
type VllmConfig struct {
BaseURL string `mapstructure:"base_url"`
VlModel string `mapstructure:"vl_model"`
Timeout time.Duration `mapstructure:"timeout"`
Level string `mapstructure:"level"`
}
// CozeConfig Coze配置
type CozeConfig struct {
BaseURL string `mapstructure:"base_url"`
ApiKey string `mapstructure:"api_key"`
ApiSecret string `mapstructure:"api_secret"`
}
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"`
// 上游订单售后
ZltxOrderAfterSaleSupplier ToolConfig `mapstructure:"zltxOrderAfterSaleSupplier"`
// 下游订单售后
ZltxOrderAfterSaleReseller ToolConfig `mapstructure:"zltxOrderAfterSaleReseller"`
// 下游批充订单售后
ZltxOrderAfterSaleResellerBatch ToolConfig `mapstructure:"zltxOrderAfterSaleResellerBatch"`
// Coze 快递查询工具
CozeExpress ToolConfig `mapstructure:"cozeExpress"`
// Coze 公司查询工具
CozeCompany ToolConfig `mapstructure:"cozeCompany"`
}
// 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"`
}
// EinoToolsConfig eino tool 配置
type EinoToolsConfig struct {
// 货易通商品上传
HytProductUpload ToolConfig `mapstructure:"hytProductUpload"`
// 货易通供应商查询
HytSupplierSearch ToolConfig `mapstructure:"hytSupplierSearch"`
// 货易通仓库查询
HytWarehouseSearch ToolConfig `mapstructure:"hytWarehouseSearch"`
// 货易通商品添加
HytGoodsAdd ToolConfig `mapstructure:"hytGoodsAdd"`
// 货易通商品图片添加
HytGoodsMediaAdd ToolConfig `mapstructure:"hytGoodsMediaAdd"`
// 货易通商品分类添加
HytGoodsCategoryAdd ToolConfig `mapstructure:"hytGoodsCategoryAdd"`
// 货易通商品分类查询
HytGoodsCategorySearch ToolConfig `mapstructure:"hytGoodsCategorySearch"`
// 货易通商品品牌查询
HytGoodsBrandSearch ToolConfig `mapstructure:"hytGoodsBrandSearch"`
}
// 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
}