-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi_tokenizers_estimate_token_count.go
52 lines (44 loc) · 1.45 KB
/
api_tokenizers_estimate_token_count.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package moonshot
import (
"context"
)
type ITokenizers interface {
EstimateTokenCount(ctx context.Context, req *TokenizersEstimateTokenCountRequest) (resp *TokenizersEstimateTokenCountResponse, err error)
}
type tokenizersEstimateTokenCount struct {
client *Client
}
// Tokenizers returns a new tokenizers controller
func (c *Client) Tokenizers() ITokenizers {
return &tokenizersEstimateTokenCount{
client: c,
}
}
type TokenizersEstimateTokenCountRequest struct {
Model ChatCompletionsModelID `json:"model"`
Messages []*ChatCompletionsMessage `json:"messages"`
}
type TokenizersEstimateTokenCountResponse struct {
CommonResponse
Data *TokenizersEstimateTokenCountResponseData `json:"data"`
}
type TokenizersEstimateTokenCountResponseData struct {
TotalTokens int `json:"total_tokens"`
}
// EstimateTokenCount estimates the number of tokens that would be generated by the model
func (t *tokenizersEstimateTokenCount) EstimateTokenCount(ctx context.Context, req *TokenizersEstimateTokenCountRequest) (*TokenizersEstimateTokenCountResponse, error) {
const path = "/v1/tokenizers/estimate-token-count"
estimateTokenCountResp := new(TokenizersEstimateTokenCountResponse)
resp, err := t.client.HTTPClient().SetPath(path).SetBody(req).Post(ctx)
if err != nil {
return nil, err
}
if !resp.StatusOK() {
return nil, ResponseToError(resp)
}
err = resp.Unmarshal(estimateTokenCountResp)
if err != nil {
return nil, err
}
return estimateTokenCountResp, nil
}