Skip to content

Latest commit

 

History

History
177 lines (128 loc) · 13.5 KB

concept-language-genereation.md

File metadata and controls

177 lines (128 loc) · 13.5 KB

Language generation

Language generation (LG) enables you to define multiple variations on a phrase, execute simple expressions based on context, and refer to conversational memory. 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 complete activity. Bot Framework Composer natively supports language generation to produce output activities using the LG templating system.

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 cards, suggested actions and attachments using the new structured response template

Language generation is achieved through:

  • markdown based .lg file format that describes the templates and their composition
  • full access to current bot's memory so you can data bind language to the state of memory
  • parser and runtime libraries that help achieve runtime resolution.

Templates

Templates are functions, which return one of the variations of the text, but fully resolve any other references to template for composition. You can define one or more text respose in template. For multiple responses, one response will be picked by random. You can also define one or more expressions so when it is a conditional template, those expressions control which particular collection of variations get picked. Templates can be parameterized meaning that different callers to the template can pass in different values for use in expansion resolution. See details in .lg file format.

Template types

Composer currently supports three different types of templates:

  • Simple template
  • Conditional template
  • Structured template (this is new and read more here)

Anatomy of a template

A template usuaslly consists of a name of the template with "#" and one of the following parts:

  • a list of one-of variation text values defined using "-"
  • a collection of conditions, each with a
  • a structure that contains
    • Structure-name
    • Properties

Below is an example of a simple .lg template with one-of variation text values.

 > this is a comment 
 # nameTemplate            
 - 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?  

Define LG template in Composer

When to define

When you want to determine how your bot should respond to users, you need to define your LG template. For example, if you want to send a welcome message to the user, you can define an LG template in the Send a response action. Click on the Send a response action node, you will see the inline LG editor where you can define the template.

send_response

What to know

To define an LG template in Composer, you will need to know

Where to define

In Composer, there are two LG editors: an inline LG editor and the Bot Responses LG editor that lists all templates. The following screenshot shows an inline LG editor which hosts the relevant templates content. Below is a screenshot of the inline LG editor where you author responses to users.

inline_editor

On the navigation pane click Bot Responses item (or the bot icon when collapsed), you will see the all-up LG editor listing all LG templates defined in the bot. Toggle Edit Mode on the upper right corner to start editing your LG template.

bot_responses

How to define

Bot Composer currently supports definition of the following three types of templates: Simple template, Conditional template and Structured tempalte. Let's go through each type of them.

Simple template

A simple template is defined to generate either a single line text response or a multi-line response.

  • To define a single-line response you will need to use a "-" before a response text or an expression with returned property value.

This is an example of a single line text response from the RespondingWithTextSample:

 - Here is a simple text message. 

This is an example of a single line expression response from the RespondingWithTextSample:

 - {user.message} 
  • To define a multi-line response you can create a name of the template starting with "#" and list the multi-line responses.

Here is an example response from the RespondingWithTextSample. In a multi-line response the bot will pick an utterance by random.

  # TextWithLG
  - Hi, this is a text with LG
  - Hey, this is a text with LG
  - Hello, this is a text with LG

Conditional template

For all conditional templates, all conditions are expressed using the Common Expression Language and condition expressions are enclosed in curly brackets. Here are two conditional template examples from the .lg file format.

IF...ELSE

  > time of day greeting reply template with conditions. 
  # timeOfDayGreeting
  IF: @{timeOfDay == 'morning'}
    - good morning
  ELSE: 
    - good evening

SWITCH...CASE

  # TestTemplate
  SWITCH: {condition}
  - CASE: {case-expression-1}
    - output1
  - CASE: {case-expression-2}
    - output2
  - DEFAULT:
   - final output

Structured template

Structured response template enables you to define a complex structure that supports all the benefits of LG (templating, composition, substitution) while leaving the interpretation of the structured response up to the caller of the LG library. It provides an easier way for users to define a full blown outgoing activity in a simple text format. Composer currently support structured LG templates such as Cards, SuggestedActions and other Chatdown style constructs.

The definition of a structured template is as follows:

# TemplateName
> this is a comment
[Structure-name
    Property1 = <plain text> .or. <plain text with template reference> .or. <expression> 
    Property2 = list of values are denoted via '|'. e.g. a | b
> this is a comment about this specific property
    Property3 = Nested structures are achieved through composition
]

Below is an example of SuggestedActions from the InterruptionSample:

- Hello, I'm the interruption demo bot! \n \[Suggestions=Get started | Reset profile]

Below is an example of Thumbnail card from the RespondingWithCardsSample:

# ThumbnailCard
[ThumbnailCard
    title = BotFramework Thumbnail Card
    subtitle = Microsoft Bot Framework
    text = Build and connect intelligent bots to interact with your users naturally wherever 
    they are, from text/sms to Skype, Slack, Office 365 mail and other popular services.
    image = https://sec.ch9.ms/ch9/7ff5/e07cfef0-aa3b-40bb-9baa-7c9ef8ff7ff5/buildreactionbotframework_960.jpg
    buttons = Get Started]

For more information on Structured template, please read the structured response template article. For more examples of structured templates, please refer to Example 1 and Example 2.

Common Expression Cheatsheet

Symbol Description
# Template definition symbol
- Variation
\ Escape character
@ A prefix character to signify need expression evaluation when in multi-line response
{} Used for all expressions. Note: templates are also functions so {templateName()} is valid and supported.
[] Short hand to refer to a template. [templateName()] is the same as {templateName()}
() Used to denote parameters to a function or to a template. E.g {templateName(‘value1’, ‘value2’)} or to a prebuilt function {length(foo)} or {length(‘value1’)}
``` Used in pair to denote multi-line segment.

References

Next