Skip to content

Commit f914c1d

Browse files
committed
refactor: make ChatCompletionMessageBuilder's DSL functions depend on the static members of the companion object
1 parent 474508c commit f914c1d

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/commonMain/kotlin/io/github/vyfor/groqkt/api/chat/ChatCompletionRequest.kt

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package io.github.vyfor.groqkt.api.chat
55
import io.github.vyfor.groqkt.GroqModel
66
import io.github.vyfor.groqkt.api.chat.CompletionFunctionCallType.Auto
77
import io.github.vyfor.groqkt.api.chat.CompletionFunctionCallType.None
8-
import io.github.vyfor.groqkt.api.chat.CompletionResponseFormatType.JSON_OBJECT
98
import io.github.vyfor.groqkt.api.chat.UserMessageContent.Image
109
import io.github.vyfor.groqkt.api.chat.UserMessageContent.Image.ImageObject
1110
import io.github.vyfor.groqkt.api.chat.UserMessageContent.Text
@@ -234,23 +233,19 @@ class ChatCompletionMessageBuilder {
234233
var messages: MutableList<CompletionMessage> = mutableListOf()
235234

236235
fun system(content: String, name: String? = null) {
237-
messages.add(CompletionMessage.System(content, name))
236+
messages.add(CompletionMessage.system(content, name))
238237
}
239238

240-
fun text(content: String) {
241-
messages.add(CompletionMessage.User(UserMessageType.Text(content)))
239+
fun text(content: String, name: String? = null) {
240+
messages.add(CompletionMessage.text(content, name))
242241
}
243242

244-
fun image(image: String) {
245-
messages.add(
246-
CompletionMessage.User(
247-
UserMessageType.Array(imageContent = Image(ImageObject(url = image)))))
243+
fun image(image: String, name: String? = null) {
244+
messages.add(CompletionMessage.image(image, name))
248245
}
249246

250-
fun user(content: String?, image: String?, name: String? = null) {
251-
messages.add(
252-
CompletionMessage.User(
253-
UserMessageType.Array(Text(content), Image(ImageObject(url = image))), name))
247+
fun user(content: String?, image: String? = null, name: String? = null) {
248+
messages.add(CompletionMessage.user(content, image, name))
254249
}
255250

256251
fun assistant(
@@ -259,17 +254,17 @@ class ChatCompletionMessageBuilder {
259254
functionCall: CompletionFunctionCall? = null,
260255
toolCalls: List<CompletionToolCall>? = null
261256
) {
262-
messages.add(CompletionMessage.Assistant(content, functionCall, name, toolCalls))
257+
messages.add(CompletionMessage.assistant(content, name, functionCall, toolCalls))
263258
}
264259

265260
fun tool(content: String, toolCallId: String) {
266-
messages.add(CompletionMessage.Tool(content, toolCallId))
261+
messages.add(CompletionMessage.tool(content, toolCallId))
267262
}
268263

269264
@Deprecated("Deprecated in the Groq API", ReplaceWith("tool"))
270265
@Suppress("DEPRECATION")
271266
fun function(content: String, name: String) {
272-
messages.add(CompletionMessage.Function(content, name))
267+
messages.add(CompletionMessage.function(content, name))
273268
}
274269
}
275270

@@ -494,8 +489,8 @@ sealed class CompletionMessage(val role: String) {
494489
is UserMessageType.Array -> {
495490
val contents =
496491
mutableListOf<UserMessageContent>().apply {
497-
value.content.textContent.text?.let { add(value.content.textContent) }
498-
value.content.imageContent.image?.let { add(value.content.imageContent) }
492+
value.content.textContent?.text?.let { add(value.content.textContent) }
493+
value.content.imageContent?.image?.let { add(value.content.imageContent) }
499494
}
500495

501496
root.encodeSerializableElement(
@@ -530,20 +525,23 @@ sealed class CompletionMessage(val role: String) {
530525
}
531526

532527
override fun deserialize(decoder: Decoder): CompletionMessage {
533-
error("Unreachable")
528+
error("unreachable")
534529
}
535530
}
536531

537532
companion object {
538533
fun system(content: String, name: String? = null) = System(content, name)
539534

540-
fun text(content: String) = User(UserMessageType.Text(content))
535+
fun text(content: String, name: String? = null) = User(UserMessageType.Text(content), name)
541536

542-
fun image(image: String) =
543-
User(UserMessageType.Array(imageContent = Image(ImageObject(url = image))))
537+
fun image(image: String, name: String? = null) =
538+
User(UserMessageType.Array(imageContent = Image(ImageObject(url = image))), name)
544539

545-
fun user(content: String?, image: String?, name: String? = null) =
546-
User(UserMessageType.Array(Text(content), Image(ImageObject(url = image))), name)
540+
fun user(content: String?, image: String? = null, name: String? = null) =
541+
User(
542+
UserMessageType.Array(
543+
content?.let { Text(it) }, image?.let { Image(ImageObject(url = it)) }),
544+
name)
547545

548546
fun assistant(
549547
content: String,
@@ -578,11 +576,11 @@ sealed class UserMessageType {
578576
*/
579577
@Serializable
580578
data class Array(
581-
val textContent: UserMessageContent.Text = Text(),
582-
val imageContent: Image = Image()
579+
val textContent: UserMessageContent.Text? = null,
580+
val imageContent: Image? = null,
583581
) : UserMessageType() {
584582
init {
585-
require(textContent.type != null || imageContent.type != null) {
583+
require(textContent?.text != null || imageContent?.image?.url != null) {
586584
"either textContent or imageContent must be specified"
587585
}
588586
}

0 commit comments

Comments
 (0)