Skip to content

Commit

Permalink
feat: support count tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
liushuangls committed Nov 6, 2024
1 parent c538633 commit aef226b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
10 changes: 5 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const (
type BetaVersion string

const (
BetaTools20240404 BetaVersion = "tools-2024-04-04"
BetaTools20240516 BetaVersion = "tools-2024-05-16"
BetaPromptCaching20240731 BetaVersion = "prompt-caching-2024-07-31"
BetaMessageBatches20240924 BetaVersion = "message-batches-2024-09-24"

BetaTools20240404 BetaVersion = "tools-2024-04-04"
BetaTools20240516 BetaVersion = "tools-2024-05-16"
BetaPromptCaching20240731 BetaVersion = "prompt-caching-2024-07-31"
BetaMessageBatches20240924 BetaVersion = "message-batches-2024-09-24"
BetaTokenCounting20241101 BetaVersion = "token-counting-2024-11-01"
BetaMaxTokens35Sonnet20240715 BetaVersion = "max-tokens-3-5-sonnet-2024-07-15"
)

Expand Down
31 changes: 31 additions & 0 deletions count_tokens.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package anthropic

import (
"context"
"net/http"
)

type CountTokensResponse struct {
httpHeader

InputTokens int `json:"input_tokens"`
}

func (c *Client) CountTokens(
ctx context.Context,
request MessagesRequest,
) (response CountTokensResponse, err error) {
var setters []requestSetter
if len(c.config.BetaVersion) > 0 {
setters = append(setters, withBetaVersion(c.config.BetaVersion...))
}

Check warning on line 21 in count_tokens.go

View check run for this annotation

Codecov / codecov/patch

count_tokens.go#L17-L21

Added lines #L17 - L21 were not covered by tests

urlSuffix := "/messages/count_tokens"
req, err := c.newRequest(ctx, http.MethodPost, urlSuffix, request, setters...)
if err != nil {
return
}

Check warning on line 27 in count_tokens.go

View check run for this annotation

Codecov / codecov/patch

count_tokens.go#L23-L27

Added lines #L23 - L27 were not covered by tests

err = c.sendRequest(req, &response)
return

Check warning on line 30 in count_tokens.go

View check run for this annotation

Codecov / codecov/patch

count_tokens.go#L29-L30

Added lines #L29 - L30 were not covered by tests
}
32 changes: 32 additions & 0 deletions integrationtest/count_tokens_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package integrationtest

import (
"context"
"testing"

"github.com/liushuangls/go-anthropic/v2"
)

func TestCountTokens(t *testing.T) {
testAPIKey(t)
client := anthropic.NewClient(APIKey, anthropic.WithBetaVersion(anthropic.BetaTokenCounting20241101))
ctx := context.Background()

request := anthropic.MessagesRequest{
Model: anthropic.ModelClaude3Dot5HaikuLatest,
MultiSystem: anthropic.NewMultiSystemMessages("you are an assistant", "you are snarky"),
Messages: []anthropic.Message{
anthropic.NewUserTextMessage("What is your name?"),
anthropic.NewAssistantTextMessage("My name is Claude."),
anthropic.NewUserTextMessage("What is your favorite color?"),
},
}

t.Run("CountTokens on real API", func(t *testing.T) {
resp, err := client.CountTokens(ctx, request)
if err != nil {
t.Fatalf("CountTokens error: %s", err)
}
t.Logf("CountTokens resp: %+v", resp)
})
}
2 changes: 1 addition & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
type MessagesRequest struct {
Model Model `json:"model"`
Messages []Message `json:"messages"`
MaxTokens int `json:"max_tokens"`
MaxTokens int `json:"max_tokens,omitempty"`

System string `json:"-"`
MultiSystem []MessageSystemPart `json:"-"`
Expand Down

0 comments on commit aef226b

Please sign in to comment.