5d7c10aeab
- Updated runtime configuration to use __CS_AI_AGENT_WIDGET_CONFIG__ instead of __CS_AGENT_WIDGET_CONFIG__. - Changed message types in support host bridge from "cs-agent" to "cs-ai-agent". - Minified SDK script updated to reflect new AI agent naming conventions. - Adjusted scrollbar styles in main.scss to use .cs-ai-agent-scrollbar instead of .cs-agent-scrollbar.
38 lines
999 B
Go
38 lines
999 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-ai-agent/internal/models"
|
|
"cs-ai-agent/internal/pkg/enums"
|
|
"cs-ai-agent/internal/pkg/errorsx"
|
|
"cs-ai-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
|
|
}
|