Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add claude backend #1297

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ require (
github.com/liamg/jfather v0.0.7 // indirect
github.com/liamg/memoryfs v1.6.0 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/liushuangls/go-anthropic/v2 v2.9.1 // indirect
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 // indirect
github.com/masahiro331/go-disk v0.0.0-20220919035250-c8da316f91ac // indirect
github.com/masahiro331/go-ext4-filesystem v0.0.0-20231208112839-4339555a0cd4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,8 @@ github.com/linode/linodego v1.35.0 h1:rIhUeCHBLEDlkoRnOTwzSGzljQ3ksXwLxacmXnrV+D
github.com/linode/linodego v1.35.0/go.mod h1:JxuhOEAMfSxun6RU5/MgTKH2GGTmFrhKRj3wL1NFin0=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
github.com/liushuangls/go-anthropic/v2 v2.9.1 h1:95Ff9/AVkgJii+cSS2bxqrBev3v1vOro0K8bl5m/2Lo=
github.com/liushuangls/go-anthropic/v2 v2.9.1/go.mod h1:8BKv/fkeTaL5R9R9bGkaknYBueyw2WxY20o7bImbOek=
github.com/lufia/plan9stats v0.0.0-20240226150601-1dcf7310316a h1:3Bm7EwfUQUvhNeKIkUct/gl9eod1TcXuj8stxvi/GoI=
github.com/lufia/plan9stats v0.0.0-20240226150601-1dcf7310316a/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k=
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 h1:EnfXoSqDfSNJv0VBNqY/88RNnhSGYkrHaO0mmFGbVsc=
Expand Down
57 changes: 57 additions & 0 deletions pkg/ai/anthropic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ai

import (
"context"
"errors"

"github.com/liushuangls/go-anthropic/v2"
"k8s.io/utils/ptr"
)

const anthropicClientName = "claude"

type ClaudeClient struct {
client *anthropic.Client
model string
temperature float32
topP float32
topK int32
maxTokens int
}

func (c *ClaudeClient) Configure(config IAIConfig) error {
token := config.GetPassword()

client := anthropic.NewClient(token)
if client == nil {
return errors.New("error creating OpenAI client")
}
c.client = client
c.model = config.GetModel()
c.temperature = config.GetTemperature()
c.topP = config.GetTopP()
c.maxTokens = 2048
return nil

Check warning on line 34 in pkg/ai/anthropic.go

View check run for this annotation

Codecov / codecov/patch

pkg/ai/anthropic.go#L22-L34

Added lines #L22 - L34 were not covered by tests
}

func (c *ClaudeClient) GetCompletion(ctx context.Context, prompt string) (string, error) {
// Create a completion request
resp, err := c.client.CreateMessages(ctx, anthropic.MessagesRequest{
Model: anthropic.ModelClaude3Dot5Sonnet20241022,
Messages: []anthropic.Message{
anthropic.NewUserTextMessage(prompt),
},
Temperature: ptr.To(c.temperature),
TopP: ptr.To(c.topP),
TopK: ptr.To[int](int(c.topK)),
MaxTokens: maxToken,
})
if err != nil {
return "", err
}
return resp.Content[0].GetText(), nil

Check warning on line 52 in pkg/ai/anthropic.go

View check run for this annotation

Codecov / codecov/patch

pkg/ai/anthropic.go#L37-L52

Added lines #L37 - L52 were not covered by tests
}

func (c *ClaudeClient) GetName() string {
return anthropicClientName

Check warning on line 56 in pkg/ai/anthropic.go

View check run for this annotation

Codecov / codecov/patch

pkg/ai/anthropic.go#L55-L56

Added lines #L55 - L56 were not covered by tests
}
Loading