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
Changes from 1 commit
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
Next Next commit
Add Anthropic Claude backend support
Signed-off-by: Surya Seetharaman <[email protected]>
  • Loading branch information
tssurya committed Nov 3, 2024
commit 906b4b20c20d58f0543a7a4b6bc7b64c5065dfaa
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
}

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
}