Skip to content

Commit 2c2f63d

Browse files
committed
remove JSON constructors
1 parent cf97a9a commit 2c2f63d

File tree

7 files changed

+23
-67
lines changed

7 files changed

+23
-67
lines changed

src/main/kotlin/com/cjcrafter/openai/OpenAI.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class OpenAI @JvmOverloads constructor(
6767
*/
6868
@Throws(OpenAIError::class)
6969
fun createChatCompletion(request: ChatRequest): ChatResponse {
70+
@Suppress("DEPRECATION")
7071
request.stream = false // use streamResponse for stream=true
7172
val httpRequest = buildRequest(request)
7273

@@ -80,7 +81,9 @@ class OpenAI @JvmOverloads constructor(
8081
rootObject = JsonParser.parseString(response.body!!.string()).asJsonObject
8182
if (rootObject!!.has("error"))
8283
throw OpenAIError.fromJson(rootObject!!.get("error").asJsonObject)
83-
return ChatResponse(rootObject!!)
84+
85+
return gson.fromJson(rootObject, ChatResponse::class.java)
86+
//return ChatResponse(rootObject!!)
8487
}
8588
} catch (ex: IOException) {
8689
throw WrappedIOError(ex)

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.cjcrafter.openai.chat
22

33
import com.cjcrafter.openai.FinishReason
44
import com.google.gson.JsonObject
5+
import com.google.gson.annotations.SerializedName
56

67
/**
78
* The OpenAI API returns a list of [ChatChoice]. Each chat choice has a
@@ -20,14 +21,8 @@ import com.google.gson.JsonObject
2021
* @constructor Create a new chat choice, for internal usage.
2122
* @see FinishReason
2223
*/
23-
data class ChatChoice(val index: Int, val message: ChatMessage, val finishReason: FinishReason) {
24-
25-
/**
26-
* JSON constructor for internal usage.
27-
*/
28-
constructor(json: JsonObject) : this(
29-
json["index"].asInt,
30-
ChatMessage(json["message"].asJsonObject),
31-
FinishReason.valueOf(json["finish_reason"].asString.uppercase())
32-
)
33-
}
24+
data class ChatChoice(
25+
val index: Int,
26+
val message: ChatMessage,
27+
@field:SerializedName("finish_reason") val finishReason: FinishReason
28+
)

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.cjcrafter.openai.chat
22

33
import com.cjcrafter.openai.FinishReason
44
import com.google.gson.JsonObject
5+
import com.google.gson.annotations.SerializedName
56

67
/**
78
*
@@ -23,21 +24,12 @@ import com.google.gson.JsonObject
2324
* @see ChatChoice
2425
* @since 1.2.0
2526
*/
26-
data class ChatChoiceChunk(val index: Int, val message: ChatMessage, var delta: String, var finishReason: FinishReason?) {
27-
28-
/**
29-
* JSON constructor for internal usage.
30-
*/
31-
constructor(json: JsonObject) : this(
32-
33-
// The first message from ChatGPT looks like this:
34-
// data: {"id":"chatcmpl-6xUB4Vi8jEG8u4hMBTMeO8KXgA87z","object":"chat.completion.chunk","created":1679635374,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}
35-
// So the only data we have so far is that ChatGPT will be responding.
36-
json["index"].asInt,
37-
ChatMessage(ChatUser.ASSISTANT, ""),
38-
"",
39-
null
40-
)
27+
data class ChatChoiceChunk(
28+
val index: Int,
29+
val message: ChatMessage,
30+
var delta: String,
31+
@field:SerializedName("finish_reason") var finishReason: FinishReason?
32+
) {
4133

4234
internal fun update(json: JsonObject) {
4335
val deltaJson = json["delta"].asJsonObject

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
package com.cjcrafter.openai.chat
22

3-
import com.google.gson.JsonObject
4-
53
/**
64
* ChatGPT's biggest innovation is its conversation memory. To remember the
75
* conversation, we need to map each message to who sent it. This data class
86
* wraps a message with the user who sent the message.
97
*
10-
* Note that
11-
*
128
* @property role The user who sent this message.
139
* @property content The string content of the message.
1410
* @see ChatUser
1511
*/
1612
data class ChatMessage(var role: ChatUser, var content: String) {
1713

18-
/**
19-
* JSON constructor for internal usage.
20-
*/
21-
constructor(json: JsonObject) : this(ChatUser.valueOf(json["role"].asString.uppercase()), json["content"].asString)
22-
2314
companion object {
2415

2516
/**

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ data class ChatResponse(
2424
val usage: ChatUsage
2525
) {
2626

27-
/**
28-
* JSON constructor for internal usage.
29-
*/
30-
constructor(json: JsonObject) : this(
31-
json["id"].asString,
32-
json["created"].asLong,
33-
json["choices"].asJsonArray.map { ChatChoice(it.asJsonObject) },
34-
ChatUsage(json["usage"].asJsonObject)
35-
)
36-
3727
/**
3828
* Returns the [Instant] time that the OpenAI Chat API sent this response.
3929
* The time is measured as a unix timestamp (measured in seconds since

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ data class ChatResponseChunk(
2727
val choices: List<ChatChoiceChunk>,
2828
) {
2929

30-
/**
31-
* JSON constructor for internal usage.
32-
*/
33-
constructor(json: JsonObject) : this(
34-
json["id"].asString,
35-
json["created"].asLong,
36-
json["choices"].asJsonArray.map { ChatChoiceChunk(it.asJsonObject) },
37-
)
38-
3930
internal fun update(json: JsonObject) {
4031
json["choices"].asJsonArray.forEachIndexed { index, jsonElement ->
4132
choices[index].update(jsonElement.asJsonObject)
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.cjcrafter.openai.chat
22

3-
import com.google.gson.JsonObject
3+
import com.google.gson.annotations.SerializedName
44

55
/**
66
* Holds how many tokens that were used by your API request. Use these
@@ -15,14 +15,8 @@ import com.google.gson.JsonObject
1515
* @param totalTokens How many tokens in total.
1616
* @see <a href="https://platform.openai.com/docs/guides/chat/managing-tokens">Managing Tokens Guide</a>
1717
*/
18-
data class ChatUsage(val promptTokens: Int, val completionTokens: Int, val totalTokens: Int) {
19-
20-
/**
21-
* JSON constructor for internal usage.
22-
*/
23-
constructor(json: JsonObject) : this(
24-
json["prompt_tokens"].asInt,
25-
json["completion_tokens"].asInt,
26-
json["total_tokens"].asInt
27-
)
28-
}
18+
data class ChatUsage(
19+
@field:SerializedName("prompt_tokens") val promptTokens: Int,
20+
@field:SerializedName("completion_tokens") val completionTokens: Int,
21+
@field:SerializedName("total_tokens") val totalTokens: Int
22+
)

0 commit comments

Comments
 (0)