-
Notifications
You must be signed in to change notification settings - Fork 288
Add LG/Expression library #1381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b95a8fa
init lg library
Danieladu a197775
refine code
Danieladu ff1087e
fix deploy error
Danieladu 8528ef7
fix test coverage
Danieladu e9b1977
remove xPath and xml functions (#1385)
cosmicshuai c4102cf
Merge branch 'master' into LG
boydc2014 13f88d5
[expression] add capability of handling null in string related built-…
cosmicshuai 4f27089
Merge branch 'master' into LG
Stevenic e5697f7
remove tslint
Danieladu f19d8ac
fix errors and warnings with eslint
Danieladu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/**/node_modules | ||
/**/.vscode | ||
/**/lib | ||
#coverage | ||
coverage | ||
.nyc_output | ||
/**/.antlr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extension": [ | ||
".js" | ||
], | ||
"include": [ | ||
"lib/**/*.js" | ||
], | ||
"exclude": [ | ||
"**/node_modules/**", | ||
"**/tests/**", | ||
"**/coverage/**", | ||
"**/*.d.ts" | ||
], | ||
"reporter": [ | ||
"html" | ||
], | ||
"all": true, | ||
"cache": true | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# Language Generation ***_[PREVIEW]_*** | ||
|
||
Learning from our customers experiences and bringing together capabilities first implemented by Cortana and Cognition teams, we are introducing Language Generation; which allows the developer to extract the embedded strings from their code and resource files and manage them through a Language Generation runtime and file format. Language Generation enable customers to define multiple variations on a phrase, execute simple expressions based on context, refer to conversational memory, and over time will enable us to bring additional capabilities all leading to a more natural conversational experience. | ||
|
||
At the core of language generation lies template expansion and entity substitution. You can provide one-of variation for expansion as well as conditionally expand a template. The output from language generation can be a simple text string or multi-line response or a complex object payload that a layer above language generation will use to construct a full blown [activity][1]. | ||
|
||
Language generation is achieved through: | ||
|
||
- markdown based .lg file that describes the templates and their composition. See [here][3] for the .lg file format. | ||
- full access to current bots memory so you can data bind language to the state of memory. | ||
- parser and runtime libraries that help achieve runtime resolution. See [here][2] for API-reference. | ||
|
||
```markdown | ||
# 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? | ||
``` | ||
|
||
You can use language generation to: | ||
|
||
- achieve a coherent personality, tone of voice for your bot | ||
- separate business logic from presentation | ||
- include variations and sophisticated composition based resolution for any of your bot's replies | ||
- construct speak .vs. display adaptations | ||
- construct cards, suggested actions and attachments. | ||
|
||
## Speak .vs. display adaptation | ||
|
||
By design, the .lg file format does not explicitly support the ability to provide speak .vs. display adaptation. The file format supports simple constructs that are composable and supports resolution on multi-line text and so you can have syntax and semantics for speak .vs. display adaptation, cards, suggested actions etc that can be interpreted as simple text and transformed into the Bot Framework [activity][1] by a layer above language generation. | ||
|
||
Bot Builder SDK supports a short hand notation that can parse and transform a piece of text separated by `displayText`||`spokenText` into speak and display text. | ||
|
||
```markdown | ||
# greetingTemplate | ||
- hi || hi there | ||
- hello || hello, what can I help with today | ||
``` | ||
|
||
You can use the `TextMessageActivityGenerator.CreateActityFromText` method to transform the command into a Bot Framework activity to post back to the user. | ||
|
||
## Using Chatdown style cards | ||
|
||
[Chatdown][6] introduced a simple markdown based way to write mock conversations. Also introduced as part of the [.chat][7] file format was the ability to express different [message commands][9] via simple text representation. Message commands include [cards][10], [Attachments][11] and suggested actions. | ||
|
||
You can include message commands via multi-line text in the .lg file format and use the `TextMessageActivityGenerator.CreateActityFromText` method to transform the command into a Bot Framework activity to post back to the user. | ||
|
||
See [here][8] for examples of how different card types are represented in .chat file format. | ||
|
||
Here is an example of a card definition. | ||
|
||
```markdown | ||
# HeroCardTemplate(buttonsCollection) | ||
- ``` | ||
[Herocard | ||
title=@{TitleText())} | ||
subtitle=@{SubText())} | ||
text=@{DescriptionText())} | ||
images=@{CardImages())} | ||
buttons=@{join(buttonsCollection, '|')] | ||
``` | ||
|
||
# TitleText | ||
- Here are some [TitleSuffix] | ||
|
||
# TitleSuffix | ||
- cool photos | ||
- pictures | ||
- nice snaps | ||
|
||
# SubText | ||
- What is your favorite? | ||
- Don't they all look great? | ||
- sorry, some of them are repeats | ||
|
||
# DescriptionText | ||
- This is description for the hero card | ||
|
||
# CardImages | ||
- https://picsum.photos/200/200?image=100 | ||
- https://picsum.photos/300/200?image=200 | ||
- https://picsum.photos/200/200?image=400 | ||
``` | ||
|
||
|
||
## Language Generation in action | ||
|
||
When building a bot, you can use language generation in several different ways. To start with, examine your current bot's code (or the new bot you plan to write) and create [.lg file][3] to cover all possible scenarios where you would like to use the language generation sub-system with your bot's replies to user. | ||
|
||
Then make sure you include the platform specific language generation library. | ||
|
||
For C#, add Microsoft.Bot.Builder.LanguageGeneration. | ||
For NodeJS, add botbuilder-lg | ||
|
||
Load the template manager with your .lg file(s) | ||
|
||
```typescript | ||
// multi lg files | ||
let lgEngine = new TemplateEngine.addFiles(filePaths, importResolver?); | ||
|
||
// single lg file | ||
let lgEngine = new TemplateEngine.addFile(filePath, importResolver?); | ||
``` | ||
|
||
When you need template expansion, call the templateEngine and pass in the relevant template name | ||
|
||
```typescript | ||
await turnContext.sendActivity(lgEngine.evaluateTemplate("<TemplateName>", entitiesCollection)); | ||
``` | ||
|
||
If your template needs specific entity values to be passed for resolution/ expansion, you can pass them in on the call to `evaluateTemplate` | ||
|
||
```typescript | ||
await turnContext.sendActivity(lgEngine.evaluateTemplate("WordGameReply", { GameName = "MarcoPolo" } )); | ||
``` | ||
|
||
## Multi-lingual generation and language fallback policy | ||
Quite often your bot might target more than one spoken/ display language. To help with resource management as well as implement a default language fall back policy, you can either use `MultiLanguageGenerator` or `ResourceMultiLanguageGenerator`. See [here][25] for an example. | ||
|
||
## Grammar check and correction | ||
|
||
The current library does not include any capabilities for grammar check or correction. | ||
|
||
|
||
[1]:https://github.com/Microsoft/BotBuilder/blob/master/specs/botframework-activity/botframework-activity.md | ||
[2]:https://github.com/microsoft/BotBuilder-Samples/blob/master/experimental/language-generation/docs/api-reference.md | ||
[3]:https://github.com/microsoft/BotBuilder-Samples/blob/master/experimental/language-generation/docs/lg-file-format.md | ||
[6]:https://github.com/Microsoft/botbuilder-tools/tree/master/packages/Chatdown | ||
[7]:https://github.com/Microsoft/botbuilder-tools/tree/master/packages/Chatdown#chat-file-format | ||
[8]:https://github.com/Microsoft/botbuilder-tools/blob/master/packages/Chatdown/Examples/CardExamples.chat | ||
[9]:https://github.com/Microsoft/botbuilder-tools/tree/master/packages/Chatdown#message-commands | ||
[10]:https://github.com/Microsoft/botbuilder-tools/tree/master/packages/Chatdown#message-cards | ||
[11]:https://github.com/Microsoft/botbuilder-tools/tree/master/packages/Chatdown#message-attachments | ||
[25]:https://github.com/microsoft/botbuilder-dotnet/blob/d953d1b7fe548cdb1800f1c2e85fe35c34edf75c/tests/Microsoft.Bot.Builder.LanguageGeneration.Renderer.Tests/LGGeneratorTests.cs#L78 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "botbuilder-lg", | ||
"description": "Typescript version of Microsoft.Expression from C# version (https://github.com/Microsoft/botbuilder-dotnet/tree/ComposableDialog/libraries/Microsoft.CommonExpressions) ", | ||
"version": "4.1.6", | ||
"license": "MIT", | ||
"keywords": [ | ||
"botbuilder", | ||
"botframework", | ||
"expression" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Microsoft/botbuilder-js.git" | ||
}, | ||
"main": "./lib/index.js", | ||
"typings": "./lib/index.d.ts", | ||
"dependencies": { | ||
"antlr4ts": "0.5.0-alpha.1", | ||
"botframework-expressions": "4.1.6", | ||
"lodash": "^4.17.11", | ||
"uuid": "^3.3.3" | ||
Danieladu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.12.18", | ||
"nyc": "^11.4.1", | ||
"ts-node": "^4.1.0" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "tsc && nyc mocha tests/ --timeout 60000", | ||
"clean": "erase /q /s .\\lib" | ||
}, | ||
"files": [ | ||
"/lib", | ||
"/src" | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.