Warm tip: This article is reproduced from serverfault.com, please click

How to access State from LG file in language generation feature of botframework

发布于 2020-11-27 07:57:38

As per these articles from LG file you should be able to access bot state directly, without passing values as scope: https://docs.microsoft.com/en-us/composer/concept-language-generation https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-language-generation?view=azure-bot-service-4.0&tabs=csharp

i.e. its mentioned:

Language generation is achieved through: A Markdown based .lg file that contains the templates and their composition. Full access to the current bot's memory so you can data bind language to the state of memory.

One of examples there are looking like they are able to access variable 'name' from the UserState:

# greetingTemplate
- Hello ${user.name}, how are you?
- Good morning ${user.name}.It's nice to see you again.
- Good day ${user.name}. What can I do for you today?

I've tested it in few ways but I simply cannot access the UserState memory from LG file. Is that possible?

Questioner
Kuba K
Viewed
0
Dana V 2020-12-01 08:26:41

k

It's not that you can directly access the bot state, but that the memory (and their scopes) in adaptive/composer bots are stored in state, and can be stored and accessed via scopes and LG. The key in in the article snippet you supplied is "memory" (In this case, referring to the adaptive memory scopes and their values).

For example, I can prompt the user for their name and store that in user.name. But that is not the same as turn.activity.from.name (which is not something from state, but is often saved into state). user.name doesn't get populated by default. You could of course take that value from turn.activity.from.name and store in user.name (maybe depending on the channel, or have other logic, etc.)

You can test this a bit more by using this:

Dialog:

{
  "$kind": "Microsoft.AdaptiveDialog",
  "$designer": {
    "id": "7zj5aK",
    "name": "GatherUserInfo",
    "description": ""
  },
  "autoEndDialog": true,
  "defaultResultProperty": "dialog.result",
  "triggers": [
    {
      "$kind": "Microsoft.OnBeginDialog",
      "$designer": {
        "name": "BeginDialog",
        "description": "",
        "id": "A25t0H"
      },
      "actions": [
        {
          "$kind": "Microsoft.TextInput",
          "$designer": {
            "id": "SOE0Ku"
          },
          "disabled": false,
          "maxTurnCount": 3,
          "alwaysPrompt": false,
          "allowInterruptions": false,
          "prompt": "${TextInput_Prompt_SOE0Ku()}",
          "unrecognizedPrompt": "",
          "invalidPrompt": "",
          "defaultValueResponse": "",
          "property": "user.name"
        },
        {
          "$kind": "Microsoft.TextInput",
          "$designer": {
            "id": "3sNA0B"
          },
          "disabled": false,
          "maxTurnCount": 3,
          "alwaysPrompt": false,
          "allowInterruptions": false,
          "prompt": "${TextInput_Prompt_3sNA0B()}",
          "unrecognizedPrompt": "",
          "invalidPrompt": "",
          "defaultValueResponse": "",
          "property": "user.gender"
        },
        {
          "$kind": "Microsoft.NumberInput",
          "$designer": {
            "id": "SsEiOF"
          },
          "defaultLocale": "en-us",
          "disabled": false,
          "maxTurnCount": 3,
          "alwaysPrompt": false,
          "allowInterruptions": false,
          "prompt": "${NumberInput_Prompt_SsEiOF()}",
          "unrecognizedPrompt": "",
          "invalidPrompt": "",
          "defaultValueResponse": "",
          "property": "user.age"
        },
        {
          "$kind": "Microsoft.SendActivity",
          "$designer": {
            "id": "CD7TVN"
          },
          "activity": "${SendActivity_CD7TVN()}"
        }
      ]
    }
  ],
  "generator": "GatherUserInfo.lg",
  "recognizer": "GatherUserInfo.lu.qna",
  "id": "GatherUserInfo"
}

LG:

# TextInput_Prompt_SOE0Ku()
 - What is your name?
# TextInput_Prompt_3sNA0B()
 - What is your gender?
# NumberInput_Prompt_SsEiOF()
 - What is your age?
# SendActivity_CD7TVN()
 - Your name is ${user.name}, you are ${user.gender} and your age is ${user.age}. You are also ${turn.activity.from.name}

And using Emulator, you might see something like this in cosmos:

{
    "id": "emulator*2fusers*2fa1464f41-f237-42dd-ad01-ca8fb150b97a",
    "realId": "emulator/users/a1464f41-f237-42dd-ad01-ca8fb150b97a",
    "document": {
        "$type": "System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[System.Object, System.Private.CoreLib]], System.Private.CoreLib",
        "name": "DanaVCosmos",
        "gender": "MaleCosmos",
        "age": 45
    },