forked from j178/chatgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show context token usage (j178#51)
- Loading branch information
Showing
4 changed files
with
72 additions
and
17 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
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
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
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 |
---|---|---|
@@ -1,7 +1,48 @@ | ||
package tokenizer | ||
|
||
import "github.com/sashabaranov/go-openai" | ||
import ( | ||
"github.com/pkoukk/tiktoken-go" | ||
"github.com/sashabaranov/go-openai" | ||
) | ||
|
||
func CountMessagesTokens(engine string, messages []openai.ChatCompletionMessage) int { | ||
return 127 | ||
var modelCache = map[string]*tiktoken.Tiktoken{} | ||
|
||
func CountTokens(model, text string) int { | ||
enc, ok := modelCache[model] | ||
if !ok { | ||
enc, _ = tiktoken.EncodingForModel(model) | ||
modelCache[model] = enc | ||
} | ||
return len(enc.Encode(text, nil, nil)) | ||
} | ||
|
||
// CountMessagesTokens based on https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb | ||
func CountMessagesTokens(model string, messages []openai.ChatCompletionMessage) int { | ||
var tokens int | ||
var tokensPerMessage int | ||
var tokensPerName int | ||
|
||
switch model { | ||
case openai.GPT3Dot5Turbo, openai.GPT3Dot5Turbo0301: | ||
tokensPerMessage = 4 // every message follows <|start|>{role/name}\n{content}<|end|>\n | ||
tokensPerName = -1 // if there's a name, the role is omitted | ||
case openai.GPT4, openai.GPT40314, openai.GPT432K, openai.GPT432K0314: | ||
tokensPerMessage = 3 | ||
tokensPerName = 1 | ||
} | ||
|
||
for k := range messages { | ||
tokens += tokensPerMessage | ||
|
||
tokens += CountTokens(model, messages[k].Role) | ||
tokens += CountTokens(model, messages[k].Content) | ||
tokens += CountTokens(model, messages[k].Name) | ||
if messages[k].Name != "" { | ||
tokens += tokensPerName | ||
} | ||
} | ||
|
||
tokens += 3 // every reply is primed with <|start|>assistant<|message|> | ||
|
||
return tokens | ||
} |