如何在 botframework 的语言生成功能中从 LG 文件访问状态
How to access State from LG file in language generation feature of botframework
根据 LG 文件中的这些文章,您应该能够直接访问机器人状态,而无需将值作为范围传递:
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
即它提到:
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.
其中一个示例看起来能够从 UserState 访问变量 'name':
# 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?
我已经用几种方法对其进行了测试,但我就是无法从 LG 文件访问 UserState 内存。这可能吗?
k
并不是说你可以直接访问机器人状态,而是 adaptive/composer 机器人中的内存(及其作用域)存储在状态中,可以通过作用域和 LG 进行存储和访问。您提供的文章片段中的关键是“内存”(在本例中,指的是自适应内存范围及其值)。
例如,我可以提示用户输入他们的姓名并将其存储在 user.name
中。但这与 turn.activity.from.name
不同(它不是来自状态的东西,但通常保存到状态中)。 user.name
默认情况下不会填充。您当然可以从 turn.activity.from.name
中获取该值并存储在 user.name
中(可能取决于频道,或者有其他逻辑等)
您可以使用以下方法对此进行更多测试:
对话:
{
"$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}
并且使用 Emulator,您可能会在 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
},
根据 LG 文件中的这些文章,您应该能够直接访问机器人状态,而无需将值作为范围传递: 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
即它提到:
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.
其中一个示例看起来能够从 UserState 访问变量 'name':
# 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?
我已经用几种方法对其进行了测试,但我就是无法从 LG 文件访问 UserState 内存。这可能吗?
k
并不是说你可以直接访问机器人状态,而是 adaptive/composer 机器人中的内存(及其作用域)存储在状态中,可以通过作用域和 LG 进行存储和访问。您提供的文章片段中的关键是“内存”(在本例中,指的是自适应内存范围及其值)。
例如,我可以提示用户输入他们的姓名并将其存储在 user.name
中。但这与 turn.activity.from.name
不同(它不是来自状态的东西,但通常保存到状态中)。 user.name
默认情况下不会填充。您当然可以从 turn.activity.from.name
中获取该值并存储在 user.name
中(可能取决于频道,或者有其他逻辑等)
您可以使用以下方法对此进行更多测试:
对话:
{
"$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}
并且使用 Emulator,您可能会在 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
},