Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ChatCompletion respone duplicating headers #462

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import io.ktor.server.auth.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.*
import io.ktor.utils.io.jvm.javaio.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.boolean
import kotlinx.serialization.json.jsonPrimitive
import java.nio.charset.Charset

enum class Provider {
OPENAI, GPT4ALL, GCP
Expand All @@ -39,21 +41,22 @@ fun Routing.aiRoutes(
authenticate("auth-bearer") {
post("/chat/completions") {
val token = call.getToken()
val body = call.receive<String>()
val byteArrayBody = call.receiveChannel().toByteArray()
val body = byteArrayBody.toString(Charsets.UTF_8)
val data = Json.decodeFromString<JsonObject>(body)

val isStream = data["stream"]?.jsonPrimitive?.boolean ?: false

if (!isStream) {
client.makeRequest(call, "$openAiUrl/chat/completions", body, token)
client.makeRequest(call, "$openAiUrl/chat/completions", byteArrayBody, token)
} else {
client.makeStreaming(call, "$openAiUrl/chat/completions", body, token)
client.makeStreaming(call, "$openAiUrl/chat/completions", byteArrayBody, token)
}
}

post("/embeddings") {
val token = call.getToken()
val context = call.receive<String>()
val context = call.receiveChannel().toByteArray()
client.makeRequest(call, "$openAiUrl/embeddings", context, token)
}
}
Expand All @@ -62,32 +65,27 @@ fun Routing.aiRoutes(
private suspend fun HttpClient.makeRequest(
call: ApplicationCall,
url: String,
body: String,
body: ByteArray,
token: Token
) {
val response = this.request(url) {
headers {
bearerAuth(token.value)
}
headers.copyFrom(call.request.headers)
contentType(ContentType.Application.Json)
method = HttpMethod.Post
setBody(body)
}
call.response.headers.copyFrom(response.headers)
call.respond(response.status, response.body<String>())
call.respond(response.status, response.readBytes())
}

private suspend fun HttpClient.makeStreaming(
call: ApplicationCall,
url: String,
body: String,
body: ByteArray,
token: Token
) {
this.preparePost(url) {
headers {
bearerAuth(token.value)
}
contentType(ContentType.Application.Json)
headers.copyFrom(call.request.headers)
method = HttpMethod.Post
setBody(body)
}.execute { httpResponse ->
Expand All @@ -107,6 +105,10 @@ private fun ResponseHeaders.copyFrom(headers: Headers) = headers
values.forEach { value -> this.appendIfAbsent(key, value) }
}

internal fun HeadersBuilder.copyFrom(headers: Headers) = headers
.filter { key, value -> !key.equals("HOST", ignoreCase = true) }
.forEach { key, values -> appendAll(key, values) }

private fun ApplicationCall.getProvider(): Provider =
request.headers["xef-provider"]?.toProvider()
?: Provider.OPENAI
Expand Down