diff --git a/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/JSON.kt b/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/JSON.kt index 8da30fc99..4f912d9dd 100644 --- a/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/JSON.kt +++ b/activators/caila/src/main/kotlin/com/justai/jaicf/activator/caila/JSON.kt @@ -2,4 +2,4 @@ package com.justai.jaicf.activator.caila import kotlinx.serialization.json.Json -internal val JSON = Json { ignoreUnknownKeys = true; encodeDefaults = true } \ No newline at end of file +internal val JSON = Json { ignoreUnknownKeys = true; isLenient = true; encodeDefaults = true } \ No newline at end of file diff --git a/activators/rasa/build.gradle.kts b/activators/rasa/build.gradle.kts index ed78db295..cb4ef7943 100644 --- a/activators/rasa/build.gradle.kts +++ b/activators/rasa/build.gradle.kts @@ -15,4 +15,5 @@ dependencies { core() api(ktor("ktor-client-cio")) api(ktor("ktor-client-serialization-jvm")) + api(ktor("ktor-client-logging-jvm")) } diff --git a/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/RasaActivatorContext.kt b/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/RasaActivatorContext.kt index 3ed61969b..10e2634a8 100644 --- a/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/RasaActivatorContext.kt +++ b/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/RasaActivatorContext.kt @@ -4,10 +4,12 @@ import com.justai.jaicf.activator.intent.IntentActivatorContext import com.justai.jaicf.activator.rasa.api.Entity import com.justai.jaicf.activator.rasa.api.Intent import com.justai.jaicf.context.ActivatorContext +import kotlinx.serialization.json.JsonObject data class RasaActivatorContext( - private val rasaIntent: Intent, - private val entities: List + val rasaIntent: Intent, + val entities: List, + val rawResponse: JsonObject ): IntentActivatorContext( confidence = rasaIntent.confidence, intent = rasaIntent.name diff --git a/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/RasaIntentActivator.kt b/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/RasaIntentActivator.kt index ad6cf869e..be85fd7cd 100644 --- a/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/RasaIntentActivator.kt +++ b/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/RasaIntentActivator.kt @@ -5,6 +5,7 @@ import com.justai.jaicf.activator.intent.BaseIntentActivator import com.justai.jaicf.activator.intent.IntentActivatorContext import com.justai.jaicf.activator.rasa.api.RasaApi import com.justai.jaicf.activator.rasa.api.RasaParseMessageRequest +import com.justai.jaicf.activator.rasa.api.RasaParseMessageResponse import com.justai.jaicf.api.BotRequest import com.justai.jaicf.api.hasQuery import com.justai.jaicf.context.BotContext @@ -23,13 +24,14 @@ class RasaIntentActivator( override fun recogniseIntent(botContext: BotContext, request: BotRequest): List { val messageId = UUID.randomUUID().toString() - val response = api.parseMessage(RasaParseMessageRequest(request.input, messageId)) + val json = api.parseMessage(RasaParseMessageRequest(request.input, messageId)) ?: return emptyList() + val response = api.Json.decodeFromJsonElement(RasaParseMessageResponse.serializer(), json) - response?.ranking ?: return emptyList() + response.ranking ?: return emptyList() return response.ranking .filter { it.confidence > confidenceThreshold } - .map { RasaActivatorContext(it, response.entities.orEmpty()) } + .map { RasaActivatorContext(it, response.entities.orEmpty(), json) } } class Factory( diff --git a/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/api/RasaApi.kt b/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/api/RasaApi.kt index 18366066f..cb9abcfa1 100644 --- a/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/api/RasaApi.kt +++ b/activators/rasa/src/main/kotlin/com/justai/jaicf/activator/rasa/api/RasaApi.kt @@ -6,27 +6,35 @@ import io.ktor.client.HttpClient import io.ktor.client.engine.cio.CIO import io.ktor.client.features.json.JsonFeature import io.ktor.client.features.json.serializer.KotlinxSerializer +import io.ktor.client.features.logging.* import io.ktor.client.request.post import io.ktor.http.ContentType import io.ktor.http.contentType import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject class RasaApi( private val uri: String ) : WithLogger { + internal val Json = Json { ignoreUnknownKeys = true; isLenient = true } + private val client = HttpClient(CIO) { expectSuccess = true install(JsonFeature) { - serializer = KotlinxSerializer(Json { ignoreUnknownKeys = true }) + serializer = KotlinxSerializer(Json) + } + + install(Logging) { + level = LogLevel.INFO } } - fun parseMessage(request: RasaParseMessageRequest) = runBlocking { + fun parseMessage(request: RasaParseMessageRequest): JsonObject? = runBlocking { try { - client.post("$uri/model/parse".toUrl()) { + client.post("$uri/model/parse".toUrl()) { contentType(ContentType.Application.Json) body = request } @@ -34,5 +42,6 @@ class RasaApi( logger.error("Cannot parse $request", e) null } + } } \ No newline at end of file diff --git a/channels/aimybox/src/main/kotlin/com/justai/jaicf/channel/aimybox/JSON.kt b/channels/aimybox/src/main/kotlin/com/justai/jaicf/channel/aimybox/JSON.kt index b88917302..eccfe5d77 100644 --- a/channels/aimybox/src/main/kotlin/com/justai/jaicf/channel/aimybox/JSON.kt +++ b/channels/aimybox/src/main/kotlin/com/justai/jaicf/channel/aimybox/JSON.kt @@ -7,6 +7,7 @@ import kotlinx.serialization.modules.SerializersModule internal val JSON = Json { ignoreUnknownKeys = true + isLenient = true classDiscriminator = "type" encodeDefaults = true diff --git a/channels/yandex-alice/src/main/kotlin/com/justai/jaicf/channel/yandexalice/JSON.kt b/channels/yandex-alice/src/main/kotlin/com/justai/jaicf/channel/yandexalice/JSON.kt index e588d7926..58b49021d 100644 --- a/channels/yandex-alice/src/main/kotlin/com/justai/jaicf/channel/yandexalice/JSON.kt +++ b/channels/yandex-alice/src/main/kotlin/com/justai/jaicf/channel/yandexalice/JSON.kt @@ -10,6 +10,7 @@ import kotlinx.serialization.modules.SerializersModule internal val JSON = Json { classDiscriminator = "type" ignoreUnknownKeys = true + isLenient = true encodeDefaults = true serializersModule = SerializersModule {