Class DefaultUsage
java.lang.Object
org.springframework.ai.chat.metadata.DefaultUsage
- All Implemented Interfaces:
Usage
- Direct Known Subclasses:
GoogleGenAiUsage
Default implementation of the
Usage interface.- Since:
- 1.0.0
- Author:
- Mark Pollack, Ilayaperumal Gopinathan
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultUsage(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 nativeUsageobject. -
Method Summary
Modifier and TypeMethodDescriptionbooleanstatic DefaultUsageCreate 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.inthashCode()toString()
-
Constructor Details
-
DefaultUsage
public DefaultUsage(Integer promptTokens, Integer completionTokens, Integer totalTokens, Object nativeUsage) Create a new DefaultUsage with promptTokens, completionTokens, totalTokens and nativeUsageobject.- Parameters:
promptTokens- the number of tokens in the prompt, ornullif not availablecompletionTokens- the number of tokens in the generation, ornullif not availabletotalTokens- the total number of tokens, ornullto calculate from promptTokens and completionTokensnativeUsage- the native usage object returned by the model provider, ornullto 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, ornullif not availablecompletionTokens- the number of tokens in the generation, ornullif not available
-
DefaultUsage
Create a new DefaultUsage with promptTokens, completionTokens, and totalTokens.- Parameters:
promptTokens- the number of tokens in the prompt, ornullif not availablecompletionTokens- the number of tokens in the generation, ornullif not availabletotalTokens- the total number of tokens, ornullto calculate from promptTokens and completionTokens
-
-
Method Details
-
fromJson
public static DefaultUsage fromJson(Integer promptTokens, Integer completionTokens, 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)totalTokens- the total number of tokensnativeUsage- the native usage object- Returns:
- a new DefaultUsage instance
-
getPromptTokens
Description copied from interface:UsageReturns the number of tokens used in the prompt of the AI request.- Specified by:
getPromptTokensin interfaceUsage- Returns:
- an
Integerwith the number of tokens used in the prompt of the AI request. - See Also:
-
getCompletionTokens
Description copied from interface:UsageReturns the number of tokens returned in the generation (aka completion) of the AI's response.- Specified by:
getCompletionTokensin interfaceUsage- Returns:
- an
Integerwith the number of tokens returned in the generation (aka completion) of the AI's response. - See Also:
-
getTotalTokens
Description copied from interface:UsageReturn the total number of tokens from both the prompt of an AI request and generation of the AI's response.- Specified by:
getTotalTokensin 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:UsageReturn the usage data from the underlying model API response.- Specified by:
getNativeUsagein interfaceUsage- Returns:
- the object of type inferred by the API response.
-
equals
-
hashCode
public int hashCode() -
toString
-