Skip to content

feat: add missing usage fields #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TestChatCompletions : TestOpenAI() {
function(
name = "currentWeather",
parameters =
"""
"""
{
"type": "object",
"properties": {
Expand Down Expand Up @@ -137,23 +137,33 @@ class TestChatCompletions : TestOpenAI() {

@Test
fun jsonSchema() = test {
val schemaJson = JsonObject(mapOf(
"type" to JsonPrimitive("object"),
"properties" to JsonObject(mapOf(
"question" to JsonObject(mapOf(
"type" to JsonPrimitive("string"),
"description" to JsonPrimitive("The question that was asked")
)),
"response" to JsonObject(mapOf(
"type" to JsonPrimitive("string"),
"description" to JsonPrimitive("The answer to the question")
))
)),
"required" to JsonArray(listOf(
JsonPrimitive("question"),
JsonPrimitive("response")
))
))
val schemaJson = JsonObject(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test serialization, add a new test function that uses a sample JSON response and verifies that serialization works correctly; here is an example.

mapOf(
"type" to JsonPrimitive("object"),
"properties" to JsonObject(
mapOf(
"question" to JsonObject(
mapOf(
"type" to JsonPrimitive("string"),
"description" to JsonPrimitive("The question that was asked")
)
),
"response" to JsonObject(
mapOf(
"type" to JsonPrimitive("string"),
"description" to JsonPrimitive("The answer to the question")
)
)
)
),
"required" to JsonArray(
listOf(
JsonPrimitive("question"),
JsonPrimitive("response")
)
)
)
)

val jsonSchema = JsonSchema(
name = "AnswerSchema",
Expand Down Expand Up @@ -301,4 +311,25 @@ class TestChatCompletions : TestOpenAI() {
assertNotNull(results.last().usage?.completionTokens)
assertNotNull(results.last().usage?.totalTokens)
}

@Test
fun usage_fields() = test {
val request = chatCompletionRequest {
model = ModelId("gpt-3.5-turbo")
messages {
message {
role = ChatRole.User
content = "What's the weather like in Boston?"
}
}
}
val response = openAI.chatCompletion(request)
assertNotNull(response.usage)
assertNotNull(response.usage?.promptTokens)
assertNotNull(response.usage?.completionTokens)
assertNotNull(response.usage?.totalTokens)
assertNotNull(response.usage?.promptTokensDetails)
assertNotNull(response.usage?.promptTokensDetails?.cachedTokens)
assertNotNull(response.usage?.completionTokensDetails)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,46 @@ public data class Usage(
* Count of total tokens.
*/
@SerialName("total_tokens") public val totalTokens: Int? = null,
/**
* Breakdown of tokens used in the prompt.
*/
@SerialName("prompt_tokens_details") public val promptTokensDetails: PromptTokensDetails? = null,
/**
* Breakdown of tokens used in a completion.
*/
@SerialName("completion_tokens_details") public val completionTokensDetails: CompletionTokensDetails? = null,
)

@Serializable
public data class PromptTokensDetails(
/**
* Cached tokens present in the prompt.
*/
public val cachedTokens: Int? = null,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add @SerialName for all fields? thanks!

/**
* Audio input tokens present in the prompt.
*/
public val audioTokens: Int? = null,
)

@Serializable
public data class CompletionTokensDetails(
/**
* Tokens generated by the model for reasoning.
*/
public val reasoningTokens: Int? = null,
/**
* Audio input tokens generated by the model.
*/
public val audioTokens: Int? = null,
/**
* When using Predicted Outputs, the number of tokens in the prediction that appeared in the completion.
*/
public val acceptedPredictionTokens: Int? = null,
/**
* When using Predicted Outputs, the number of tokens in the prediction that did not appear in the completion.
* However, like reasoning tokens, these tokens are still counted in the total completion tokens for
* purposes of billing, output, and context window limits.
*/
public val rejectedPredictionTokens: Int? = null,
)