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

frontend + endpoints wip #737

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
implementation(libs.jmf)
implementation(libs.mp3.wav.converter)
api(libs.ktor.client)

}

spotless {
Expand Down
279 changes: 279 additions & 0 deletions examples/src/main/resources/documents/ghibli.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ dependencies {
testImplementation(libs.kotest.testcontainers)
testImplementation(libs.testcontainers.postgresql)
testRuntimeOnly(libs.kotest.junit5)
implementation(libs.logback)
implementation(libs.klogging)
}

spotless {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object Server {
requestTimeout = 0 // disabled
}
install(Auth)
install(Logging) { level = LogLevel.INFO }
install(Logging) { level = LogLevel.ALL }
install(ClientContentNegotiation)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.xebia.functional.xef.server.http.routes

import com.xebia.functional.openai.generated.model.AssistantObject
import com.xebia.functional.openai.generated.model.CreateAssistantRequest
import com.xebia.functional.openai.generated.model.ListAssistantsResponse
import com.xebia.functional.xef.Config
import com.xebia.functional.xef.OpenAI
import com.xebia.functional.xef.llm.assistants.Assistant
import com.xebia.functional.xef.server.models.Token
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.*

fun Routing.assistantRoutes() {
authenticate("auth-bearer") {
post("/v1/settings/assistants") {
try {
val contentType = call.request.contentType()
if (contentType == ContentType.Application.Json) {
val request = call.receive<CreateAssistantRequest>()
val token = call.getToken()
val response = createAssistant(token, request)
call.respond(status = HttpStatusCode.Created, response)
} else {
call.respond(
HttpStatusCode.UnsupportedMediaType,
"Unsupported content type: $contentType"
)
}
} catch (e: Exception) {
val trace = e.stackTraceToString()
call.respond(HttpStatusCode.BadRequest, "Invalid request: $trace")
}
}

// put("/v1/settings/assistants/{id}") {
// val request = Json.decodeFromString<AssistantRequest>(call.receive<String>())
// val token = call.getToken()
// val id = call.getId()
// val response = updateAssistant(token, request, id)
// call.respond(status = HttpStatusCode.NoContent, response)
// }
// get("/v1/settings/assistants") {
// val token = call.getToken()
// val response = ListAssistantsResponse("list", emptyList(), null, null, false)
// val assistantResponse = listAssistants(token, response)
// call.respond(assistantResponse)
// }
// delete("/v1/settings/assistants/{id}") {
// val token = call.getToken()
// val id = call.parameters["id"]?.toIntOrNull()
// if (id == null) {
// call.respond(HttpStatusCode.BadRequest, "Invalid assistant id")
// return@delete
// }
// val response = deleteAssistant(token, id)
// call.respond(status = HttpStatusCode.NoContent, response)
// }
}
}

suspend fun createAssistant(token: Token, request: CreateAssistantRequest): AssistantObject {
val openAIConfig = Config(token = token.value)
val openAI = OpenAI(openAIConfig, logRequests = true)
val assistants = openAI.assistants
val assistant = Assistant(request)
return assistant.get()
}

// suspend fun updateAssistant(token: String, request: AssistantRequest, id: Int): String {
// // Implement the logic for updating an assistant in OpenAI here
// }

suspend fun listAssistants(token: Token, response: ListAssistantsResponse): ListAssistantsResponse {
val openAIConfig = Config(token = token.value)
val openAI = OpenAI(openAIConfig)
val assistants = openAI.assistants
val listAssistants = assistants.listAssistants()

return listAssistants
}

/*suspend fun deleteAssistant(token: String, id: Int): String {
// Implement the logic for deleting an assistant in OpenAI here
}*/
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ fun Routing.xefRoutes(logger: KLogger) {
organizationRoutes(OrganizationRepositoryService(logger))
projectsRoutes(ProjectRepositoryService(logger))
tokensRoutes(TokenRepositoryService(logger))
assistantRoutes()
}
26 changes: 26 additions & 0 deletions server/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>

<configuration>
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />

<appender name="NOOP" class="ch.qos.logback.core.helpers.NOPAppender" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp- %msg%n</pattern>
</encoder>
</appender>

<root level="trace">
<appender-ref ref="STDOUT"/>
</root>

<logger name="com.xebia.functional.xef" level="debug">
<appender-ref ref="STDOUT" />
</logger>

<logger name="com.gargoylesoftware.htmlunit" level="off">
<appender-ref ref="STDOUT" />
</logger>
</configuration>
Loading
Loading