diff --git a/go.mod b/go.mod index b5fdd32e57..7918fb1940 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3894951ee4..6a6691900b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/ai/anthropic.go b/pkg/ai/anthropic.go new file mode 100644 index 0000000000..8f55aa6f19 --- /dev/null +++ b/pkg/ai/anthropic.go @@ -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 +} + +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 +} + +func (c *ClaudeClient) GetName() string { + return anthropicClientName +}