1
1
package com.cjcrafter.openai.chat
2
2
3
3
import com.google.gson.JsonObject
4
+ import java.time.Instant
5
+ import java.time.ZoneId
6
+ import java.time.ZonedDateTime
7
+ import java.util.*
4
8
5
9
/* *
6
10
* The [ChatResponse] contains all the data returned by the OpenAI Chat API.
7
11
* For most use cases, [ChatResponse.get] (passing 0 to the index argument) is
8
12
* all you need.
9
13
*
10
- * @property id
11
- * @property object
12
- * @property created
13
- * @property choices
14
- * @property usage
15
- * @constructor Create empty Chat response
14
+ * @property id The unique id for your request.
15
+ * @property created The Unix timestamp (measured in seconds since 00:00:00 UTC on January 1, 1970) when the API response was created.
16
+ * @property choices The list of generated messages.
17
+ * @property usage The number of tokens used in this request/response.
18
+ * @constructor Create Chat response (for internal usage).
19
+ * @see ChatChoice
20
+ * @see ChatUsage
16
21
*/
17
- class ChatResponse (
22
+ data class ChatResponse (
18
23
val id : String ,
19
- val `object `: String ,
20
24
val created : Long ,
21
25
val choices : List <ChatChoice >,
22
26
val usage : ChatUsage
@@ -27,12 +31,39 @@ class ChatResponse(
27
31
*/
28
32
constructor (json: JsonObject ) : this (
29
33
json[" id" ].asString,
30
- json[" object" ].asString,
31
34
json[" created" ].asLong,
32
35
json[" choices" ].asJsonArray.map { ChatChoice (it.asJsonObject) },
33
36
ChatUsage (json[" usage" ].asJsonObject)
34
37
)
35
38
39
+ /* *
40
+ * Returns the [Instant] time that the OpenAI Chat API sent this response.
41
+ * The time is measured as a unix timestamp (measured in seconds since
42
+ * 00:00:00 UTC on January 1, 1970).
43
+ *
44
+ * Note that users expect time to be measured in their timezone, so
45
+ * [getZonedTime] is preferred.
46
+ *
47
+ * @return The instant the api created this response.
48
+ * @see getZonedTime
49
+ */
50
+ fun getTime (): Instant {
51
+ return Instant .ofEpochSecond(created)
52
+ }
53
+
54
+ /* *
55
+ * Returns the time-zoned instant that the OpenAI Chat API sent this
56
+ * response. By default, this method uses the system's timezone.
57
+ *
58
+ * @param timezone The user's timezone.
59
+ * @return The timezone adjusted date time.
60
+ * @see TimeZone.getDefault
61
+ */
62
+ @JvmOverloads
63
+ fun getZonedTime (timezone : ZoneId = TimeZone .getDefault().toZoneId()): ZonedDateTime {
64
+ return ZonedDateTime .ofInstant(getTime(), timezone)
65
+ }
66
+
36
67
/* *
37
68
* Shorthand for accessing the generated messages (shorthand for
38
69
* [ChatResponse.choices]).
0 commit comments