-
Notifications
You must be signed in to change notification settings - Fork 693
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Anthropic Claude backend support
Signed-off-by: Surya Seetharaman <[email protected]>
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |