Skip to content

Commit

Permalink
Alice NLU support (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
morfeusys authored Jun 30, 2020
1 parent 5820b31 commit 851feac
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ buildscript {
allprojects {

group = "com.justai.jaicf"
version = "0.4.3"
version = "0.5.0"

repositories {
google()
Expand Down
33 changes: 33 additions & 0 deletions channels/yandex-alice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,39 @@ state("auth") {

Бибилиотека автоматически записывает заголовок с токеном авторизации в поле `request.alice?.accessToken`.

### NLP в Алисе
Яндекс Диалоги предоставляют возможность] обрабатываеть запросы пользователя на естественном языке.
Подробнее читайте в [документации Яндекс Диалогов](https://yandex.ru/dev/dialogs/alice/doc/nlu-docpage/).

Чтобы использовать этот функционал в вашем сценарии, добавьте в настройки вашего агента активатор `AliceIntentActivator`, например:

```kotlin
val skill = BotEngine(
model = MainScenario.model,
activators = arrayOf(
AliceIntentActivator,
BaseEventActivator,
CatchAllActivator
)
)
```

После этого вы сможете обрабатывать интенты Алисы и ваши собственные интенты в вашем сценарии:

```kotlin
state("repeat") {
activators {
intent(AliceIntent.REPEAT)
}

action {
reactions.say("Повторяю...")
}
}
```

Слоты доступны с помощью `activator.alice?.slots`.

# Вопросы и предложения

Если вы нашли баг, то можете написать о нем в [issues](https://github.com/just-ai/jaicf-kotlin/issues) или предложить вашу реализацию и отправить pull request.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.justai.jaicf.channel.yandexalice.api.model
package com.justai.jaicf.channel.yandexalice

object AliceIntent {
const val CONFIRM = "YANDEX.CONFIRM"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.justai.jaicf.channel.yandexalice.activator

import com.justai.jaicf.activator.Activator
import com.justai.jaicf.activator.ActivatorFactory
import com.justai.jaicf.activator.intent.BaseIntentActivator
import com.justai.jaicf.activator.intent.IntentActivatorContext
import com.justai.jaicf.api.BotRequest
import com.justai.jaicf.channel.yandexalice.api.AliceBotRequest
import com.justai.jaicf.context.BotContext
import com.justai.jaicf.model.scenario.ScenarioModel

class AliceIntentActivator(model: ScenarioModel): BaseIntentActivator(model) {

override fun canHandle(request: BotRequest) = request is AliceBotRequest

override fun recogniseIntent(botContext: BotContext, request: BotRequest): IntentActivatorContext? {
val aliceRequest = request as? AliceBotRequest ?: return null

val intent = aliceRequest.request?.let { req ->
req.nlu.intents.map { entry ->
findState(entry.key, botContext) to entry
}.maxBy { it.first?.count { c -> c == '/'} ?: 0 }?.second
}

return intent?.let {
AliceIntentActivatorContext(it.key, it.value)
}
}

companion object : ActivatorFactory {
override fun create(model: ScenarioModel) = AliceIntentActivator(model)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.justai.jaicf.channel.yandexalice.activator

import com.justai.jaicf.activator.intent.IntentActivatorContext
import com.justai.jaicf.channel.yandexalice.api.Request
import com.justai.jaicf.context.ActivatorContext

class AliceIntentActivatorContext(
intentName: String,
intentSlots: Request.Nlu.Intent
): IntentActivatorContext(
confidence = 1f,
intent = intentName
) {
val slots = intentSlots.slots
}

val ActivatorContext.alice
get() = this as? AliceIntentActivatorContext
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.justai.jaicf.channel.yandexalice.api
import com.justai.jaicf.api.BotRequest
import com.justai.jaicf.api.BotRequestType
import com.justai.jaicf.channel.yandexalice.AliceEvent
import com.justai.jaicf.channel.yandexalice.api.model.AliceIntent
import com.justai.jaicf.channel.yandexalice.AliceIntent
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
Expand Down Expand Up @@ -81,7 +81,7 @@ data class User(
@SerialName("user_id")
val userId: String,
@SerialName("access_token")
val accessToken: String?
val accessToken: String? = null
)

@Serializable
Expand Down Expand Up @@ -137,7 +137,7 @@ data class Request(
data class Slot(
val type: String,
val value: JsonElement,
val tokens: Tokens?
val tokens: Tokens? = null
)

@Serializable
Expand Down

0 comments on commit 851feac

Please sign in to comment.