Power BI Embedded 中的问答Q&A in Power BI Embedded

Power BI Embedded 提供了一种将问答融入应用的方法,使用户能够使用自然语言提问并收到视觉对象(例如图表和图形)形式的即时答复。Power BI Embedded offers you a way to incorporate Q&A into an application and allow your users to ask questions using natural language and receive immediate answers in the form of visuals like charts or graphs.

嵌入框架中的问答交互式问题

将问答功能嵌入应用的模式有两种,即“交互式”和“仅结果”模式。There are two modes for embedding Q&A within your application: interactive and result only. 借助“交互式”模式,可以键入问题,并让它们显示在视觉对象中。Interactive mode allows you to type in questions and have them displayed within the visual. 如果有已保存的问题或要显示的已设置问题,可以在嵌入配置中填充问题,从而使用“仅结果”模式。If you have a saved question, or a set question you want to display, you can use the result only mode by populating the question in your embed config.

下面是 JavaScript 代码的示例。Here is a look at what the JavaScript code will look like.

// Embed configuration used to describe the what and how to embed.
// This object is used when calling powerbi.embed within the JavaScript API.
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
var config= {
    type: 'qna',
    tokenType:   models.TokenType.Embed | models.TokenType.Aad,
    accessToken: access token value,
    embedUrl:    https://app.powerbi.com/qnaEmbed (groupId to be appended as query parameter if required),
    datasetIds:  array of requested data set ids (at the moment we support only one dataset),
    viewMode:    models.QnAMode.Interactive | models.QnAMode.ResultOnly,
    question:    optional parameter for Explore mode (QnAMode.Interactive) and mandatory for Render Result mode (QnAMode.ResultOnly)
};

// Get a reference to the embedded QNA HTML element
var qnaContainer = $('#qnaContainer')[0];

// Embed the QNA and display it within the div container.
var qna = powerbi.embed(qnaContainer, config);

已设置问题Set question

如果将“结果模式”与已设置问题结合使用,可以将其他问题注入框架,并立即显示这些问题的解答,从而替换之前的结果。If you used result mode with a set question, you can inject additional questions into the frame and have them immediately answered replacing the previous result. 与新问题匹配的新视觉对象将呈现。A new visual is rendered matching the new question.

此用法的一个示例是常见问题列表。One example of this usage would be a frequently asked question list. 用户可以浏览这些问题并在同一个嵌入部分中进行回答。The user could go through the questions and have them answered within the same embedded part.

JS SDK 用法的代码片段:Code snippet for JS SDK usage:

// Get a reference to the embedded Q&A HTML element
var qnaContainer = $('#qnaContainer')[0];

// Get a reference to the embedded Q&A.
qna = powerbi.get(qnaContainer);

qna.setQuestion("This year sales")
    .then(function (result) {
        …….
    })
    .catch(function (errors) {
        …….
    });

视觉对象呈现的事件Visual rendered event

对于“交互式”模式,每当呈现的视觉对象发生更改以在键入更新的输入查询时针对该更新查询,则会通过数据更改事件通知应用。For interactive mode, the application can be notified with a data changed event each time the rendered visual changes to target the updated input query as it is being typed.

通过侦听 visualRendered 事件,可以保存问题,以供日后使用。Listening to the visualRendered event allows you to save questions for use later.

JS SDK 用法的代码片段:Code snippet for JS SDK usage:

// Get a reference to the embedded Q&A HTML element
var qnaContainer = $('#qnaContainer')[0];

// Get a reference to the embedded Q&A.
qna = powerbi.get(qnaContainer);

// qna.off removes a given event listener if it exists.
qna.off("visualRendered");

// qna.on will add an event listener.
qna.on("visualRendered", function(event) {
     …….
});

嵌入令牌Embed token

创建数据集的嵌入令牌以启动问答部分。Create an embed token off of a dataset to start a Q&A part. 有关更多信息,请参阅为问答生成令牌For more information, see Generate token for Q&A.

后续步骤Next steps

若要尝试问答嵌入,请查看 JavaScript 嵌入示例To give Q&A embedding a try, check out the JavaScript embed sample.

更多问题?More questions? 尝试咨询 Power BI 社区Try asking the Power BI Community