Class OpenAiTokenCountEstimator
java.lang.Object
dev.langchain4j.model.openai.OpenAiTokenCountEstimator
- All Implemented Interfaces:
TokenCountEstimator
This class can be used to estimate the cost (in tokens) before calling OpenAI.
Magic numbers present in this class were found empirically while testing.
There are integration tests in place that are making sure that the calculations here are very close to that of OpenAI.
-
Constructor Summary
ConstructorsConstructorDescriptionOpenAiTokenCountEstimator(OpenAiChatModelName modelName) Creates an instance of theOpenAiTokenCountEstimatorfor a givenOpenAiChatModelName.Creates an instance of theOpenAiTokenCountEstimatorfor a givenOpenAiEmbeddingModelName.OpenAiTokenCountEstimator(OpenAiLanguageModelName modelName) Creates an instance of theOpenAiTokenCountEstimatorfor a givenOpenAiLanguageModelName.OpenAiTokenCountEstimator(String modelName) Creates an instance of theOpenAiTokenCountEstimatorfor a given model name. -
Method Summary
Modifier and TypeMethodDescriptionintestimateTokenCountInMessage(ChatMessage message) Estimates the count of tokens in the given message.intestimateTokenCountInMessages(Iterable<ChatMessage> messages) Estimates the count of tokens in the given messages.intEstimates the count of tokens in the given text.
-
Constructor Details
-
OpenAiTokenCountEstimator
Creates an instance of theOpenAiTokenCountEstimatorfor a givenOpenAiChatModelName. -
OpenAiTokenCountEstimator
Creates an instance of theOpenAiTokenCountEstimatorfor a givenOpenAiEmbeddingModelName. -
OpenAiTokenCountEstimator
Creates an instance of theOpenAiTokenCountEstimatorfor a givenOpenAiLanguageModelName. -
OpenAiTokenCountEstimator
Creates an instance of theOpenAiTokenCountEstimatorfor a given model name.
-
-
Method Details
-
estimateTokenCountInText
Description copied from interface:TokenCountEstimatorEstimates the count of tokens in the given text.- Specified by:
estimateTokenCountInTextin interfaceTokenCountEstimator- Parameters:
text- the text.- Returns:
- the estimated count of tokens.
-
estimateTokenCountInMessage
Description copied from interface:TokenCountEstimatorEstimates the count of tokens in the given message.- Specified by:
estimateTokenCountInMessagein interfaceTokenCountEstimator- Parameters:
message- the message.- Returns:
- the estimated count of tokens.
-
estimateTokenCountInMessages
Description copied from interface:TokenCountEstimatorEstimates the count of tokens in the given messages.- Specified by:
estimateTokenCountInMessagesin interfaceTokenCountEstimator- Parameters:
messages- the messages.- Returns:
- the estimated count of tokens.
-
encode
-
encode
-
decode
-