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

Feature: Assistant Endpoints (Create, Modify, Delete, List) - Success #741

Merged
merged 20 commits into from
May 23, 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
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
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.openai.generated.model.ModifyAssistantRequest
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.github.oshai.kotlinlogging.KLogger
import io.ktor.client.request.*
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() {
fun Routing.assistantRoutes(logger: KLogger) {
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)
val assistant = Assistant(request)
val response = assistant.get()
logger.info("Created assistant: ${response.name} with id: ${response.id}")
call.respond(status = HttpStatusCode.Created, response)
} else {
call.respond(
Expand All @@ -36,53 +37,68 @@ fun Routing.assistantRoutes() {
}
}

// 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
// }
get("/v1/settings/assistants") {
try {
val token = call.getToken()
val openAI = OpenAI(Config(token = token.value), logRequests = true)
val assistantsApi = openAI.assistants
val response =
assistantsApi.listAssistants(configure = { header("OpenAI-Beta", "assistants=v1") })
call.respond(HttpStatusCode.OK, response)
} catch (e: Exception) {
val trace = e.stackTraceToString()
logger.error("Error creating assistant: $trace")
call.respond(HttpStatusCode.BadRequest, "Invalid request: $trace")
}
}

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()
put("/v1/settings/assistants/{id}") {
try {
val contentType = call.request.contentType()
if (contentType == ContentType.Application.Json) {
val request = call.receive<ModifyAssistantRequest>()
val id = call.parameters["id"]
if (id == null) {
call.respond(HttpStatusCode.BadRequest, "Invalid assistant id")
return@put
}
val assistant = Assistant(id)
val response = assistant.modify(request).get()
logger.info("Modified assistant: ${response.name} with id: ${response.id}")
call.respond(HttpStatusCode.OK, response)
} else {
call.respond(
HttpStatusCode.UnsupportedMediaType,
"Unsupported content type: $contentType"
)
}
} catch (e: Exception) {
val trace = e.stackTraceToString()
logger.error("Error modifying assistant: $trace")
call.respond(HttpStatusCode.BadRequest, "Invalid request: $trace")
}
}

return listAssistants
delete("/v1/settings/assistants/{id}") {
try {
val token = call.getToken()
val id = call.parameters["id"]
if (id == null) {
call.respond(HttpStatusCode.BadRequest, "Invalid assistant id")
return@delete
}
val openAI = OpenAI(Config(token = token.value), logRequests = true)
val assistantsApi = openAI.assistants
val assistant = assistantsApi.getAssistant(id)
val response =
assistantsApi.deleteAssistant(id, configure = { header("OpenAI-Beta", "assistants=v1") })
logger.info("Deleted assistant: ${assistant.name} with id: ${response.id}")
call.respond(status = HttpStatusCode.NoContent, response)
} catch (e: Exception) {
val trace = e.stackTraceToString()
logger.error("Error deleting assistant: $trace")
call.respond(HttpStatusCode.BadRequest, "Invalid request: $trace")
}
}
}
}

/*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,5 +12,5 @@ fun Routing.xefRoutes(logger: KLogger) {
organizationRoutes(OrganizationRepositoryService(logger))
projectsRoutes(ProjectRepositoryService(logger))
tokensRoutes(TokenRepositoryService(logger))
assistantRoutes()
assistantRoutes(logger)
}
2 changes: 1 addition & 1 deletion server/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</encoder>
</appender>

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

Expand Down
1 change: 1 addition & 0 deletions server/web/src/assets/assistants-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions server/web/src/assets/chat-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions server/web/src/assets/generic-question-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions server/web/src/assets/home-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions server/web/src/assets/organizations-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions server/web/src/assets/projects-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions server/web/src/assets/settings-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions server/web/src/components/Pages/Assistants/Assistants.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.container {
position: relative;
height: 100%;
}

.sliders {
display: flex;
justify-content: space-between;
align-items: center;
}

.boldText {
font-weight: bold;
}
Loading