Package dev.langchain4j.model.openai
Class OpenAiTokenizer
java.lang.Object
dev.langchain4j.model.openai.OpenAiTokenizer
- All Implemented Interfaces:
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
estimateTokenCountInForcefulToolExecutionRequest
(ToolExecutionRequest toolExecutionRequest) int
estimateTokenCountInForcefulToolSpecification
(ToolSpecification toolSpecification) int
estimateTokenCountInMessage
(ChatMessage message) int
estimateTokenCountInMessages
(Iterable<ChatMessage> messages) int
int
estimateTokenCountInToolExecutionRequests
(Iterable<ToolExecutionRequest> toolExecutionRequests) int
estimateTokenCountInToolSpecifications
(Iterable<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
-
-
Method Details
-
estimateTokenCountInText
- Specified by:
estimateTokenCountInText
in interfaceTokenizer
-
estimateTokenCountInMessage
- Specified by:
estimateTokenCountInMessage
in interfaceTokenizer
-
estimateTokenCountInMessages
- Specified by:
estimateTokenCountInMessages
in interfaceTokenizer
-
estimateTokenCountInToolSpecifications
- Specified by:
estimateTokenCountInToolSpecifications
in interfaceTokenizer
-
estimateTokenCountInForcefulToolSpecification
- Specified by:
estimateTokenCountInForcefulToolSpecification
in interfaceTokenizer
-
encode
-
encode
-
decode
-
estimateTokenCountInToolExecutionRequests
public int estimateTokenCountInToolExecutionRequests(Iterable<ToolExecutionRequest> toolExecutionRequests) - Specified by:
estimateTokenCountInToolExecutionRequests
in interfaceTokenizer
-
estimateTokenCountInForcefulToolExecutionRequest
public int estimateTokenCountInForcefulToolExecutionRequest(ToolExecutionRequest toolExecutionRequest) - Specified by:
estimateTokenCountInForcefulToolExecutionRequest
in interfaceTokenizer
-