Skip to content

Commit

Permalink
refact: validate and update 'stream' value
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam committed Mar 9, 2021
1 parent 2aa216b commit c309b59
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ internal class OpenAIApi(config: OpenAIConfig) : OpenAI {
}

override suspend fun completion(engineId: EngineId, request: CompletionRequest?): TextCompletion {
return httpClient.post(path = "/v1/engines/$engineId/completions", body = request ?: EmptyContent)
val body = if (request?.stream == true) request.copy(stream = false) else request
return httpClient.post(path = "/v1/engines/$engineId/completions", body = body ?: EmptyContent)
}

override fun completions(engineId: EngineId, request: CompletionRequest?): Flow<TextCompletion> {
val body = if (request?.stream != true) request?.copy(stream = true) else request
return flow {
httpClient.post<HttpStatement>(
path = "/v1/engines/$engineId/completions",
body = request ?: EmptyContent
body = body ?: EmptyContent
).execute {
val channel = it.receive<ByteReadChannel>()
while (!channel.isClosedForRead) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlinx.serialization.Serializable
* [documentation](https://beta.openai.com/docs/api-reference/create-completion)
*/
@Serializable
public class CompletionRequest(
public data class CompletionRequest(
/**
* The prompt(s) to generate completions for, encoded as a string, a list of strings, or a list of token lists.
*
Expand Down
12 changes: 2 additions & 10 deletions sample/js/src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,12 @@ suspend fun main() {
println(ada)

println("\n>️ Creating completion...")
val completionRequest = CompletionRequest(
prompt = "Somebody once told me the world is gonna roll me",
echo = true
)
val completionRequest = CompletionRequest(prompt = "Somebody once told me the world is gonna roll me")
openAI.completion(EngineId.Ada, completionRequest).choices.forEach(::println)

println("\n>️ Creating completion stream...")
val completionRequestStream = CompletionRequest(
prompt = "Somebody once told me the world is gonna roll me",
stream = true,
)

val scope = CoroutineScope(coroutineContext)
openAI.completions(EngineId.Ada, completionRequestStream)
openAI.completions(EngineId.Ada, completionRequest)
.onEach { print(it.choices[0].text) }
.onCompletion { println() }
.launchIn(scope)
Expand Down
11 changes: 2 additions & 9 deletions sample/jvm/src/main/kotlin/com/aallam/openai/sample/jvm/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,11 @@ fun main() = runBlocking {
println(ada)

println("\n>️ Creating completion...")
val completionRequest = CompletionRequest(
prompt = "Somebody once told me the world is gonna roll me",
echo = true
)
val completionRequest = CompletionRequest(prompt = "Somebody once told me the world is gonna roll me")
openAI.completion(EngineId.Ada, completionRequest).choices.forEach(::println)

println("\n>️ Creating completion stream...")
val completionRequestStream = CompletionRequest(
prompt = "Somebody once told me the world is gonna roll me",
stream = true,
)
openAI.completions(EngineId.Ada, completionRequestStream)
openAI.completions(EngineId.Ada, completionRequest)
.onEach { print(it.choices[0].text) }
.onCompletion { println() }
.launchIn(this)
Expand Down
11 changes: 2 additions & 9 deletions sample/native/src/nativeMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,11 @@ fun main(): Unit = runBlocking {
println(ada)

println("\n>️ Creating completion...")
val completionRequest = CompletionRequest(
prompt = "Somebody once told me the world is gonna roll me",
echo = true
)
val completionRequest = CompletionRequest(prompt = "Somebody once told me the world is gonna roll me")
openAI.completion(EngineId.Ada, completionRequest).choices.forEach(::println)

println("\n>️ Creating completion stream...")
val completionRequestStream = CompletionRequest(
prompt = "Somebody once told me the world is gonna roll me",
stream = true,
)
openAI.completions(EngineId.Ada, completionRequestStream)
openAI.completions(EngineId.Ada, completionRequest)
.onEach { print(it.choices[0].text) }
.onCompletion { println() }
.launchIn(this)
Expand Down

0 comments on commit c309b59

Please sign in to comment.