|
| 1 | +/* |
| 2 | +Copyright 2024 The Karmada Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package openai |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "sync" |
| 22 | + |
| 23 | + "github.com/sashabaranov/go-openai" |
| 24 | + "k8s.io/klog/v2" |
| 25 | + |
| 26 | + "github.com/karmada-io/dashboard/cmd/api/app/options" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + globalOpenAIOptions *options.Options |
| 31 | + openAIInitialized bool |
| 32 | + openAIMutex sync.RWMutex |
| 33 | +) |
| 34 | + |
| 35 | +// InitOpenAIConfig initializes OpenAI configuration from Options. |
| 36 | +func InitOpenAIConfig(opts *options.Options) { |
| 37 | + openAIMutex.Lock() |
| 38 | + defer openAIMutex.Unlock() |
| 39 | + |
| 40 | + globalOpenAIOptions = opts |
| 41 | + openAIInitialized = true |
| 42 | + |
| 43 | + klog.InfoS("OpenAI configuration initialized", |
| 44 | + "hasAPIKey", opts.OpenAIAPIKey != "", |
| 45 | + "model", opts.OpenAIModel, |
| 46 | + "endpoint", opts.OpenAIEndpoint) |
| 47 | +} |
| 48 | + |
| 49 | +// GetOpenAIClient returns a configured OpenAI client. |
| 50 | +func GetOpenAIClient() (*openai.Client, error) { |
| 51 | + openAIMutex.RLock() |
| 52 | + defer openAIMutex.RUnlock() |
| 53 | + |
| 54 | + if !openAIInitialized || globalOpenAIOptions == nil { |
| 55 | + return nil, errors.New("OpenAI not initialized, call InitOpenAIConfig first") |
| 56 | + } |
| 57 | + |
| 58 | + if globalOpenAIOptions.OpenAIAPIKey == "" { |
| 59 | + return nil, errors.New("OpenAI API key not configured") |
| 60 | + } |
| 61 | + |
| 62 | + config := openai.DefaultConfig(globalOpenAIOptions.OpenAIAPIKey) |
| 63 | + if globalOpenAIOptions.OpenAIEndpoint != "" { |
| 64 | + config.BaseURL = globalOpenAIOptions.OpenAIEndpoint |
| 65 | + } |
| 66 | + |
| 67 | + return openai.NewClientWithConfig(config), nil |
| 68 | +} |
| 69 | + |
| 70 | +// GetOpenAIModel returns the configured OpenAI model. |
| 71 | +func GetOpenAIModel() string { |
| 72 | + openAIMutex.RLock() |
| 73 | + defer openAIMutex.RUnlock() |
| 74 | + |
| 75 | + if !openAIInitialized || globalOpenAIOptions == nil || globalOpenAIOptions.OpenAIModel == "" { |
| 76 | + return openai.GPT3Dot5Turbo // default value |
| 77 | + } |
| 78 | + return globalOpenAIOptions.OpenAIModel |
| 79 | +} |
| 80 | + |
| 81 | +// IsOpenAIConfigured returns true if OpenAI is properly configured. |
| 82 | +func IsOpenAIConfigured() bool { |
| 83 | + openAIMutex.RLock() |
| 84 | + defer openAIMutex.RUnlock() |
| 85 | + |
| 86 | + return openAIInitialized && globalOpenAIOptions != nil && globalOpenAIOptions.OpenAIAPIKey != "" |
| 87 | +} |
0 commit comments