Class WatsonxTokenCountEstimator

java.lang.Object
dev.langchain4j.model.watsonx.WatsonxTokenCountEstimator
All Implemented Interfaces:
dev.langchain4j.model.TokenCountEstimator

public class WatsonxTokenCountEstimator extends Object implements dev.langchain4j.model.TokenCountEstimator
A TokenCountEstimator implementation that integrates IBM watsonx.ai with LangChain4j.

Example usage:

TokenCountEstimator tokenCountEstimator = WatsonxTokenCountEstimator.builder()
    .url("https://...") // or use CloudRegion
    .apiKey("...")
    .projectId("...")
    .build();
  • Method Details

    • estimateTokenCountInText

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

      public int estimateTokenCountInText(String text, com.ibm.watsonx.ai.tokenization.TokenizationParameters parameters)
      Estimates the count of tokens in the given text using the specified TokenizationParameters.
      Parameters:
      text - the text.
      parameters - the tokenization parameters to use.
      Returns:
      the estimated count of tokens.
    • 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
    • builder

      public static WatsonxTokenCountEstimator.Builder builder()
      Returns a new WatsonxTokenCountEstimator.Builder instance.

      Example usage:

      TokenCountEstimator tokenCountEstimator = WatsonxTokenCountEstimator.builder()
          .url("https://...") // or use CloudRegion
          .apiKey("...")
          .projectId("...")
          .build();