Files
slogan/server/styleagent/agent/agent_config.go
T
admin a6de9ebd12 Add 'server/' from commit 'e64421295fff83acbb6d6ab3d3b27f3ef8368f00'
git-subtree-dir: server
git-subtree-mainline: c4e617ada7
git-subtree-split: e64421295f
2026-08-04 15:02:35 +08:00

38 lines
1.2 KiB
Go

package agent
import (
"context"
"fmt"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcache"
)
var modelCfgCache = gcache.New()
// GetModelConfig 从 config.yml 读取 LLM 配置(缓存 60s),未配置返回明确错误
func GetModelConfig(ctx context.Context) (*ModelConfig, error) {
cacheKey := "llm:model_config"
v, err := modelCfgCache.Get(ctx, cacheKey)
if err == nil && !v.IsNil() {
if cfg, ok := v.Val().(*ModelConfig); ok {
return cfg, nil
}
}
cfg := &ModelConfig{
BaseURL: g.Cfg().MustGet(ctx, "llm.base_url", "").String(),
APIKey: g.Cfg().MustGet(ctx, "llm.api_key", "").String(),
ModelName: g.Cfg().MustGet(ctx, "llm.model_name", "").String(),
MaxTokens: g.Cfg().MustGet(ctx, "llm.max_tokens", 4096).Int(),
Temperature: g.Cfg().MustGet(ctx, "llm.temperature", 0.8).Float32(),
Timeout: time.Duration(g.Cfg().MustGet(ctx, "chat.timeout", 300).Int()) * time.Second,
MaxRetries: g.Cfg().MustGet(ctx, "chat.max_retries", 3).Int(),
}
if cfg.APIKey == "" || cfg.ModelName == "" || cfg.BaseURL == "" {
return nil, fmt.Errorf("LLM 未配置:请在 config.yml 设置 llm.base_url / llm.api_key / llm.model_name")
}
_ = modelCfgCache.Set(ctx, cacheKey, cfg, 60*time.Second)
return cfg, nil
}