@@ -5,7 +5,6 @@ package io.github.vyfor.groqkt.api.chat
5
5
import io.github.vyfor.groqkt.GroqModel
6
6
import io.github.vyfor.groqkt.api.chat.CompletionFunctionCallType.Auto
7
7
import io.github.vyfor.groqkt.api.chat.CompletionFunctionCallType.None
8
- import io.github.vyfor.groqkt.api.chat.CompletionResponseFormatType.JSON_OBJECT
9
8
import io.github.vyfor.groqkt.api.chat.UserMessageContent.Image
10
9
import io.github.vyfor.groqkt.api.chat.UserMessageContent.Image.ImageObject
11
10
import io.github.vyfor.groqkt.api.chat.UserMessageContent.Text
@@ -234,23 +233,19 @@ class ChatCompletionMessageBuilder {
234
233
var messages: MutableList <CompletionMessage > = mutableListOf ()
235
234
236
235
fun system (content : String , name : String? = null) {
237
- messages.add(CompletionMessage .System (content, name))
236
+ messages.add(CompletionMessage .system (content, name))
238
237
}
239
238
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 ))
242
241
}
243
242
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))
248
245
}
249
246
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))
254
249
}
255
250
256
251
fun assistant (
@@ -259,17 +254,17 @@ class ChatCompletionMessageBuilder {
259
254
functionCall : CompletionFunctionCall ? = null,
260
255
toolCalls : List <CompletionToolCall >? = null
261
256
) {
262
- messages.add(CompletionMessage .Assistant (content, functionCall, name , toolCalls))
257
+ messages.add(CompletionMessage .assistant (content, name, functionCall , toolCalls))
263
258
}
264
259
265
260
fun tool (content : String , toolCallId : String ) {
266
- messages.add(CompletionMessage .Tool (content, toolCallId))
261
+ messages.add(CompletionMessage .tool (content, toolCallId))
267
262
}
268
263
269
264
@Deprecated(" Deprecated in the Groq API" , ReplaceWith (" tool" ))
270
265
@Suppress(" DEPRECATION" )
271
266
fun function (content : String , name : String ) {
272
- messages.add(CompletionMessage .Function (content, name))
267
+ messages.add(CompletionMessage .function (content, name))
273
268
}
274
269
}
275
270
@@ -494,8 +489,8 @@ sealed class CompletionMessage(val role: String) {
494
489
is UserMessageType .Array -> {
495
490
val contents =
496
491
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) }
499
494
}
500
495
501
496
root.encodeSerializableElement(
@@ -530,20 +525,23 @@ sealed class CompletionMessage(val role: String) {
530
525
}
531
526
532
527
override fun deserialize (decoder : Decoder ): CompletionMessage {
533
- error(" Unreachable " )
528
+ error(" unreachable " )
534
529
}
535
530
}
536
531
537
532
companion object {
538
533
fun system (content : String , name : String? = null) = System (content, name)
539
534
540
- fun text (content : String ) = User (UserMessageType .Text (content))
535
+ fun text (content : String , name : String? = null ) = User (UserMessageType .Text (content), name )
541
536
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 )
544
539
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)
547
545
548
546
fun assistant (
549
547
content : String ,
@@ -578,11 +576,11 @@ sealed class UserMessageType {
578
576
*/
579
577
@Serializable
580
578
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 ,
583
581
) : UserMessageType() {
584
582
init {
585
- require(textContent.type != null || imageContent.type != null ) {
583
+ require(textContent?.text != null || imageContent?.image?.url != null ) {
586
584
" either textContent or imageContent must be specified"
587
585
}
588
586
}
0 commit comments