git-subtree-dir: server git-subtree-mainline:c4e617ada7git-subtree-split:e64421295f
38 lines
1.2 KiB
Go
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
|
|
}
|