Skip to content

Commit

Permalink
Merge branch 'main' into evaluator/markdown-exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Montagon authored Apr 15, 2024
2 parents d8ed4ef + d33f606 commit 6c5447c
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 117 deletions.
1 change: 0 additions & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ kotlin {
val jvmMain by getting {
dependencies {
implementation(libs.ktor.http)
implementation(libs.logback)
implementation(libs.rss.reader)
api(libs.jackson)
api(libs.jackson.schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.jvm.JvmName
import kotlinx.coroutines.flow.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive

class AssistantThread(
val threadId: String,
Expand Down Expand Up @@ -81,77 +82,107 @@ class AssistantThread(
when (event) {
// submit tool outputs and join streams
is RunDelta.RunRequiresAction -> {
val steps =
runSteps(event.run.id).map { metric.assistantCreateRunStep(event.run.id) { it } }

steps.forEach { step ->
val calls = step.stepDetails.toolCalls()
takeRequiredAction(1, event, this@AssistantThread, assistant, this)
}
// previous to submitting tool outputs we let all events pass through the outer flow
else -> {
emit(event)
}
}
}
}

val run = getRun(event.run.id)
if (
run.status == RunObject.Status.requires_action &&
run.requiredAction?.type == RunObjectRequiredAction.Type.submit_tool_outputs
) {
val callsResult: List<Pair<String, Assistant.Companion.ToolOutput>> =
calls
.filterIsInstance<
RunStepDetailsToolCallsObjectToolCallsInner.CaseRunStepDetailsToolCallsFunctionObject
>()
.parMapNotNull { toolCall -> executeToolCall(toolCall, assistant) }
val results: Map<String, Assistant.Companion.ToolOutput> = callsResult.toMap()
val toolOutputsRequest =
SubmitToolOutputsRunRequest(
toolOutputs =
results.map { (toolCallId, result) ->
SubmitToolOutputsRunRequestToolOutputsInner(
toolCallId = toolCallId,
output =
Json.encodeToString(Assistant.Companion.ToolOutput.serializer(), result)
)
}
)
metric.assistantToolOutputsRun(event.run.id) {
api
.submitToolOuputsToRunStream(
threadId = threadId,
runId = event.run.id,
submitToolOutputsRunRequest = toolOutputsRequest,
configure = ::defaultConfig
)
.collect {
val delta = RunDelta.fromServerSentEvent(it)
if (delta is RunDelta.RunStepCompleted) {
emit(delta)
emit(RunDelta.RunSubmitToolOutputs(toolOutputsRequest))
} else {
emit(delta)
}
}
getRun(event.run.id)
}
private suspend fun takeRequiredAction(
depth: Int,
event: RunDelta.RunRequiresAction,
assistantThread: AssistantThread,
assistant: Assistant,
flowCollector: FlowCollector<RunDelta>
) {
if (
event.run.status == RunObject.Status.requires_action &&
event.run.requiredAction?.type == RunObjectRequiredAction.Type.submit_tool_outputs
) {
val calls = event.run.requiredAction?.submitToolOutputs?.toolCalls.orEmpty()
val callsResult: List<Pair<String, Assistant.Companion.ToolOutput>> =
calls.parMapNotNull { toolCall -> assistantThread.executeToolCall(toolCall, assistant) }
val results: Map<String, Assistant.Companion.ToolOutput> = callsResult.toMap()
val toolOutputsRequest =
SubmitToolOutputsRunRequest(
toolOutputs =
results.map { (toolCallId, result) ->
SubmitToolOutputsRunRequestToolOutputsInner(
toolCallId = toolCallId,
output = Json.encodeToString(Assistant.Companion.ToolOutput.serializer(), result)
)
}
)
val run =
metric.assistantToolOutputsRun(event.run.id) {
api
.submitToolOuputsToRunStream(
threadId = threadId,
runId = event.run.id,
submitToolOutputsRunRequest = toolOutputsRequest,
configure = ::defaultConfig
)
.collect {
val delta = RunDelta.fromServerSentEvent(it)
if (delta is RunDelta.RunStepCompleted) {
flowCollector.emit(RunDelta.RunSubmitToolOutputs(toolOutputsRequest))
}
flowCollector.emit(delta)
}
}
// previous to submitting tool outputs we let all events pass through the outer flow
else -> emit(event)
val run = getRun(event.run.id)
val finalEvent =
when (run.status) {
RunObject.Status.queued -> RunDelta.RunQueued(run)
RunObject.Status.in_progress -> RunDelta.RunInProgress(run)
RunObject.Status.requires_action -> RunDelta.RunRequiresAction(run)
RunObject.Status.cancelling -> RunDelta.RunCancelling(run)
RunObject.Status.cancelled -> RunDelta.RunCancelled(run)
RunObject.Status.failed -> RunDelta.RunFailed(run)
RunObject.Status.completed -> RunDelta.RunCompleted(run)
RunObject.Status.expired -> RunDelta.RunExpired(run)
}
flowCollector.emit(finalEvent)
run
}

if (run.status == RunObject.Status.requires_action) {
takeRequiredAction(
depth + 1,
RunDelta.RunRequiresAction(run),
assistantThread,
assistant,
flowCollector
)
}
}
}

private suspend fun executeToolCall(
toolCall: RunStepDetailsToolCallsObjectToolCallsInner.CaseRunStepDetailsToolCallsFunctionObject,
toolCall: RunToolCallObject,
assistant: Assistant
): Pair<String, Assistant.Companion.ToolOutput>? {
val function = toolCall.value.function
val functionName = function.name
val functionArguments = function.arguments
return if (functionName != null && functionArguments != null) {
val result = assistant.getToolRegistered(functionName, functionArguments)
val callId = toolCall.value.id
if (callId != null) {
callId to result
return try {
val function = toolCall.function
val functionName = function.name
val functionArguments = function.arguments
return if (functionName != null && functionArguments != null) {
val result = assistant.getToolRegistered(functionName, functionArguments)
val callId = toolCall.id
if (callId != null) {
callId to result
} else null
} else null
} else null
} catch (e: Throwable) {
toolCall.id to
Assistant.Companion.ToolOutput(
schema = JsonObject(emptyMap()),
result = JsonObject(mapOf("error" to JsonPrimitive(e.message ?: "Unknown error")))
)
}
}

suspend fun getRun(runId: String): RunObject =
Expand Down
1 change: 0 additions & 1 deletion core/src/commonTest/resources/logback-test.xml

This file was deleted.

11 changes: 0 additions & 11 deletions detekt-rules/src/main/resources/logback.xml

This file was deleted.

4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exposed = "0.48.0"
kotlin = "1.9.23"
kotlinx-json = "1.6.2"
kotlinx-datetime = "0.5.0"
ktor = "2.3.9"
ktor = "2.3.10"
ktor-logging = "0.4.0"
spotless = "6.25.0"
okio = "3.9.0"
Expand All @@ -27,7 +27,7 @@ pdfbox = "3.0.2"
mysql = "8.0.33"
semverGradle = "0.5.0-rc.6"
jackson = "2.16.1"
jsonschema = "4.34.0"
jsonschema = "4.35.0"
jakarta = "3.0.2"
suspendApp = "0.4.0"
flyway = "9.22.3"
Expand Down
1 change: 0 additions & 1 deletion integrations/gcp/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ kotlin {
...
val jvmMain by getting {
dependencies {
implementation(libs.logback)
api(libs.ktor.client.cio)
}
}
Expand Down
1 change: 0 additions & 1 deletion integrations/gcp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ kotlin {
}
val jvmMain by getting {
dependencies {
implementation(libs.logback)
api(libs.ktor.client.cio)
}
}
Expand Down
1 change: 0 additions & 1 deletion integrations/mlflow/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ kotlin {
}
val jvmMain by getting {
dependencies {
implementation(libs.logback)
api(libs.ktor.client.cio)
}
}
Expand Down
1 change: 0 additions & 1 deletion openai-client/client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ kotlin {
}
val jvmMain by getting {
dependencies {
implementation(libs.logback)
api(libs.ktor.client.cio)
}
}
Expand Down
1 change: 0 additions & 1 deletion reasoning/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ kotlin {
}
val jvmMain by getting {
dependencies {
implementation(libs.logback)
implementation(projects.xefPdf)
implementation(projects.xefFilesystem)
api(libs.ktor.client.cio)
Expand Down
1 change: 0 additions & 1 deletion server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ dependencies {
implementation(libs.ktor.server.cors)
implementation(libs.ktor.server.request.validation)
implementation(libs.ktor.server.status.pages)
implementation(libs.logback)
implementation(libs.suspendApp.core)
implementation(libs.suspendApp.ktor)
implementation(libs.uuid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.xebia.functional.xef.server.http.routes.xefRoutes
import com.xebia.functional.xef.server.services.hikariDataSource
import com.xebia.functional.xef.server.services.vectorStoreService
import com.xebia.functional.xef.store.migrations.runDatabaseMigrations
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.auth.Auth
Expand All @@ -30,13 +31,12 @@ import io.ktor.server.resources.Resources
import io.ktor.server.routing.routing
import kotlinx.coroutines.awaitCancellation
import org.jetbrains.exposed.sql.Database
import org.slf4j.LoggerFactory

object Server {
@JvmStatic
fun main(args: Array<String>) = SuspendApp {
resourceScope {
val logger = LoggerFactory.getLogger("xef-server")
val logger = KotlinLogging.logger("xef-server")

val config = ConfigFactory.load("database.conf").resolve()
val xefDBConfig = XefDatabaseConfig.load("xef-database", config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import com.xebia.functional.xef.server.services.OrganizationRepositoryService
import com.xebia.functional.xef.server.services.ProjectRepositoryService
import com.xebia.functional.xef.server.services.TokenRepositoryService
import com.xebia.functional.xef.server.services.UserRepositoryService
import io.github.oshai.kotlinlogging.KLogger
import io.ktor.server.routing.*
import org.slf4j.Logger

fun Routing.xefRoutes(logger: Logger) {
fun Routing.xefRoutes(logger: KLogger) {
userRoutes(UserRepositoryService(logger))
organizationRoutes(OrganizationRepositoryService(logger))
projectsRoutes(ProjectRepositoryService(logger))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import com.xebia.functional.xef.server.db.tables.Organization
import com.xebia.functional.xef.server.db.tables.User
import com.xebia.functional.xef.server.models.*
import com.xebia.functional.xef.server.models.exceptions.XefExceptions.*
import io.github.oshai.kotlinlogging.KLogger
import kotlinx.datetime.Clock
import org.jetbrains.exposed.sql.SizedCollection
import org.jetbrains.exposed.sql.transactions.transaction
import org.slf4j.Logger

class OrganizationRepositoryService(private val logger: Logger) {
class OrganizationRepositoryService(private val logger: KLogger) {
fun createOrganization(data: OrganizationRequest, token: Token): OrganizationSimpleResponse {
logger.info("Creating organization with name: ${data.name}")
logger.info { "Creating organization with name: ${data.name}" }
return transaction {
// Getting the user from the token
val user = token.getUser()
Expand All @@ -30,7 +30,7 @@ class OrganizationRepositoryService(private val logger: Logger) {
}

fun getOrganizations(token: Token): List<OrganizationFullResponse> {
logger.info("Getting organizations")
logger.info { "Getting organizations" }
return transaction {
// Getting the user from the token
val user = token.getUser()
Expand All @@ -41,7 +41,7 @@ class OrganizationRepositoryService(private val logger: Logger) {
}

fun getOrganization(token: Token, id: Int): OrganizationFullResponse {
logger.info("Getting organizations")
logger.info { "Getting organizations" }
return transaction {
// Getting the user from the token
val user = token.getUser()
Expand All @@ -53,7 +53,7 @@ class OrganizationRepositoryService(private val logger: Logger) {
}

fun getUsersInOrganization(token: Token, id: Int): List<UserResponse> {
logger.info("Getting users in organization")
logger.info { "Getting users in organization" }
return transaction {
// Getting the user from the token
val user = token.getUser()
Expand All @@ -71,7 +71,7 @@ class OrganizationRepositoryService(private val logger: Logger) {
data: OrganizationUpdateRequest,
id: Int
): OrganizationFullResponse {
logger.info("Updating organization with name: ${data.name}")
logger.info { "Updating organization with name: ${data.name}" }
return transaction {
// Getting the user from the token
val user = token.getUser()
Expand All @@ -95,7 +95,7 @@ class OrganizationRepositoryService(private val logger: Logger) {
}

fun deleteOrganization(token: Token, id: Int) {
logger.info("Deleting organization with id: $id")
logger.info { "Deleting organization with id: $id" }
transaction {
val user = token.getUser()
val organization =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import com.xebia.functional.xef.store.VectorStore
import com.xebia.functional.xef.store.postgresql.PGDistanceStrategy
import com.xebia.functional.xef.store.postgresql.addNewCollection
import com.xebia.functional.xef.store.postgresql.connection
import io.github.oshai.kotlinlogging.KLogger
import javax.sql.DataSource
import kotlinx.uuid.UUID
import kotlinx.uuid.generateUUID
import org.slf4j.Logger

class PostgresVectorStoreService(
private val logger: Logger,
private val logger: KLogger,
private val dataSource: DataSource,
private val collectionName: String,
private val vectorSize: Int,
Expand All @@ -33,7 +33,7 @@ class PostgresVectorStoreService(
bind(uuid.toString())
bind(collectionName)
}
.also { logger.info("Created collection $collectionName") }
.also { logger.info { "Created collection $collectionName" } }
}
}

Expand Down
Loading

0 comments on commit 6c5447c

Please sign in to comment.