Package dev.langchain4j.model.openai
Class OpenAiTokenizer
java.lang.Object
dev.langchain4j.model.openai.OpenAiTokenizer
- All Implemented Interfaces:
dev.langchain4j.model.Tokenizer
This class can be used to estimate the cost (in tokens) before calling OpenAI or when using streaming.
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
ConstructorsConstructorDescriptionCreates an instance of theOpenAiTokenizer
for the "gpt-3.5-turbo" model.OpenAiTokenizer
(OpenAiChatModelName modelName) Creates an instance of theOpenAiTokenizer
for a givenOpenAiChatModelName
.OpenAiTokenizer
(OpenAiEmbeddingModelName modelName) Creates an instance of theOpenAiTokenizer
for a givenOpenAiEmbeddingModelName
.OpenAiTokenizer
(OpenAiLanguageModelName modelName) Creates an instance of theOpenAiTokenizer
for a givenOpenAiLanguageModelName
.OpenAiTokenizer
(String modelName) Creates an instance of theOpenAiTokenizer
for a given model name. -
Method Summary
Modifier and TypeMethodDescriptionint
estimateTokenCountInForcefulToolExecutionRequest
(dev.langchain4j.agent.tool.ToolExecutionRequest toolExecutionRequest) int
estimateTokenCountInForcefulToolSpecification
(dev.langchain4j.agent.tool.ToolSpecification toolSpecification) int
estimateTokenCountInMessage
(dev.langchain4j.data.message.ChatMessage message) int
estimateTokenCountInMessages
(Iterable<dev.langchain4j.data.message.ChatMessage> messages) int
int
estimateTokenCountInToolExecutionRequests
(Iterable<dev.langchain4j.agent.tool.ToolExecutionRequest> toolExecutionRequests) int
estimateTokenCountInToolSpecifications
(Iterable<dev.langchain4j.agent.tool.ToolSpecification> toolSpecifications) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface dev.langchain4j.model.Tokenizer
estimateTokenCountInTools, estimateTokenCountInTools
-
Constructor Details
-
OpenAiTokenizer
public OpenAiTokenizer()Creates an instance of theOpenAiTokenizer
for the "gpt-3.5-turbo" model. It should be suitable for all current OpenAI models, as they all use the same cl100k_base encoding. -
OpenAiTokenizer
Creates an instance of theOpenAiTokenizer
for a givenOpenAiChatModelName
. -
OpenAiTokenizer
Creates an instance of theOpenAiTokenizer
for a givenOpenAiEmbeddingModelName
. -
OpenAiTokenizer
Creates an instance of theOpenAiTokenizer
for a givenOpenAiLanguageModelName
. -
OpenAiTokenizer
Creates an instance of theOpenAiTokenizer
for a given model name.
-
-
Method Details
-
estimateTokenCountInText
- Specified by:
estimateTokenCountInText
in interfacedev.langchain4j.model.Tokenizer
-
estimateTokenCountInMessage
public int estimateTokenCountInMessage(dev.langchain4j.data.message.ChatMessage message) - Specified by:
estimateTokenCountInMessage
in interfacedev.langchain4j.model.Tokenizer
-
estimateTokenCountInMessages
public int estimateTokenCountInMessages(Iterable<dev.langchain4j.data.message.ChatMessage> messages) - Specified by:
estimateTokenCountInMessages
in interfacedev.langchain4j.model.Tokenizer
-
estimateTokenCountInToolSpecifications
public int estimateTokenCountInToolSpecifications(Iterable<dev.langchain4j.agent.tool.ToolSpecification> toolSpecifications) - Specified by:
estimateTokenCountInToolSpecifications
in interfacedev.langchain4j.model.Tokenizer
-
estimateTokenCountInForcefulToolSpecification
public int estimateTokenCountInForcefulToolSpecification(dev.langchain4j.agent.tool.ToolSpecification toolSpecification) - Specified by:
estimateTokenCountInForcefulToolSpecification
in interfacedev.langchain4j.model.Tokenizer
-
encode
-
encode
-
decode
-
estimateTokenCountInToolExecutionRequests
public int estimateTokenCountInToolExecutionRequests(Iterable<dev.langchain4j.agent.tool.ToolExecutionRequest> toolExecutionRequests) - Specified by:
estimateTokenCountInToolExecutionRequests
in interfacedev.langchain4j.model.Tokenizer
-
estimateTokenCountInForcefulToolExecutionRequest
public int estimateTokenCountInForcefulToolExecutionRequest(dev.langchain4j.agent.tool.ToolExecutionRequest toolExecutionRequest) - Specified by:
estimateTokenCountInForcefulToolExecutionRequest
in interfacedev.langchain4j.model.Tokenizer
-