Class DefaultUsage
java.lang.Object
org.springframework.ai.chat.metadata.DefaultUsage
- All Implemented Interfaces:
Usage
Default implementation of the
Usage
interface.- Since:
- 1.0.0
- Author:
- Mark Pollack, Ilayaperumal Gopinathan
-
Constructor Summary
ConstructorDescriptionDefaultUsage
(Integer promptTokens, Integer completionTokens) Create a new DefaultUsage with promptTokens and completionTokens.DefaultUsage
(Integer promptTokens, Integer completionTokens, Integer totalTokens) Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens.DefaultUsage
(Integer promptTokens, Integer completionTokens, Integer totalTokens, Object nativeUsage) Create a new DefaultUsage with promptTokens, completionTokens, totalTokens and nativeUsage
object. -
Method Summary
Modifier and TypeMethodDescriptionboolean
static DefaultUsage
fromJson
(Integer promptTokens, Integer completionTokens, Long generationTokens, Integer totalTokens, Object nativeUsage) Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens.Returns the number of tokens returned in the generation (aka completion) of the AI's response.Return the usage data from the underlying model API response.Returns the number of tokens used in the prompt of the AI request.Return the total number of tokens from both the prompt of an AI request and generation of the AI's response.int
hashCode()
toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.springframework.ai.chat.metadata.Usage
getGenerationTokens
-
Constructor Details
-
DefaultUsage
public DefaultUsage(Integer promptTokens, Integer completionTokens, Integer totalTokens, Object nativeUsage) Create a new DefaultUsage with promptTokens, completionTokens, totalTokens and nativeUsage
object.- Parameters:
promptTokens
- the number of tokens in the prompt, ornull
if not availablecompletionTokens
- the number of tokens in the generation, ornull
if not availabletotalTokens
- the total number of tokens, ornull
to calculate from promptTokens and completionTokensnativeUsage
- the native usage object returned by the model provider, ornull
to return the map of prompt, completion and total tokens.
-
DefaultUsage
Create a new DefaultUsage with promptTokens and completionTokens.- Parameters:
promptTokens
- the number of tokens in the prompt, ornull
if not availablecompletionTokens
- the number of tokens in the generation, ornull
if not available
-
DefaultUsage
Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens.- Parameters:
promptTokens
- the number of tokens in the prompt, ornull
if not availablecompletionTokens
- the number of tokens in the generation, ornull
if not availabletotalTokens
- the total number of tokens, ornull
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 promptcompletionTokens
- the number of tokens in the completion (new format)generationTokens
- the number of tokens in the generation (legacy format)totalTokens
- the total number of tokensnativeUsage
- the native usage object- Returns:
- a new DefaultUsage instance
-
getPromptTokens
Description copied from interface:Usage
Returns the number of tokens used in the prompt of the AI request.- Specified by:
getPromptTokens
in interfaceUsage
- Returns:
- an
Integer
with the number of tokens used in the prompt of the AI request. - See Also:
-
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 interfaceUsage
- Returns:
- an
Integer
with the number of tokens returned in the generation (aka completion) of the AI's response. - See Also:
-
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 interfaceUsage
- Returns:
- the total number of tokens from both the prompt of an AI request and generation of the AI's response.
- See Also:
-
getNativeUsage
Description copied from interface:Usage
Return the usage data from the underlying model API response.- Specified by:
getNativeUsage
in interfaceUsage
- Returns:
- the object of type inferred by the API response.
-
equals
-
hashCode
public int hashCode() -
toString
-