Class OpenAiTokenCountEstimator

java.lang.Object
dev.langchain4j.model.openai.OpenAiTokenCountEstimator
All Implemented Interfaces:
dev.langchain4j.model.TokenCountEstimator

public class OpenAiTokenCountEstimator extends Object implements dev.langchain4j.model.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 Details

    • OpenAiTokenCountEstimator

      public OpenAiTokenCountEstimator(OpenAiChatModelName modelName)
      Creates an instance of the OpenAiTokenCountEstimator for a given OpenAiChatModelName.
    • OpenAiTokenCountEstimator

      public OpenAiTokenCountEstimator(OpenAiEmbeddingModelName modelName)
      Creates an instance of the OpenAiTokenCountEstimator for a given OpenAiEmbeddingModelName.
    • OpenAiTokenCountEstimator

      public OpenAiTokenCountEstimator(OpenAiLanguageModelName modelName)
      Creates an instance of the OpenAiTokenCountEstimator for a given OpenAiLanguageModelName.
    • OpenAiTokenCountEstimator

      public OpenAiTokenCountEstimator(String modelName)
      Creates an instance of the OpenAiTokenCountEstimator for a given model name.
  • Method Details

    • estimateTokenCountInText

      public int estimateTokenCountInText(String text)
      Specified by:
      estimateTokenCountInText in interface dev.langchain4j.model.TokenCountEstimator
    • estimateTokenCountInMessage

      public int estimateTokenCountInMessage(dev.langchain4j.data.message.ChatMessage message)
      Specified by:
      estimateTokenCountInMessage in interface dev.langchain4j.model.TokenCountEstimator
    • estimateTokenCountInMessages

      public int estimateTokenCountInMessages(Iterable<dev.langchain4j.data.message.ChatMessage> messages)
      Specified by:
      estimateTokenCountInMessages in interface dev.langchain4j.model.TokenCountEstimator
    • encode

      public List<Integer> encode(String text)
    • encode

      public List<Integer> encode(String text, int maxTokensToEncode)
    • decode

      public String decode(List<Integer> tokens)