對話方塊程式庫Dialogs library

適用于: SDK v4APPLIES TO: SDK v4

對話 是 SDK 中的核心概念,可讓您管理與使用者長時間執行的對話。Dialogs are a central concept in the SDK, providing ways to manage a long-running conversation with the user. 對話方塊會執行可代表一部分或完整對話式執行緒的工作。A dialog performs a task that can represent part of or a complete conversational thread. 它可以只跨越一個回合或許多,而且可以橫跨短時間或很長的一段時間。It can span just one turn or many, and can span a short or long period of time.

本文說明對話方塊程式庫的核心類別和功能。This article describes the core classes and features of the dialog library. 您應該熟悉 bot 的 運作方式 (包括 回合) 和 管理狀態You should be familiar with how bots work (including what a turn is) and managing state.

每個對話方塊都代表可執行到完成的對話工作,並傳回所收集的資訊。Each dialog represents a conversational task that can run to completion and return collected information. 每個對話方塊都代表控制流程的基本單位:它可以開始、繼續和結束;暫停和繼續;或將其取消。Each dialog represents a basic unit of control flow: it can begin, continue, and end; pause and resume; or be canceled.

對話方塊類似于程式設計語言中的方法或函數。Dialogs are similar to a method or function in a programming language. 當您啟動對話方塊時,可以傳入引數或參數,而對話方塊稍後會在結束時產生傳回值。You can pass in arguments or parameters when your start a dialog, and the dialog can later produce a return value when it ends.

對話方塊狀態Dialog state

對話是一種執行 _多回合對話_的方法,因此它們會依賴跨回合的 持續性狀態Dialogs are a way of implementing a multi-turn conversation, and as such, they rely on persisted state across turns. 在對話方塊中沒有狀態時,您的 bot 不會知道它在對話中的位置,或是已收集的資訊。Without state in dialogs, your bot wouldn't know where it was in the conversation or what information it had already gathered.

若要在對話中保留對話的位置,對話的狀態必須從每個回合中取出並儲存至記憶體。To retain a dialog's place in the conversation, the dialog's state must be retrieved from and saved to memory each turn. 這是透過 bot 交談狀態上定義的對話狀態屬性存取子來處理。This is handled via a dialog state property accessor defined on the bot's conversation state. 此對話狀態會收集所有使用中對話的資訊,以及作用中對話方塊的子系。This dialog state collects information for all active dialogs, and children of active dialogs. 這可讓 bot 從最後的位置開始,並處理各種不同的交談模型。This allows the bot to pick up where it left off last and to handle a variety of conversation models.

在執行時間,對話方塊狀態屬性會包含對話方塊在其邏輯進程中的資訊,包括任何內部收集的資訊(以 對話實例 物件的形式)。At run time, the dialog state property includes information on where the dialog is in its logical process, including any internally collected information in the form of a dialog instance object. 同樣地,這需要讀入 bot,並將每個回合儲存至記憶體。Again, this needs to be read into the bot and saved out to memory each turn.

對話方塊基礎結構Dialog infrastructure

除了各種對話方塊類型之外,下列類別也牽涉到設計和控制交談。Along with various types of dialogs, the following classes are involved in the design and control of conversations. 雖然您通常不需要直接與這些類別互動,但是在設計 bot 的對話時,也很有其用途。While you don't usually need to interact with these classes directly, being aware of them and their purpose is useful when designing dialogs for a bot.

類別Class 描述Description
對話方塊集Dialog set 定義可以彼此參考並協同合作的對話集合。Defines a collection of dialogs that can reference each other and work in concert.
對話內容Dialog context 包含所有使用中對話的相關資訊。Contains information about all active dialogs.
對話方塊實例Dialog instance 包含一個作用中對話的相關資訊。Contains information about one active dialog.
對話方塊回合結果Dialog turn result 包含作用中或最近使用中之對話方塊的狀態資訊。Contains status information from an active, or recently active, dialog. 如果使用中的對話方塊結束,則會包含其傳回值。If the active dialog ended, this contains its return value.

注意

在推出適應性對話方塊時,4.9 版的 c # SDK 引進了一個 對話方塊管理員 類別,可為您自動化許多對話方塊管理工作。As part of the introduction of adaptive dialogs, version 4.9 of the C# SDK introduced a dialog manager class that automates many of the dialog management tasks for you. (調適型對話方塊在 JavaScript SDK 4.10 版中處於預覽狀態 ) 。如需詳細資訊,請參閱 自我調整對話方塊簡介(Adaptive dialogs are in preview in version 4.10 of the JavaScript SDK.) For more information, see the introduction to adaptive dialogs.

對話類型Dialog types

對話方塊程式庫提供幾種類型的對話,讓您的 bot 對話更容易管理。The dialogs library provides a few types of dialogs to make your bot's conversations easier to manage.

類型Type 描述Description
對話 框dialog 所有對話方塊的基類。The base class for all dialogs.
容器對話方塊container dialog 所有 容器 對話方塊的基類,例如元件和調適型對話方塊。The base class for all container dialogs, such as component and adaptive dialogs. 它會維護內部的對話集,並可讓您將對話的集合視為一個單位。It maintains an inner dialog set and allows you to treat a collection of dialogs as a unit.
元件對話方塊component dialog 封裝一組對話的一般用途容器對話類型,可讓您重複使用整個集合。A general-purpose type of container dialog that encapsulates a set of dialogs, allowing for the reuse of the set as a whole. 當元件對話開始時,它會在其集合中以指定的對話開始。When a component dialog starts, it begins with a designated dialog in its collection. 當內部進程完成時,元件對話方塊會結束。When the inner process completes, the component dialog ends.
瀑布式對話方塊waterfall dialog 定義一連串的步驟,讓您的 bot 可以引導使用者完成線性程式。Defines a sequence of steps, allowing your bot to guide a user through a linear process. 這些通常是設計來在元件對話的內容中運作。These are typically designed to work within the context of a component dialog.
提示對話prompt dialogs 要求使用者輸入,並傳回結果。Ask the user for input and return the result. 提示會重複,直到它取得有效的輸入或取消為止。A prompt will repeat until it gets valid input or it is canceled. 它們是設計來使用瀑布式對話。They are designed to work with waterfall dialogs.
適應性對話方塊adaptive dialog 允許彈性對話流程的容器對話方塊類型。A type of container dialog that allows for flexible conversation flow. 它包含語言辨識、語言產生和記憶體範圍功能的內建支援。It includes built-in support for language recognition, language generation, and memory scoping features. 若要執行調適型對話 (或另一個包含調適型對話的對話),您必須從對話管理員啟動對話。To run an adaptive dialog (or another dialog that contains an adaptive dialog), you must start the dialog from a dialog manager.
動作對話方塊action dialogs 代表程式設計結構。Represent programmatic structures. 這些可讓您使用傳統程式設計語言的運算式和語句來設計對話流程,讓您在應用程式中設計程式流程。These let you design conversation flow much like expressions and statements in a traditional programming language let you design procedural flow in an application. 這些只適用于調適型對話方塊中。These only work within an adaptive dialog.
輸入對話方塊input dialogs 要求使用者輸入,與提示對話很類似,但可以與調適型對話方塊中的其他結構互動。Ask the user for input, much like prompt dialogs, but can interact with other structures in an adaptive dialog. 這些只適用于調適型對話方塊中。These only work within an adaptive dialog.
技能對話方塊skill dialog 從技能取用者自動管理一或多個技能 bot。Automates the management of one or more skill bots from a skill consumer.
QnA Maker 對話方塊QnA Maker dialog 自動存取 QnA Maker 知識庫。Automates access to a QnA Maker knowledge base.

對話方塊模式Dialog patterns

有兩種主要模式可從 bot 啟動及管理對話。There are two main patterns for starting and managing dialogs from a bot.

  1. 針對調適型對話方塊或任何包含調適型對話方塊的對話集,您需要為根對話方塊建立對話方塊管理員的實例,並讓您的對話狀態 (,並選擇性地讓您的使用者狀態) 可供管理員使用。For adaptive dialogs, or any set of dialogs that contains an adaptive dialog, you need to create an instance of the dialog manager for your root dialog, and make your conversation state (and optionally your user state) available to the manager. 如需詳細資訊,請參閱調適型 對話方塊的簡介 ,以及如何 使用適應性對話方塊建立 botFor more information, see the introduction to adaptive dialogs and how to create a bot using adaptive dialogs.
  2. 針對其他對話方塊,您可以使用 [對話方塊管理員],或只使用根對話方塊的 [ 執行 擴充方法]。For other dialogs, you can use the dialog manager or just use the root dialog's run extension method. 如需搭配使用 run 方法與元件對話方塊的詳細資訊,請參閱 關於元件和瀑布式對話 ,以及如何 實行順序對話流程For information on using the run method with a component dialog, see about component and waterfall dialogs and how to implement sequential conversation flow.

對話方塊堆疊The dialog stack

對話內容包含所有使用中對話的相關資訊,並包含 對話堆疊,可作為所有使用中對話方塊的 呼叫堆疊A dialog context contains information about all active dialogs and includes a dialog stack, which acts as a call stack for all the active dialogs. 每個容器對話方塊都有一組內部的對話,因此每個使用中的容器對話方塊都會在其狀態中引進內部對話內容和對話堆疊。Each container dialog has an inner set of dialogs that it is controlling, and so each active container dialog introduces an inner dialog context and dialog stack as part of its state.

雖然您不會直接存取堆疊,但請瞭解它是否存在,且其功能將有助於您瞭解對話方塊程式庫的各個層面如何運作。While you will not access the stack directly, understanding that it exists and its function will help you understand how various aspects of the dialogs library work.

容器對話Container dialogs

容器對話方塊A container dialog 可以是較大的對話集的一部分。can be part of a larger dialog set. 每個容器都有一個同時受管理的內部對話集。Each container has an inner dialog set that is also managed.

  • 每個對話方塊集會建立一個範圍來解析對話識別碼。Each dialog set creates a scope for resolving dialog IDs.
  • SDK 目前會執行兩種類型的容器對話:元件對話方塊和調適型對話方塊。The SDK currently implements two types of container dialogs: component dialogs and adaptive dialogs. 雖然這兩個的概念結構相當不同,但它們可以搭配使用。While the conceptual structure of the two are quite different, they can be used together.

對話識別碼Dialog IDs

當您將對話新增至對話方塊集合時,會在該集合中為它指派唯一的識別碼。When you add a dialog to a dialog set, you assign it an unique ID within that set. 集合內的對話會依其識別碼彼此參考。Dialogs within a set reference each other by their IDs.

當某個對話方塊在執行時間參考另一個對話方塊時,它會透過對話方塊的識別碼來執行此動作。When one dialog references another dialog at run time, it does so by the dialog's ID. 對話方塊內容會嘗試根據 [立即] 對話方塊集中的其他對話方塊來解析識別碼。The dialog context tries to resolve the ID based on the other dialogs in the immediate dialog set. 如果沒有相符的結果,它會在包含或外部對話方塊集合中尋找相符的結果,依此類推。If there is no match, it looks for a match in the containing or outer dialog set, and so on. 如果找不到相符項,則會產生例外狀況或錯誤。If no match is found, an exception or error is generated.

元件對話Component dialogs

元件對話會針對交談使用時序模型,而容器中的每個對話方塊會負責呼叫容器中的其他對話。Component dialogs use a sequence model for conversations, and each dialog in the container is responsible for calling other dialogs in the container. 當元件對話方塊的內部對話堆疊是空的時,元件就會結束。When the component dialog's inner dialog stack is empty, the component ends.

如果您的 bot 具有相當簡單的控制流程,而不需要更多動態對話流程,請考慮使用元件和瀑布式對話。Consider using component and waterfall dialogs if your bot has a relatively simple control flow that does not require more dynamic conversation flow.

關於元件和瀑布式對話 會更詳細地描述元件、瀑布圖和提示對話方塊。About component and waterfall dialogs describes component, waterfall, and prompt dialogs in more detail.

自適性對話方塊Adaptive dialogs

調適型對話方塊使用彈性模型來進行交談,以處理更廣泛的使用者互動。Adaptive dialogs use a flexible model for conversations to handle a wider range of user interaction. 適應性對話方塊可設計為在其內部對話堆疊為空白時,結束或保持使用中狀態。An adaptive dialog can be designed to end or remain active when its inner dialog stack is empty. 它們提供數個內建功能,包括中斷處理、語言辨識、語言產生等等。They offer several built-in capabilities, including interruption handling, language recognition, language generation, and more. 使用調適型對話,您可更專注於建立交談模型,而比較少注意對話機制。With adaptive dialogs, you can focus more on modeling the conversation and less on dialog mechanics.

調適型對話是 Dialogs 程式庫的一部分,可搭配其他所有對話類型運作。An adaptive dialog is part of the dialogs library and works with all of the other dialog types. 您可以輕鬆地建置可使用許多對話類型的 Bot。You can easily build a bot that uses many dialog types.

如果您的 Bot 情況如下,請考慮使用調適型對話:Consider using adaptive dialogs if your bot:

  • 要求交談流程根據商務邏輯、使用者輸入或其他因素處理分支或執行迴圈。Requires the conversation flow to branch or loop based on business logic, user input, or other factors.
  • 需要隨著內容調整、以任意順序接受資訊,或允許一個交談執行緒暫停以進行單邊交談,然後再次拾回交談。Needs to adjust to context, accept information in an arbitrary order, or allow one conversational thread to pause for a side-conversation and then pick back up again.
  • 需要內容專屬語言理解模型,或需要從使用者輸入擷取實體資訊。Needs context-specific language understanding models or needs to extract entity information from user input.
  • 會受益於自訂輸入處理或回應產生。Would benefit from custom input processing or response generation.

調適型對話方塊和其他調適型對話方塊主題的 簡介 會說明調適型對話方塊所支援的功能:語言辨識和語言產生支援、使用觸發程式和動作來模型交談流程、記憶體範圍的存取等等。The introduction to adaptive dialogs and the other adaptive dialog topics describe the features supported by adaptive dialogs: language recognition and language generation support, use of triggers and actions to model conversation flow, access to memory scopes, and so on. 瞭解如何使用調適型對話方塊來 建立 bot ,以深入瞭解如何使用對話方塊管理員來執行調適型對話方塊。See how to create a bot using adaptive dialogs for more about using a dialog manager to run an adaptive dialog.

其他對話方塊Other dialogs

QnA Maker 和技能對話方塊可作為獨立的對話使用,或作為容器中對話方塊集合的一部分。The QnA Maker and skill dialogs can be used as stand-alone dialogs or as part of a collection of dialogs in a container.

QnA Maker 對話方塊QnA Maker dialog

QnA Maker 對話方塊會存取 QnA Maker 知識庫,並支援 QnA Maker 的後續提示和主動式學習功能。The QnA Maker dialog accesses a QnA Maker knowledge base and supports QnA Maker's follow-up prompt and active learning features.

  • 後續的提示(也稱為多重回合提示)可讓知識庫先要求使用者提供詳細資訊,再回答問題。Follow-up prompts, also known as multi-turn prompts, allow a knowledge base to ask the user for more information before answering their question.
  • 主動式學習建議可讓知識庫隨著時間改進。Active learning suggestions allow the knowledge base to improve over time. QnA Maker 對話支援主動式學習功能的明確意見反應。The QnA Maker dialog supports explicit feedback for the active learning feature.

如需 QnA Maker 的詳細資訊,請參閱如何 使用 QnA Maker 回答問題For information about QnA Maker, see how to use QnA Maker to answer questions.

技能對話方塊Skill dialog

技能對話方塊會存取和管理一或多項技能。A skill dialog accesses and manages one or more skills. 技能對話會將父 Bot 的活動張貼至技能 Bot,並將技能回應傳回給使用者。The skill dialog posts activities from the parent bot to the skill bot and returns the skill responses to the user.

如需技能的相關資訊,請參閱 技能總覽For information about skills, see the skills overview.

後續步驟Next steps