Files
ai-agent/internal/ai/openai_client.go
T
mlogclub 976b9defde Refactor AI Agent and AI Config handling across multiple files
- Updated function signatures to accept AI Agent and AI Config as non-pointer types for better clarity and safety.
- Modified instances where AI Agent and AI Config were dereferenced to improve code readability.
- Removed unnecessary nil checks for AI Agent and AI Config, simplifying the logic.
- Adjusted related tests and services to align with the new function signatures.
- Cleaned up code in runtime, skills, and executor packages to ensure consistency in handling AI configurations.
2026-04-17 17:57:01 +08:00

38 lines
987 B
Go

package ai
import (
"time"
"github.com/mlogclub/simple/sqls"
openai "github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
"cs-agent/internal/models"
"cs-agent/internal/pkg/enums"
"cs-agent/internal/pkg/errorsx"
"cs-agent/internal/repositories"
)
func newOpenAIClient(config models.AIConfig) openai.Client {
opts := []option.RequestOption{
option.WithAPIKey(config.APIKey),
option.WithBaseURL(config.BaseURL),
}
if config.TimeoutMS > 0 {
opts = append(opts, option.WithRequestTimeout(time.Duration(config.TimeoutMS)*time.Millisecond))
}
if config.MaxRetryCount >= 0 {
opts = append(opts, option.WithMaxRetries(config.MaxRetryCount))
}
return openai.NewClient(opts...)
}
func GetEnabledAIConfig(modelType enums.AIModelType) (*models.AIConfig, error) {
item := repositories.AIConfigRepository.GetEnabled(sqls.DB(), modelType)
if item == nil {
return nil, errorsx.BusinessError(2005, "未配置可用的 AI 配置")
}
return item, nil
}