Skip to content

Commit 4e832b4

Browse files
committed
feat(openai): implement openai interface
Signed-off-by: kasanatte <[email protected]>
1 parent 6787cee commit 4e832b4

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/karmada-io/karmada v1.15.0
1515
github.com/prometheus/client_golang v1.22.0
1616
github.com/prometheus/common v0.65.0
17+
github.com/sashabaranov/go-openai v1.41.2
1718
github.com/spf13/cobra v1.9.1
1819
github.com/spf13/pflag v1.0.6
1920
golang.org/x/net v0.40.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
210210
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
211211
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
212212
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
213+
github.com/sashabaranov/go-openai v1.41.2 h1:vfPRBZNMpnqu8ELsclWcAvF19lDNgh1t6TVfFFOPiSM=
214+
github.com/sashabaranov/go-openai v1.41.2/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
213215
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
214216
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
215217
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=

pkg/openai/config.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)