You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2021/07/22 08:52:36 UTC

[incubator-nlpcraft] branch NLPCRAFT-371 created (now 6921b14)

This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a change to branch NLPCRAFT-371
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git.


      at 6921b14  Merge branch 'master' into NLPCRAFT-371

This branch includes the following new commits:

     new 4564f42  WIP.
     new 6921b14  Merge branch 'master' into NLPCRAFT-371

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[incubator-nlpcraft] 02/02: Merge branch 'master' into NLPCRAFT-371

Posted by se...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch NLPCRAFT-371
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git

commit 6921b14c82d2b4bbda6d99a38e471445adb96df2
Merge: 4564f42 8c0c347
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Thu Jul 22 11:52:16 2021 +0300

    Merge branch 'master' into NLPCRAFT-371

 .../lightswitch/src/main/resources/lightswitch_model.yaml         | 2 ++
 nlpcraft-examples/minecraft/src/main/resources/minecraft.yaml     | 2 ++
 nlpcraft-examples/sql/src/main/resources/sql_model.yaml           | 2 ++
 nlpcraft/src/main/scala/org/apache/nlpcraft/model/NCVariant.java  | 8 ++++----
 4 files changed, 10 insertions(+), 4 deletions(-)

[incubator-nlpcraft] 01/02: WIP.

Posted by se...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch NLPCRAFT-371
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git

commit 4564f4230dd0d1727db12c5950acaa7c2bd2248d
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Wed Jul 21 15:26:31 2021 +0300

    WIP.
---
 .../opencensus/NCOpenCensusServerStats.scala       |  5 +-
 .../nlpcraft/server/rest/NCBasicRestApi.scala      | 68 +++++++++++++++++++++-
 .../scala/org/apache/nlpcraft/NCTestElement.scala  | 20 ++++++-
 .../nlpcraft/server/rest/NCRestModelSpec.scala     | 22 ++++++-
 .../nlpcraft/server/rest/RestTestModel.scala       |  5 +-
 5 files changed, 110 insertions(+), 10 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/opencensus/NCOpenCensusServerStats.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/opencensus/NCOpenCensusServerStats.scala
index 25340d7..dde284a 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/opencensus/NCOpenCensusServerStats.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/opencensus/NCOpenCensusServerStats.scala
@@ -17,9 +17,7 @@
 
 package org.apache.nlpcraft.server.opencensus
 
-import java.util.Collections
 import io.opencensus.stats.Measure._
-import io.opencensus.stats.View.Name
 import io.opencensus.stats._
 import org.apache.nlpcraft.common.opencensus.NCOpenCensusStats
 
