Skip to content

Commit

Permalink
Add isLenient = true. Add logging to Rasa. Add raw response to Rasa.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikvoloshin committed Apr 13, 2022
1 parent c352bd2 commit 32e324a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package com.justai.jaicf.activator.caila

import kotlinx.serialization.json.Json

internal val JSON = Json { ignoreUnknownKeys = true; encodeDefaults = true }
internal val JSON = Json { ignoreUnknownKeys = true; isLenient = true; encodeDefaults = true }
1 change: 1 addition & 0 deletions activators/rasa/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ dependencies {
core()
api(ktor("ktor-client-cio"))
api(ktor("ktor-client-serialization-jvm"))
api(ktor("ktor-client-logging-jvm"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity>
val rasaIntent: Intent,
val entities: List<Entity>,
val rawResponse: JsonObject
): IntentActivatorContext(
confidence = rasaIntent.confidence,
intent = rasaIntent.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,13 +24,14 @@ class RasaIntentActivator(

override fun recogniseIntent(botContext: BotContext, request: BotRequest): List<IntentActivatorContext> {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,42 @@ 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<RasaParseMessageResponse>("$uri/model/parse".toUrl()) {
client.post<JsonObject>("$uri/model/parse".toUrl()) {
contentType(ContentType.Application.Json)
body = request
}
} catch (e: Exception) {
logger.error("Cannot parse $request", e)
null
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.serialization.modules.SerializersModule
internal val JSON = Json {

ignoreUnknownKeys = true
isLenient = true
classDiscriminator = "type"
encodeDefaults = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.serialization.modules.SerializersModule
internal val JSON = Json {
classDiscriminator = "type"
ignoreUnknownKeys = true
isLenient = true
encodeDefaults = true

serializersModule = SerializersModule {
Expand Down

0 comments on commit 32e324a

Please sign in to comment.