Skip to content

Instantly share code, notes, and snippets.

@laiso
Created May 21, 2023 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laiso/8beb6da93429b7f857d27f2f5e164958 to your computer and use it in GitHub Desktop.
Save laiso/8beb6da93429b7f857d27f2f5e164958 to your computer and use it in GitHub Desktop.
Chat completetion streaming with openai-kotlin in unite test
package com.example.openaikotlintest
import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.chat.ChatCompletionRequest
import com.aallam.openai.api.chat.ChatMessage
import com.aallam.openai.api.chat.ChatRole
import com.aallam.openai.api.http.Timeout
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.OpenAI
import com.aallam.openai.client.OpenAIConfig
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.test.runTest
import kotlin.time.Duration.Companion.seconds
import org.junit.Test
import org.junit.Assert.*
@OptIn(BetaOpenAI::class)
class ExampleUnitTest {
@Test
fun chatCompletionStream() {
runTest(timeout = 600.seconds) {
val scope = CoroutineScope(coroutineContext)
val apiKey = "{OPENAI_API_KEY}"
val config = OpenAIConfig(
token = apiKey,
timeout = Timeout(socket = 60.seconds)
)
val openAI = OpenAI(config)
val chatCompletionRequest = ChatCompletionRequest(
model = ModelId("gpt-3.5-turbo"),
messages = listOf(
ChatMessage(
role = ChatRole.System,
content = "You are a helpful assistant that reply user message."
),
ChatMessage(
role = ChatRole.User,
content = "Please write a poem as long as possible."
)
)
)
openAI.chatCompletions(chatCompletionRequest)
.onEach { print(it.choices.first().delta?.content.orEmpty()) }
.onCompletion { println("DONE") }
.launchIn(scope)
.join()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment