語言產生Language Generation

適用于: SDK v4APPLIES TO: SDK v4

語言生成 (LG) 可讓開發人員從程式碼和資源檔案中擷取內嵌字串,並透過 LG 執行階段和檔案格式進行管理。Language Generation (LG) allows developers to extract embedded strings from their code and resource files and manage them through a LG runtime and file format. 利用 LG,開發人員可定義片語的多種變化、執行以內容為基礎的簡單運算式,以及參考交談記憶體,進而建立更自然的交談體驗。With LG, developers can create a more natural conversation experience by defining multiple variations on a phrase, executing simple expressions based on context, and referring to conversational memory.

開發人員可使用 LG 來執行下列作業:LG can be used by developers to:

  • 讓 Bot 達成一致的特質、語調achieve a coherent personality, tone of voice for their bot
  • 區分商務邏輯與呈現separate business logic from presentation
  • 針對其 Bot 的任何回覆,包含各種變化和複雜的組合型解析include variations and sophisticated composition based resolution for any of their bot's replies
  • 建構口說與顯示調適construct speak .vs. display adaptations
  • 建構卡片、建議動作和附件construct cards, suggested actions and attachments

LG 的核心是範本擴充和實體替代。At the core of LG lies template expansion and entity substitution. 您可以為擴充提供一次性變化,以及有條件地擴充範本。You can provide one-of variation for expansion as well as conditionally expand a template. LG 的輸出可以是簡單的文字字串、多行回應或複雜的物件承載,而 LG 的上一層會用來建構活動The output from LG can be a simple text string, multi-line response, or a complex object payload that a layer above LG will use to construct an activity.

以下是簡單的問候語 LG 範本。The following is a simple greeting LG template. 請注意,所有問候語都會以變數 ${user.name} 來參考記憶體中的使用者名稱。Notice that all of the greetings reference the user's name in memory with the variable ${user.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 實際操作LG in action

開發 Bot 時,您可以各種方式來使用 LG。You can use LG in a variety of ways when developing bots. 若要開始,請建立一或多個 .lg 檔案 來涵蓋所有可能的案例,而您會在其中使用語言生成子系統搭配 Bot 對使用者的回覆。To start, create one or more .lg file(s) to cover all possible scenarios where you would use the language generation sub-system with your bot's replies to a user.

請確定您包含語言生成程式庫 Microsoft.Bot.Builder.LanguageGenerationMake sure you include the language Generation library Microsoft.Bot.Builder.LanguageGeneration. 然後藉由新增下列內容,剖析並載入 .lg 檔案中的範本:Then parse and load templates in your .lg file by adding the following:

    _templates = Templates.ParseFile(fullPath);

當您需要擴充範本時,請使用 Evaluate 並傳入相關的範本名稱。When you need template expansion, use Evaluate and pass in the relevant template name.

    var lgOutput = _templates.Evaluate(<TemplateName>);

如果您的範本需要傳遞特定屬性來進行解析/擴充,您可在呼叫 Evaluate 時進行傳遞。If your template needs specific properties to be passed for resolution/expansion, you can pass them when calling Evaluate.

    var lgOutput = _templates.Evaluate("WordGameReply", new { GameName = "MarcoPolo" } );

多語系產生和語言回復原則Multilingual generation and language fallback policy

您的 Bot 可能會以多種口說或顯示語言為目標。Your bot might target more than one spoken or display language. 您可以管理 TemplateEngine 的個別執行個體,每種目標語言有一個執行個體。You can manage separate instances of the TemplateEngine, one per target language. 如需如何將多種語言 (也稱為語言遞補) 新增至 Bot 的範例,請參閱 C#JavaScript 中具有語言遞補範例的多回合提示。For an example of how to add multiple languages, also known as language fallback, to your bot, see the multi-turn prompt with language fallback sample in C# or JavaScript.

擴充 APIExpand API

若要取得範本的所有可能擴充項目,您可以使用 ExpandTemplateTo get all possible expansions of a template, you can use ExpandTemplate.

    var results = lgTemplates.ExpandTemplate("WordGameReply", { GameName = "MarcoPolo" } )

例如,假設此 LG 內容:For example, given this LG content:

# Greeting
- Hi
- Hello

#TimeOfDay
- Morning
- Evening

# FinalGreeting
- ${Greeting()} ${TimeOfDay()}

# TimeOfDayWithCondition
- IF: ${time == 'morning'}
    - ${Greeting()} Morning
- ELSEIF: ${time == 'evening'}
    - ${Greeting()} Evening
- ELSE:
    - ${Greeting()} Afternoon

呼叫 ExpandTemplate("FinalGreeting") 會產生四種評估:The call ExpandTemplate("FinalGreeting") results in four evaluations:

  • Hi MorningHi Morning
  • Hi EveningHi Evening
  • Hello MorningHello Morning
  • Hello EveningHello Evening

限定範圍的呼叫 ExpandTemplate("TimeOfDayWithCondition", new { time = "evening" })會產生兩項擴充:The call ExpandTemplate("TimeOfDayWithCondition", new { time = "evening" }) with scope, results in two expansions:

  • Hi EveningHi Evening
  • Hello EveningHello Evening

其他資源Additional resources