@@ -3,21 +3,29 @@ package com.cjcrafter.openai.chat
3
3
import com.google.gson.JsonObject
4
4
5
5
/* *
6
- * Holds the data for 1 generated text completion. For most use cases, only 1
7
- * [ChatChoice] is generated.
6
+ * The OpenAI API returns a list of [ChatChoice]. Each chat choice has a
7
+ * generated message ([ChatChoice.message]) and a finish reason
8
+ * ([ChatChoice.finishReason]). For most use cases, you only need the generated
9
+ * message.
10
+ *
11
+ * By default, only 1 [ChatChoice] is generated (since [ChatRequest.n] == 1).
12
+ * When you increase `n`, more options are generated. The more options you
13
+ * generate, the more tokens you use. In general, it is best to **ONLY**
14
+ * generate 1 response, and to let the user regenerate the response.
8
15
*
9
16
* @param index The index in the array... 0 if [ChatRequest.n]=1.
10
17
* @param message The generated text.
11
18
* @param finishReason Why did the bot stop generating tokens?
19
+ * @see FinishReason
12
20
*/
13
- class ChatChoice (val index : Int , val message : ChatMessage , val finishReason : String ) {
21
+ class ChatChoice (val index : Int , val message : ChatMessage , val finishReason : FinishReason ? ) {
14
22
15
23
/* *
16
24
* JSON constructor for internal use.
17
25
*/
18
26
constructor (json: JsonObject ) : this (
19
27
json[" index" ].asInt,
20
28
ChatMessage (json[" message" ].asJsonObject),
21
- json[" finish_reason" ].toString( )
29
+ if ( json[" finish_reason" ].isJsonNull) null else FinishReason .valueOf(json[ " finish_reason " ].asString.uppercase() )
22
30
)
23
31
}
0 commit comments