Skip to content
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

fix: function mode serializer #203

Merged
Merged
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 @@ -15,6 +15,7 @@ import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonEncoder
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonPrimitive
import kotlin.jvm.JvmInline

/**
Expand Down Expand Up @@ -62,7 +63,7 @@ internal object FunctionModeSerializer : KSerializer<FunctionMode> {
require(decoder is JsonDecoder) { "This decoder is not a JsonDecoder. Cannot deserialize `FunctionCall`" }
return when (val json = decoder.decodeJsonElement()) {
is JsonPrimitive -> Default(json.content)
is JsonObject -> Named.serializer().deserialize(decoder)
is JsonObject -> json["name"]?.jsonPrimitive?.content?.let(FunctionMode::Named) ?: error("Missing 'name'")
else -> throw UnsupportedOperationException("Cannot deserialize FunctionMode. Unsupported JSON element.")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.aallam.openai.api.chat

import com.aallam.openai.api.BetaOpenAI
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals

@OptIn(BetaOpenAI::class)
class TestFunctionMode {

@Test
fun serialize() {
listOf(
FunctionMode.Auto,
FunctionMode.None,
FunctionMode.Named("someFunctionName")
).forEach { functionMode ->
val jsonString = Json.encodeToString(FunctionMode.serializer(), functionMode)
val decoded = Json.decodeFromString(FunctionMode.serializer(), jsonString)
assertEquals(functionMode, decoded)
}
}
}