Class WatsonxTokenCountEstimator

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

public class WatsonxTokenCountEstimator extends Object implements 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)
      Description copied from interface: TokenCountEstimator
      Estimates the count of tokens in the given text.
      Specified by:
      estimateTokenCountInText in interface TokenCountEstimator
      Parameters:
      text - the text.
      Returns:
      the estimated count of tokens.
    • 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(ChatMessage message)
      Description copied from interface: TokenCountEstimator
      Estimates the count of tokens in the given message.
      Specified by:
      estimateTokenCountInMessage in interface TokenCountEstimator
      Parameters:
      message - the message.
      Returns:
      the estimated count of tokens.
    • estimateTokenCountInMessages

      public int estimateTokenCountInMessages(Iterable<ChatMessage> messages)
      Description copied from interface: TokenCountEstimator
      Estimates the count of tokens in the given messages.
      Specified by:
      estimateTokenCountInMessages in interface TokenCountEstimator
      Parameters:
      messages - the messages.
      Returns:
      the estimated count of tokens.
    • 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();