Skip to content

Commit 76ffe95

Browse files
committed
chore(openai): Refactor openai.New() function to use options for arguments.
- Add a new file `openai/options.go` - Replace `openai.New()` arguments with `openai.WithToken()`, `openai.WithModel()`, `openai.WithOrgID()`, and `openai.WithProxyURL()` options in `commit.go`
1 parent 1407cf7 commit 76ffe95

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

cmd/commit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ var commitCmd = &cobra.Command{
5555

5656
color.Green("Summarize the commit message use " + viper.GetString("openai.model") + " model")
5757
client, err := openai.New(
58-
viper.GetString("openai.api_key"),
59-
viper.GetString("openai.model"),
60-
viper.GetString("openai.org_id"),
61-
viper.GetString("openai.proxy"),
58+
openai.WithToken(viper.GetString("openai.api_key")),
59+
openai.WithModel(viper.GetString("openai.model")),
60+
openai.WithOrgID(viper.GetString("openai.org_id")),
61+
openai.WithProxyURL(viper.GetString("openai.proxy")),
6262
)
6363
if err != nil {
6464
return err

git/git.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func diffFiles() *exec.Cmd {
4545
"--ignore-all-space",
4646
"--diff-algorithm=minimal",
4747
"--function-context",
48+
"--unified=1",
4849
}
4950

5051
args = append(args, excludeFiles()...)

openai/openai.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,35 +107,43 @@ func (c *Client) Completion(
107107
}
108108

109109
// New for initialize OpenAI client interface.
110-
func New(token, model, orgID, proxyURL string) (*Client, error) {
110+
func New(opts ...Option) (*Client, error) {
111+
cfg := &config{}
112+
113+
// Loop through each option
114+
for _, o := range opts {
115+
// Call the option giving the instantiated
116+
o.apply(cfg)
117+
}
118+
111119
instance := &Client{}
112-
if token == "" {
120+
if cfg.token == "" {
113121
return nil, errors.New("missing api key")
114122
}
115123

116-
v, ok := modelMaps[model]
124+
v, ok := modelMaps[cfg.model]
117125
if !ok {
118126
return nil, errors.New("missing model")
119127
}
120128
instance.model = v
121129

122-
cfg := openai.DefaultConfig(token)
123-
if orgID != "" {
124-
cfg.OrgID = orgID
130+
c := openai.DefaultConfig(cfg.token)
131+
if cfg.orgID != "" {
132+
c.OrgID = cfg.orgID
125133
}
126134

127-
if proxyURL != "" {
135+
if cfg.proxyURL != "" {
128136
httpClient := &http.Client{
129137
Timeout: time.Second * 10,
130138
}
131-
proxy, _ := url.Parse(proxyURL)
139+
proxy, _ := url.Parse(cfg.proxyURL)
132140
httpClient.Transport = &http.Transport{
133141
Proxy: http.ProxyURL(proxy),
134142
}
135-
cfg.HTTPClient = httpClient
143+
c.HTTPClient = httpClient
136144
}
137145

138-
instance.client = openai.NewClientWithConfig(cfg)
146+
instance.client = openai.NewClientWithConfig(c)
139147

140148
return instance, nil
141149
}

openai/options.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package openai
2+
3+
// Option specifies instrumentation configuration options.
4+
type Option interface {
5+
apply(*config)
6+
}
7+
8+
var _ Option = (*optionFunc)(nil)
9+
10+
type optionFunc func(*config)
11+
12+
func (o optionFunc) apply(c *config) {
13+
o(c)
14+
}
15+
16+
func WithToken(val string) Option {
17+
return optionFunc(func(c *config) {
18+
c.token = val
19+
})
20+
}
21+
22+
func WithOrgID(val string) Option {
23+
return optionFunc(func(c *config) {
24+
c.orgID = val
25+
})
26+
}
27+
28+
func WithModel(val string) Option {
29+
return optionFunc(func(c *config) {
30+
c.model = val
31+
})
32+
}
33+
34+
func WithProxyURL(val string) Option {
35+
return optionFunc(func(c *config) {
36+
c.proxyURL = val
37+
})
38+
}
39+
40+
type config struct {
41+
token string
42+
orgID string
43+
model string
44+
proxyURL string
45+
}

0 commit comments

Comments
 (0)