Skip to content

Commit

Permalink
refactor: client initialization across multiple files (#175)
Browse files Browse the repository at this point in the history
- Remove `errors` import from `commit.go`
- Replace inline client initialization with `GetClient` function in `commit.go`
- Add `errors` and `core` imports to `openai.go`
- Implement `GetClient` function in `openai.go` to handle different providers
- Add `core` import to `review.go`
- Replace inline client initialization with `GetClient` function in `review.go`
- Move model logging to after client initialization in `review.go`

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Jun 2, 2024
1 parent 6a7b4b2 commit 2022f9f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
13 changes: 1 addition & 12 deletions cmd/commit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"errors"
"html"
"os"
"path"
Expand Down Expand Up @@ -90,17 +89,7 @@ var commitCmd = &cobra.Command{

// check provider
provider := core.Platform(viper.GetString("openai.provider"))
if !provider.IsValid() {
return errors.New("invalid provider")
}

var client core.Generative
switch provider {
case core.Gemini:
// TODO: implement Gemini
case core.OpenAI, core.Azure:
client, err = NewOpenAI()
}
client, err := GetClient(provider)
if err != nil && !promptOnly {
return err
}
Expand Down
14 changes: 14 additions & 0 deletions cmd/openai.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"errors"

"github.com/appleboy/CodeGPT/core"
"github.com/appleboy/CodeGPT/openai"

"github.com/spf13/viper"
Expand All @@ -26,3 +29,14 @@ func NewOpenAI() (*openai.Client, error) {
openai.WithPresencePenalty(float32(viper.GetFloat64("openai.presence_penalty"))),
)
}

// GetClient returns the generative client based on the platform
func GetClient(p core.Platform) (core.Generative, error) {
switch p {
case core.Gemini:
// TODO: implement Gemini
case core.OpenAI, core.Azure:
return NewOpenAI()
}
return nil, errors.New("invalid provider")
}
10 changes: 7 additions & 3 deletions cmd/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"
"strings"

"github.com/appleboy/CodeGPT/core"
"github.com/appleboy/CodeGPT/git"
"github.com/appleboy/CodeGPT/prompt"
"github.com/appleboy/CodeGPT/util"
Expand Down Expand Up @@ -53,13 +54,16 @@ var reviewCmd = &cobra.Command{
viper.Set("openai.timeout", timeout)
}

currentModel := viper.GetString("openai.model")
color.Green("Code review your changes using " + currentModel + " model")
client, err := NewOpenAI()
// check provider
provider := core.Platform(viper.GetString("openai.provider"))
client, err := GetClient(provider)
if err != nil {
return err
}

currentModel := viper.GetString("openai.model")
color.Green("Code review your changes using " + currentModel + " model")

out, err := util.GetTemplateByString(
prompt.CodeReviewTemplate,
util.Data{
Expand Down

0 comments on commit 2022f9f

Please sign in to comment.