@@ -31,6 +29,7 @@ trait NCOpenCensusServerStats extends NCOpenCensusStats {
     val M_ASK_LATENCY_MS: MeasureLong = MeasureLong.create("ask_latency", "The latency of '/ask' REST call", "ms")
     val M_CHECK_LATENCY_MS: MeasureLong = MeasureLong.create("check_latency", "The latency of '/check' REST call", "ms")
     val M_MODEL_SUGSYN_LATENCY_MS: MeasureLong = MeasureLong.create("model_inspect_latency", "The latency of '/model/inspect' REST call", "ms")
+    val M_MODEL_SYNS_LATENCY_MS: MeasureLong = MeasureLong.create("model_synonyms_latency", "The latency of '/model/syns' REST call", "ms")
     val M_CANCEL_LATENCY_MS: MeasureLong = MeasureLong.create("cancel_latency", "The latency of '/cancel' REST call", "ms")
     val M_SIGNIN_LATENCY_MS: MeasureLong = MeasureLong.create("signin_latency", "The latency of '/signin' REST call", "ms")
     val M_SIGNOUT_LATENCY_MS: MeasureLong = MeasureLong.create("signout_latency", "The latency of '/signout' REST call", "ms")
@@ -77,6 +76,7 @@ trait NCOpenCensusServerStats extends NCOpenCensusStats {
             mkViews(M_COMPANY_TOKEN_LATENCY_MS, "company/token"),
             mkViews(M_COMPANY_DELETE_LATENCY_MS, "company/delete"),
             mkViews(M_MODEL_SUGSYN_LATENCY_MS, "model/sugsyn"),
+            mkViews(M_MODEL_SYNS_LATENCY_MS, "model/syns"),
             mkViews(M_USER_ADD_LATENCY_MS, "user/add"),
             mkViews(M_USER_GET_LATENCY_MS, "user/get"),
             mkViews(M_USER_DELETE_LATENCY_MS, "user/delete"),
@@ -87,7 +87,6 @@ trait NCOpenCensusServerStats extends NCOpenCensusStats {
             mkViews(M_FEEDBACK_ADD_LATENCY_MS, "feedback/add"),
             mkViews(M_FEEDBACK_DELETE_LATENCY_MS, "feedback/delete"),
             mkViews(M_FEEDBACK_GET_LATENCY_MS, "feedback/get"),
-            mkViews(M_MODEL_SUGSYN_LATENCY_MS, "model/sugsyn"),
             mkViews(M_PROBE_ALL_LATENCY_MS, "probe/all"),
             mkViews(M_ROUND_TRIP_LATENCY_MS, "roundTrip/latdist"),
         ).flatten
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
index 45ab892..70368f1 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
@@ -36,6 +36,7 @@ import org.apache.nlpcraft.common.pool.NCThreadPoolManager
 import org.apache.nlpcraft.common.util.NCUtils.{jsonToJavaMap, uncompress}
 import org.apache.nlpcraft.common.{JavaMeta, NCE, U}
 import org.apache.nlpcraft.model.NCModelView
+import org.apache.nlpcraft.probe.mgrs.model.NCModelManager
 import org.apache.nlpcraft.server.apicodes.NCApiStatusCode.{API_OK, _}
 import org.apache.nlpcraft.server.company.NCCompanyManager
 import org.apache.nlpcraft.server.feedback.NCFeedbackManager
@@ -786,15 +787,15 @@ class NCBasicRestApi extends NCRestApi with LazyLogging with NCOpenCensusTrace w
       * @return
       */
     protected def sugsyn$(): Route = {
-        case class Req$Model$Sugsyn$(
+        case class Req$Model$Sugsyn(
             acsTok: String,
             mdlId: String,
             minScore: Option[Double]
         )
 
-        implicit val reqFmt: RootJsonFormat[Req$Model$Sugsyn$] = jsonFormat3(Req$Model$Sugsyn$)
+        implicit val reqFmt: RootJsonFormat[Req$Model$Sugsyn] = jsonFormat3(Req$Model$Sugsyn)
 
-        entity(as[Req$Model$Sugsyn$]) { req =>
+        entity(as[Req$Model$Sugsyn]) { req =>
             startScopedSpan(
                 "model$sugsyn",
                 "acsTok" -> req.acsTok,
@@ -819,6 +820,66 @@ class NCBasicRestApi extends NCRestApi with LazyLogging with NCOpenCensusTrace w
         }
     }
 
+
+    /**
+      *
+      * @return
+      */
+    protected def elementSynonyms$(): Route = {
+        case class Req$Model$Syn(
+            acsTok: String,
+            mdlId: String,
+            elemId: String
+        )
+
+        implicit val reqFmt: RootJsonFormat[Req$Model$Syn] = jsonFormat3(Req$Model$Syn)
+
+        case class Res$Model$Value(
+            name: String,
+            synonyms: Seq[String]
+        )
+
+        case class Res$Model$Element(
+            status: String,
+            synonyms: Seq[String],
+            values: Seq[Res$Model$Value]
+        )
+
+        implicit val resValFmt: RootJsonFormat[Res$Model$Value] = jsonFormat2(Res$Model$Value)
+        implicit val resFmt: RootJsonFormat[Res$Model$Element] = jsonFormat3(Res$Model$Element)
+
+        entity(as[Req$Model$Syn]) { req =>
+            startScopedSpan(
+                "model$syns",
+                "acsTok" -> req.acsTok,
+                "mdlId" -> req.mdlId,
+                "elemId" -> req.elemId) { span =>
+                checkLength("acsTok" -> req.acsTok, "mdlId" -> req.mdlId)
+
+                val admUsr = authenticateAsAdmin(req.acsTok)
+
+                checkModelId(req.mdlId, admUsr.companyId)
+
+                val elm =
+                    NCModelManager.
+                        getModel(req.mdlId, span).
+                        model.
+                        getElements.asScala.find(_.getId == req.elemId).getOrElse(throw InvalidModelId(req.elemId))
+
+                complete {
+                    Res$Model$Element(
+                        API_OK,
+                        elm.getSynonyms.asScala.toSeq,
+                        if (elm.getValues != null)
+                            elm.getValues.asScala.map(p => Res$Model$Value(p.getName, p.getSynonyms.asScala.toSeq)).toSeq
+                        else
+                            Seq.empty
+                    )
+                }
+            }
+        }
+    }
+
     /**
       *
       * @return
@@ -1977,6 +2038,7 @@ class NCBasicRestApi extends NCRestApi with LazyLogging with NCOpenCensusTrace w
                                     path(API / "feedback" / "delete") { withMetric(M_FEEDBACK_DELETE_LATENCY_MS, feedback$Delete) } ~
                                     path(API / "probe" / "all") { withMetric(M_PROBE_ALL_LATENCY_MS, probe$All) } ~
                                     path(API / "model" / "sugsyn") { withMetric(M_MODEL_SUGSYN_LATENCY_MS, sugsyn$) } ~
+                                    path(API / "model" / "syns") { withMetric(M_MODEL_SYNS_LATENCY_MS, elementSynonyms$) } ~
                                     path(API / "ask") { withMetric(M_ASK_LATENCY_MS, ask$) } ~
                                     path(API / "ask" / "sync") { withMetric(M_ASK_SYNC_LATENCY_MS, ask$Sync) }
                                 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestElement.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestElement.scala
index dc465d8..3598d2e 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestElement.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestElement.scala
@@ -17,7 +17,7 @@
 
 package org.apache.nlpcraft
 
-import org.apache.nlpcraft.model.NCElement
+import org.apache.nlpcraft.model.{NCElement, NCValue}
 
 import java.util
 import scala.jdk.CollectionConverters.{SeqHasAsJava, SetHasAsJava}
@@ -27,8 +27,11 @@ import scala.language.implicitConversions
   * Simple test element.
   */
 case class NCTestElement(id: String, syns: String*) extends NCElement {
+    private val values = new util.ArrayList[NCValue]
+
     override def getId: String = id
     override def getSynonyms: util.List[String] = (syns :+ id).asJava
+    override def getValues: util.List[NCValue] = values
 }
 
 /**
@@ -38,4 +41,19 @@ object NCTestElement {
     private def to(e: NCTestElement): NCElement = e
 
     implicit def to(set: Set[NCTestElement]): util.Set[NCElement] = set.map(to).asJava
+
+    def apply(id: String, syns: Seq[String], vals: Map[String, Seq[String]]): NCTestElement = {
+        val e = NCTestElement(id, syns :_*)
+
+        e.getValues.addAll(
+            vals.map { case (value, syns) =>
+                new NCValue {
+                    override def getName: String = value
+                    override def getSynonyms: util.List[String] = syns.asJava
+                }
+            }.toSeq.asJava
+        )
+
+        e
+    }
 }
\ No newline at end of file
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala
index d47a297..86fb1af 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala
@@ -29,7 +29,7 @@ import scala.jdk.CollectionConverters.ListHasAsScala
 @NCTestEnvironment(model = classOf[RestTestModel], startClient = false)
 class NCRestModelSpec extends NCRestSpec {
     @Test
-    def test(): Unit = {
+    def testSugsyn(): Unit = {
         def extract(data: JList[java.util.Map[String, Object]]): Seq[Double] =
             data.asScala.map(_.get("score").asInstanceOf[Number].doubleValue()).toSeq
 
@@ -55,4 +55,24 @@ class NCRestModelSpec extends NCRestSpec {
             })
         )
     }
+
+    @Test
+    def testSyns(): Unit = {
+        def extract(data: JList[java.util.Map[String, Object]]): Seq[Double] =
+            data.asScala.map(_.get("score").asInstanceOf[Number].doubleValue()).toSeq
+
+        // Note that checked values are valid for current configuration of `RestTestModel` model.
+        post("model/syns", "mdlId" -> "rest.test.model", "elemId" -> "x")(
+            ("$.status", (status: String) => assertEquals("API_OK", status)),
+            ("$.synonyms", (data: ResponseList) => {
+                println("data="+data)
+            })
+        )
+        post("model/syns", "mdlId" -> "rest.test.model", "elemId" -> "valElem")(
+            ("$.status", (status: String) => assertEquals("API_OK", status)),
+            ("$.synonyms", (data: ResponseList) => {
+                println("data="+data)
+            })
+        )
+    }
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/RestTestModel.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/RestTestModel.scala
index 8099e39..8319862 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/RestTestModel.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/RestTestModel.scala
@@ -18,7 +18,7 @@
 package org.apache.nlpcraft.server.rest
 
 import org.apache.nlpcraft.NCTestElement
-import org.apache.nlpcraft.model.{NCElement, NCIntent, NCIntentSample, NCModelAdapter, NCResult}
+import org.apache.nlpcraft.model.{NCElement, NCIntent, NCIntentSample, NCModelAdapter, NCResult, NCValue}
 
 import java.util
 
@@ -49,7 +49,8 @@ class RestTestModel extends NCModelAdapter("rest.test.model", "REST test model",
             NCTestElement("a"),
             NCTestElement("b"),
             NCTestElement("x", "cat"),
-            NCTestElement("meta")
+            NCTestElement("meta"),
+            NCTestElement("valElem", Seq("valElem1"), Map("v1"->Seq("v11", "v12"), "v2" -> Seq("v21")))
         )
 
     @NCIntent("intent=onA term(t)={tok_id() == 'a'}")