2026-04-09 10:01:23 +08:00
|
|
|
package ai
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/mlogclub/simple/sqls"
|
|
|
|
|
openai "github.com/openai/openai-go/v3"
|
|
|
|
|
"github.com/openai/openai-go/v3/option"
|
|
|
|
|
|
2026-05-31 18:43:48 +08:00
|
|
|
"agent-desk/internal/models"
|
|
|
|
|
"agent-desk/internal/pkg/enums"
|
|
|
|
|
"agent-desk/internal/pkg/errorsx"
|
|
|
|
|
"agent-desk/internal/repositories"
|
2026-04-09 10:01:23 +08:00
|
|
|
)
|
|
|
|
|
|
2026-04-17 17:57:01 +08:00
|
|
|
func newOpenAIClient(config models.AIConfig) openai.Client {
|
2026-04-09 10:01:23 +08:00
|
|
|
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 {
|
2026-06-02 21:10:17 +08:00
|
|
|
return nil, errorsx.BusinessErrorI18n(2005, "error.aiConfig.noneEnabled")
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
return item, nil
|
|
|
|
|
}
|