Class DefaultUsage

java.lang.Object
org.springframework.ai.chat.metadata.DefaultUsage
All Implemented Interfaces:
Usage

public class DefaultUsage extends Object implements Usage
Default implementation of the Usage interface.
Since:
1.0.0
Author:
Mark Pollack, Ilayaperumal Gopinathan
  • Constructor Details

    • DefaultUsage

      public DefaultUsage(Integer promptTokens, Integer completionTokens, Integer totalTokens, Object nativeUsage)
      Create a new DefaultUsage with promptTokens, completionTokens, totalTokens and native Usage object.
      Parameters:
      promptTokens - the number of tokens in the prompt, or null if not available
      completionTokens - the number of tokens in the generation, or null if not available
      totalTokens - the total number of tokens, or null to calculate from promptTokens and completionTokens
      nativeUsage - the native usage object returned by the model provider, or null to return the map of prompt, completion and total tokens.
    • DefaultUsage

      public DefaultUsage(Integer promptTokens, Integer completionTokens)
      Create a new DefaultUsage with promptTokens and completionTokens.
      Parameters:
      promptTokens - the number of tokens in the prompt, or null if not available
      completionTokens - the number of tokens in the generation, or null if not available
    • DefaultUsage

      public DefaultUsage(Integer promptTokens, Integer completionTokens, Integer totalTokens)
      Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens.
      Parameters:
      promptTokens - the number of tokens in the prompt, or null if not available
      completionTokens - the number of tokens in the generation, or null if not available
      totalTokens - the total number of tokens, or null to calculate from promptTokens and completionTokens
  • Method Details

    • fromJson

      public static DefaultUsage fromJson(Integer promptTokens, Integer completionTokens, Long generationTokens, Integer totalTokens, Object nativeUsage)
      Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens. This constructor is used for JSON deserialization and handles both the new format with completionTokens and the legacy format with generationTokens.
      Parameters:
      promptTokens - the number of tokens in the prompt
      completionTokens - the number of tokens in the completion (new format)
      generationTokens - the number of tokens in the generation (legacy format)
      totalTokens - the total number of tokens
      nativeUsage - the native usage object
      Returns:
      a new DefaultUsage instance
    • getPromptTokens

      public Integer getPromptTokens()
      Description copied from interface: Usage
      Returns the number of tokens used in the prompt of the AI request.
      Specified by:
      getPromptTokens in interface Usage
      Returns:
      an Integer with the number of tokens used in the prompt of the AI request.
      See Also:
    • getCompletionTokens

      public Integer getCompletionTokens()
      Description copied from interface: Usage
      Returns the number of tokens returned in the generation (aka completion) of the AI's response.
      Specified by:
      getCompletionTokens in interface Usage
      Returns:
      an Integer with the number of tokens returned in the generation (aka completion) of the AI's response.
      See Also:
    • getTotalTokens

      public Integer getTotalTokens()
      Description copied from interface: Usage
      Return the total number of tokens from both the prompt of an AI request and generation of the AI's response.
      Specified by:
      getTotalTokens in interface Usage
      Returns:
      the total number of tokens from both the prompt of an AI request and generation of the AI's response.
      See Also:
    • getNativeUsage

      public Object getNativeUsage()
      Description copied from interface: Usage
      Return the usage data from the underlying model API response.
      Specified by:
      getNativeUsage in interface Usage
      Returns:
      the object of type inferred by the API response.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object