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 TypeMethodDescriptionintestimateTokenCountInForcefulToolExecutionRequest(ToolExecutionRequest toolExecutionRequest) intestimateTokenCountInForcefulToolSpecification(ToolSpecification toolSpecification) intestimateTokenCountInMessage(ChatMessage message) intestimateTokenCountInMessages(Iterable<ChatMessage> messages) intintestimateTokenCountInToolExecutionRequests(Iterable<ToolExecutionRequest> toolExecutionRequests) intestimateTokenCountInToolSpecifications(Iterable<ToolSpecification> toolSpecifications) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.langchain4j.model.Tokenizer
estimateTokenCountInTools, estimateTokenCountInTools
-
Constructor Details
-
OpenAiTokenizer
-
-
Method Details
-
estimateTokenCountInText
- Specified by:
estimateTokenCountInTextin interfaceTokenizer
-
estimateTokenCountInMessage
- Specified by:
estimateTokenCountInMessagein interfaceTokenizer
-
estimateTokenCountInMessages
- Specified by:
estimateTokenCountInMessagesin interfaceTokenizer
-
estimateTokenCountInToolSpecifications
- Specified by:
estimateTokenCountInToolSpecificationsin interfaceTokenizer
-
estimateTokenCountInForcefulToolSpecification
- Specified by:
estimateTokenCountInForcefulToolSpecificationin interfaceTokenizer
-
encode
-
encode
-
decode
-
estimateTokenCountInToolExecutionRequests
public int estimateTokenCountInToolExecutionRequests(Iterable<ToolExecutionRequest> toolExecutionRequests) - Specified by:
estimateTokenCountInToolExecutionRequestsin interfaceTokenizer
-
estimateTokenCountInForcefulToolExecutionRequest
public int estimateTokenCountInForcefulToolExecutionRequest(ToolExecutionRequest toolExecutionRequest) - Specified by:
estimateTokenCountInForcefulToolExecutionRequestin interfaceTokenizer
-