-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor and removing non-necessary dependency (#459)
- Loading branch information
Showing
3 changed files
with
133 additions
and
127 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
121 changes: 121 additions & 0 deletions
121
server/src/main/kotlin/com/xebia/functional/xef/server/http/routes/AIRoutes.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,121 @@ | ||
package com.xebia.functional.xef.server.http.routes | ||
|
||
import com.aallam.openai.api.BetaOpenAI | ||
import com.xebia.functional.xef.server.models.Token | ||
import com.xebia.functional.xef.server.models.exceptions.XefExceptions | ||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
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 | ||
|
||
enum class Provider { | ||
OPENAI, GPT4ALL, GCP | ||
} | ||
|
||
fun String.toProvider(): Provider? = when (this) { | ||
"openai" -> Provider.OPENAI | ||
"gpt4all" -> Provider.GPT4ALL | ||
"gcp" -> Provider.GCP | ||
else -> Provider.OPENAI | ||
} | ||
|
||
@OptIn(BetaOpenAI::class) | ||
fun Routing.aiRoutes( | ||
client: HttpClient | ||
) { | ||
val openAiUrl = "https://api.openai.com/v1" | ||
|
||
authenticate("auth-bearer") { | ||
post("/chat/completions") { | ||
val token = call.getToken() | ||
val body = call.receive<String>() | ||
val data = Json.decodeFromString<JsonObject>(body) | ||
|
||
val isStream = data["stream"]?.jsonPrimitive?.boolean ?: false | ||
|
||
if (!isStream) { | ||
client.makeRequest(call, "$openAiUrl/chat/completions", body, token) | ||
} else { | ||
client.makeStreaming(call, "$openAiUrl/chat/completions", body, token) | ||
} | ||
} | ||
|
||
post("/embeddings") { | ||
val token = call.getToken() | ||
val context = call.receive<String>() | ||
client.makeRequest(call, "$openAiUrl/embeddings", context, token) | ||
} | ||
} | ||
} | ||
|
||
private suspend fun HttpClient.makeRequest( | ||
call: ApplicationCall, | ||
url: String, | ||
body: String, | ||
token: Token | ||
) { | ||
val response = this.request(url) { | ||
headers { | ||
bearerAuth(token.value) | ||
} | ||
contentType(ContentType.Application.Json) | ||
method = HttpMethod.Post | ||
setBody(body) | ||
} | ||
call.response.headers.copyFrom(response.headers) | ||
call.respond(response.status, response.body<String>()) | ||
} | ||
|
||
private suspend fun HttpClient.makeStreaming( | ||
call: ApplicationCall, | ||
url: String, | ||
body: String, | ||
token: Token | ||
) { | ||
this.preparePost(url) { | ||
headers { | ||
bearerAuth(token.value) | ||
} | ||
contentType(ContentType.Application.Json) | ||
method = HttpMethod.Post | ||
setBody(body) | ||
}.execute { httpResponse -> | ||
call.response.headers.copyFrom(httpResponse.headers) | ||
call.respondOutputStream { | ||
httpResponse | ||
.bodyAsChannel() | ||
.copyTo(this@respondOutputStream) | ||
} | ||
} | ||
} | ||
|
||
private fun ResponseHeaders.copyFrom(headers: Headers) = headers | ||
.entries() | ||
.filter { (key, _) -> !HttpHeaders.isUnsafe(key) } // setting unsafe headers results in exception | ||
.forEach { (key, values) -> | ||
values.forEach { value -> this.appendIfAbsent(key, value) } | ||
} | ||
|
||
private fun ApplicationCall.getProvider(): Provider = | ||
request.headers["xef-provider"]?.toProvider() | ||
?: Provider.OPENAI | ||
|
||
fun ApplicationCall.getToken(): Token = | ||
principal<UserIdPrincipal>()?.name?.let { Token(it) } ?: throw XefExceptions.AuthorizationException("No token found") | ||
|
||
fun ApplicationCall.getId(): Int = getInt("id") | ||
|
||
fun ApplicationCall.getInt(field: String): Int = | ||
this.parameters[field]?.toInt() ?: throw XefExceptions.ValidationException("Invalid $field") | ||
|
125 changes: 9 additions & 116 deletions
125
server/src/main/kotlin/com/xebia/functional/xef/server/http/routes/XefRoutes.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 |
---|---|---|
@@ -1,123 +1,16 @@ | ||
package com.xebia.functional.xef.server.http.routes | ||
|
||
import com.aallam.openai.api.BetaOpenAI | ||
import com.xebia.functional.xef.server.models.Token | ||
import com.xebia.functional.xef.server.models.exceptions.XefExceptions | ||
import com.xebia.functional.xef.server.services.VectorStoreService | ||
import com.xebia.functional.xef.server.services.OrganizationRepositoryService | ||
import com.xebia.functional.xef.server.services.ProjectRepositoryService | ||
import com.xebia.functional.xef.server.services.UserRepositoryService | ||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
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 org.slf4j.Logger | ||
|
||
enum class Provider { | ||
OPENAI, GPT4ALL, GCP | ||
} | ||
|
||
fun String.toProvider(): Provider? = when (this) { | ||
"openai" -> Provider.OPENAI | ||
"gpt4all" -> Provider.GPT4ALL | ||
"gcp" -> Provider.GCP | ||
else -> Provider.OPENAI | ||
} | ||
|
||
@OptIn(BetaOpenAI::class) | ||
fun Routing.genAIRoutes( | ||
client: HttpClient, | ||
vectorStoreService: VectorStoreService | ||
) { | ||
val openAiUrl = "https://api.openai.com/v1" | ||
|
||
authenticate("auth-bearer") { | ||
post("/chat/completions") { | ||
val token = call.getToken() | ||
val body = call.receive<String>() | ||
val data = Json.decodeFromString<JsonObject>(body) | ||
|
||
val isStream = data["stream"]?.jsonPrimitive?.boolean ?: false | ||
|
||
if (!isStream) { | ||
client.makeRequest(call, "$openAiUrl/chat/completions", body, token) | ||
} else { | ||
client.makeStreaming(call, "$openAiUrl/chat/completions", body, token) | ||
} | ||
} | ||
|
||
post("/embeddings") { | ||
val token = call.getToken() | ||
val context = call.receive<String>() | ||
client.makeRequest(call, "$openAiUrl/embeddings", context, token) | ||
} | ||
} | ||
} | ||
|
||
private suspend fun HttpClient.makeRequest( | ||
call: ApplicationCall, | ||
url: String, | ||
body: String, | ||
token: Token | ||
fun Routing.xefRoutes( | ||
logger: Logger | ||
) { | ||
val response = this.request(url) { | ||
headers { | ||
bearerAuth(token.value) | ||
} | ||
contentType(ContentType.Application.Json) | ||
method = HttpMethod.Post | ||
setBody(body) | ||
} | ||
call.response.headers.copyFrom(response.headers) | ||
call.respond(response.status, response.body<String>()) | ||
userRoutes(UserRepositoryService(logger)) | ||
organizationRoutes(OrganizationRepositoryService(logger)) | ||
projectsRoutes(ProjectRepositoryService(logger)) | ||
} | ||
|
||
private suspend fun HttpClient.makeStreaming( | ||
call: ApplicationCall, | ||
url: String, | ||
body: String, | ||
token: Token | ||
) { | ||
this.preparePost(url) { | ||
headers { | ||
bearerAuth(token.value) | ||
} | ||
contentType(ContentType.Application.Json) | ||
method = HttpMethod.Post | ||
setBody(body) | ||
}.execute { httpResponse -> | ||
call.response.headers.copyFrom(httpResponse.headers) | ||
call.respondOutputStream { | ||
httpResponse | ||
.bodyAsChannel() | ||
.copyTo(this@respondOutputStream) | ||
} | ||
} | ||
} | ||
|
||
private fun ResponseHeaders.copyFrom(headers: Headers) = headers | ||
.entries() | ||
.filter { (key, _) -> !HttpHeaders.isUnsafe(key) } // setting unsafe headers results in exception | ||
.forEach { (key, values) -> | ||
values.forEach { value -> this.appendIfAbsent(key, value) } | ||
} | ||
|
||
private fun ApplicationCall.getProvider(): Provider = | ||
request.headers["xef-provider"]?.toProvider() | ||
?: Provider.OPENAI | ||
|
||
fun ApplicationCall.getToken(): Token = | ||
principal<UserIdPrincipal>()?.name?.let { Token(it) } ?: throw XefExceptions.AuthorizationException("No token found") | ||
|
||
fun ApplicationCall.getId(): Int = getInt("id") | ||
|
||
fun ApplicationCall.getInt(field: String): Int = | ||
this.parameters[field]?.toInt() ?: throw XefExceptions.ValidationException("Invalid $field") | ||
|