Skip to content

Commit 193e00b

Browse files
authored
feat: add support for Anthropic provider integration (#210)
- Rename `cmd/openai.go` to `cmd/provider.go` - Add support for the Anthropic provider - Add `NewAnthropic` function to create a new instance of `anthropic.Client` - Update `GetClient` function to include the Anthropic platform - Define `Anthropic` as a new platform constant - Update `IsValid` function to include the Anthropic platform - Change `WithModel` function parameter type from `anthropic.Model` to `string` Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent a097f58 commit 193e00b

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

cmd/openai.go renamed to cmd/provider.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66

77
"github.com/appleboy/CodeGPT/core"
8+
"github.com/appleboy/CodeGPT/provider/anthropic"
89
"github.com/appleboy/CodeGPT/provider/gemini"
910
"github.com/appleboy/CodeGPT/provider/openai"
1011

@@ -44,13 +45,35 @@ func NewGemini(ctx context.Context) (*gemini.Client, error) {
4445
)
4546
}
4647

48+
// NewAnthropic creates a new instance of the anthropic.Client using configuration
49+
// values retrieved from Viper. The configuration values include the API key,
50+
// model, maximum tokens, temperature, and top_p.
51+
//
52+
// Parameters:
53+
// - ctx: The context for the client.
54+
//
55+
// Returns:
56+
// - A pointer to an anthropic.Client instance.
57+
// - An error if the client could not be created.
58+
func NewAnthropic(ctx context.Context) (*anthropic.Client, error) {
59+
return anthropic.New(
60+
anthropic.WithAPIKey(viper.GetString("openai.api_key")),
61+
anthropic.WithModel(viper.GetString("openai.model")),
62+
anthropic.WithMaxTokens(viper.GetInt("openai.max_tokens")),
63+
anthropic.WithTemperature(float32(viper.GetFloat64("openai.temperature"))),
64+
anthropic.WithTopP(float32(viper.GetFloat64("openai.top_p"))),
65+
)
66+
}
67+
4768
// GetClient returns the generative client based on the platform
4869
func GetClient(ctx context.Context, p core.Platform) (core.Generative, error) {
4970
switch p {
5071
case core.Gemini:
5172
return NewGemini(ctx)
5273
case core.OpenAI, core.Azure:
5374
return NewOpenAI()
75+
case core.Anthropic:
76+
return NewAnthropic(ctx)
5477
}
5578
return nil, errors.New("invalid provider")
5679
}

core/platform.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package core
33
type Platform string
44

55
const (
6-
OpenAI Platform = "openai"
7-
Azure Platform = "azure"
8-
Gemini Platform = "gemini"
6+
OpenAI Platform = "openai"
7+
Azure Platform = "azure"
8+
Gemini Platform = "gemini"
9+
Anthropic Platform = "anthropic"
910
)
1011

1112
// String returns the string representation of the Platform.
@@ -16,7 +17,7 @@ func (p Platform) String() string {
1617
// IsValid returns true if the Platform is valid.
1718
func (p Platform) IsValid() bool {
1819
switch p {
19-
case OpenAI, Azure, Gemini:
20+
case OpenAI, Azure, Gemini, Anthropic:
2021
return true
2122
}
2223
return false

provider/anthropic/options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func WithAPIKey(val string) Option {
4343
}
4444

4545
// WithModel is a function that returns an Option, which sets the model field of the config struct.
46-
func WithModel(val anthropic.Model) Option {
46+
func WithModel(val string) Option {
4747
return optionFunc(func(c *config) {
48-
c.model = val
48+
c.model = anthropic.Model(val)
4949
})
5050
}
5151

0 commit comments

Comments
 (0)