-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assistant): add streaming (#400)
* Added feature assistant-streaming * Fixed THREAD_MESSAGE_CREATED data type --------- Co-authored-by: Nicholas Dalton <[email protected]>
- Loading branch information
Showing
12 changed files
with
593 additions
and
2 deletions.
There are no files selected for viewing
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
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
30 changes: 30 additions & 0 deletions
30
...i-client/src/commonMain/kotlin/com.aallam.openai.client/extension/AssistantStreamEvent.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,30 @@ | ||
package com.aallam.openai.client.extension | ||
|
||
import com.aallam.openai.api.run.AssistantStreamEvent | ||
import com.aallam.openai.client.internal.JsonLenient | ||
import kotlinx.serialization.KSerializer | ||
|
||
/** | ||
* Get the data of the [AssistantStreamEvent] using the provided [serializer] from the corresponding event type. | ||
* @param <T> the type of the data. | ||
* @throws IllegalStateException if the [AssistantStreamEvent] data is null. | ||
* @throws ClassCastException if the [AssistantStreamEvent] data cannot be cast to the provided type. | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
public fun <T> AssistantStreamEvent.getData(): T { | ||
return type | ||
.let { it.serializer as? KSerializer<T> } | ||
?.let(::getData) | ||
?: throw IllegalStateException("Failed to decode ServerSentEvent: $rawType") | ||
} | ||
|
||
|
||
/** | ||
* Get the data of the [AssistantStreamEvent] using the provided [serializer]. | ||
* @throws IllegalStateException if the [AssistantStreamEvent] data is null. | ||
* @throws ClassCastException if the [AssistantStreamEvent] data cannot be cast to the provided type. | ||
*/ | ||
public fun <T> AssistantStreamEvent.getData(serializer: KSerializer<T>): T = | ||
data | ||
?.let { JsonLenient.decodeFromString(serializer, it) } | ||
?: throw IllegalStateException("ServerSentEvent data was null: $rawType") |
19 changes: 19 additions & 0 deletions
19
openai-client/src/commonMain/kotlin/com.aallam.openai.client/extension/ServerSentEvent.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,19 @@ | ||
package com.aallam.openai.client.extension | ||
|
||
import com.aallam.openai.api.run.AssistantStreamEvent | ||
import com.aallam.openai.api.run.AssistantStreamEventType | ||
import com.aallam.openai.client.internal.JsonLenient | ||
import io.ktor.sse.ServerSentEvent | ||
import kotlinx.serialization.KSerializer | ||
|
||
/** | ||
* Convert a [ServerSentEvent] to [AssistantStreamEvent]. Type will be [AssistantStreamEventType.UNKNOWN] if the event is null or unrecognized. | ||
*/ | ||
internal fun ServerSentEvent.toAssistantStreamEvent() : AssistantStreamEvent = | ||
AssistantStreamEvent( | ||
event, | ||
event | ||
?.let(AssistantStreamEventType::fromEvent) | ||
?:AssistantStreamEventType.UNKNOWN, | ||
data | ||
) |
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
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
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
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
Oops, something went wrong.