Skip to content

Commit c75b97f

Browse files
committed
add isFinished methods for chunks
1 parent f99e074 commit c75b97f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/main/kotlin/com/cjcrafter/openai/chat/ChatChoiceChunk.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ data class ChatChoiceChunk(
3737
message.content += delta
3838
finishReason = if (json["finish_reason"].isJsonNull) null else FinishReason.valueOf(json["finish_reason"].asString.uppercase())
3939
}
40+
41+
/**
42+
* Returns `true` if this message chunk is complete. Once complete, no more
43+
* tokens will be generated, and [ChatChoiceChunk.message] will contain the
44+
* complete message.
45+
*/
46+
fun isFinished() = finishReason != null
4047
}
4148

4249
/*

src/main/kotlin/com/cjcrafter/openai/completions/CompletionChoiceChunk.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cjcrafter.openai.completions
22

33
import com.cjcrafter.openai.FinishReason
4+
import com.cjcrafter.openai.chat.ChatChoiceChunk
45
import com.google.gson.annotations.SerializedName
56

67
/**
@@ -25,4 +26,10 @@ data class CompletionChoiceChunk(
2526
val index: Int,
2627
val logprobs: List<Float>?,
2728
@field:SerializedName("finish_reason") val finishReason: FinishReason?
28-
)
29+
) {
30+
/**
31+
* Returns `true` if this message chunk is complete. Once complete, no more
32+
* tokens will be generated.
33+
*/
34+
fun isFinished() = finishReason != null
35+
}

0 commit comments

Comments
 (0)