-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add merge list of chatchunk into chatmessage
- Loading branch information
Showing
4 changed files
with
198 additions
and
37 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
openai-client/src/commonMain/kotlin/com.aallam.openai.client/extension/ChatChuck.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.aallam.openai.client.extension | ||
|
||
import com.aallam.openai.api.ExperimentalOpenAI | ||
import com.aallam.openai.api.chat.ChatChunk | ||
import com.aallam.openai.api.chat.ChatMessage | ||
import com.aallam.openai.client.extension.internal.ChatMessageAssembler | ||
|
||
/** | ||
* Merges a list of [ChatChunk]s into a single consolidated [ChatMessage]. | ||
*/ | ||
@ExperimentalOpenAI | ||
public fun List<ChatChunk>.mergeToChatMessage(): ChatMessage { | ||
return fold(ChatMessageAssembler()) { assembler, chatChunk -> assembler.merge(chatChunk) }.build() | ||
} |
40 changes: 40 additions & 0 deletions
40
...src/commonMain/kotlin/com.aallam.openai.client/extension/internal/ChatMessageAssembler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.aallam.openai.client.extension.internal | ||
|
||
import com.aallam.openai.api.chat.* | ||
|
||
/** | ||
* A class to help assemble chat messages from chat chunks. | ||
*/ | ||
internal class ChatMessageAssembler { | ||
private val chatFuncName = StringBuilder() | ||
private val chatFuncArgs = StringBuilder() | ||
private val chatContent = StringBuilder() | ||
private var chatRole: ChatRole? = null | ||
|
||
/** | ||
* Merges a chat chunk into the chat message being assembled. | ||
*/ | ||
fun merge(chunk: ChatChunk): ChatMessageAssembler { | ||
chunk.delta.run { | ||
role?.let { chatRole = it } | ||
content?.let { chatContent.append(it) } | ||
functionCall?.let { call -> | ||
call.nameOrNull?.let { chatFuncName.append(it) } | ||
call.argumentsOrNull?.let { chatFuncArgs.append(it) } | ||
} | ||
} | ||
return this | ||
} | ||
|
||
/** | ||
* Builds and returns the assembled chat message. | ||
*/ | ||
fun build(): ChatMessage = chatMessage { | ||
this.role = chatRole | ||
this.content = chatContent.toString() | ||
if (chatFuncName.isNotEmpty() || chatFuncArgs.isNotEmpty()) { | ||
this.functionCall = FunctionCall(chatFuncName.toString(), chatFuncArgs.toString()) | ||
this.name = chatFuncName.toString() | ||
} | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestChatChunk.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package com.aallam.openai.client | ||
|
||
import com.aallam.openai.api.chat.ChatChunk | ||
import com.aallam.openai.api.chat.ChatDelta | ||
import com.aallam.openai.api.chat.ChatMessage | ||
import com.aallam.openai.api.chat.ChatRole | ||
import com.aallam.openai.api.core.FinishReason | ||
import com.aallam.openai.client.extension.mergeToChatMessage | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class TestChatChunk { | ||
|
||
@Test | ||
fun testMerge() { | ||
val chunks = listOf( | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = ChatRole(role = "assistant"), | ||
content = "" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = "The" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = " World" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = " Series" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = " in" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = " " | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = "202" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = "0" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = " is" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = " being held" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = " in" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = " Texas" | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = "." | ||
), | ||
finishReason = null | ||
), | ||
ChatChunk( | ||
index = 0, | ||
delta = ChatDelta( | ||
role = null, | ||
content = null | ||
), | ||
finishReason = FinishReason(value = "stop") | ||
) | ||
) | ||
val chatMessage = chunks.mergeToChatMessage() | ||
val message = ChatMessage( | ||
role = ChatRole.Assistant, | ||
content = "The World Series in 2020 is being held in Texas.", | ||
name = null, | ||
functionCall = null | ||
) | ||
assertEquals(chatMessage, message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters