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

Ability to count tokens before sending #200

Closed
AndrewMohawk opened this issue Mar 26, 2023 · 10 comments
Closed

Ability to count tokens before sending #200

AndrewMohawk opened this issue Mar 26, 2023 · 10 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request

Comments

@AndrewMohawk
Copy link

Reading the Open AI docs on Chat completion they specifically call out counting tokens on input (as you are billed on these as well as the amount of tokens on output). They use their TikToken python library, it would be awesome if there was something in this lib to do the same

@sashabaranov
Copy link
Owner

Related: #62

@sashabaranov
Copy link
Owner

sashabaranov commented Apr 6, 2023

I think this library should solve this, right? https://github.com/pkoukk/tiktoken-go

@allesan
Copy link

allesan commented Apr 11, 2023

Can you give example how one would use this library that you have linked?
Because ChatCompletionMessage.Content accepts string, not []uint

@sashabaranov
Copy link
Owner

@allesan tiktoken-go library might be used to count tokens, but not to pass them to ChatCompletionMessage

@allesan
Copy link

allesan commented Apr 12, 2023

Is there some library or a way to send tokens with go-openai?

@sashabaranov
Copy link
Owner

@allesan what do you mean? You are sending tokens, just packed in the string representation (i.e. just a prompt string).

@allesan
Copy link

allesan commented Apr 12, 2023

But if I understand correctly, with tiktoken lib you are sending tokens that are shortend and optimized for openai models?

@sashabaranov
Copy link
Owner

@allesan OpenAI API does not allow you to send tokenized inputs to it, see the API documentation: https://platform.openai.com/docs/api-reference/introduction

tiktoken lets you tokenize your inputs to count the number of tokens or use these tokens to train your own models.

@qhenkart
Copy link
Contributor

qhenkart commented Apr 13, 2023

@allesan tiktoken-go does resolve this problem for me. Now I can effectively count the tokens in the request by setting. Now you can properly adjust the MaxToken parameter

where count tokens is just a copy and paste of the example in tiktoken

func countTokens(prompt string) int {
	encoding := "cl100k_base"

	tke, err := tiktoken.GetEncoding(encoding)
	if err != nil {
		panic(err)
	}

	return len(tke.Encode(prompt, nil, nil))
}

@vvatanabe vvatanabe added enhancement New feature or request duplicate This issue or pull request already exists labels Jul 1, 2023
@vvatanabe
Copy link
Collaborator

If there's more to discuss, please post it in issue #62.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants