Answer Question

Answers questions based on structured data from a knowledge base.

Command availability: IBM RPA SaaS and IBM RPA on premises

Script syntax

IBM RPA's proprietary script language has a syntax similar to other programming languages. The script syntax defines the command's syntax in the script file. You can work with this syntax in IBM RPA Studio's Script mode.

answerQuestion --kb(String) [--version(Numeric)] [--minimumscore(Numeric)] [--topn(Numeric)] [--options(Nullable<AnswerQuestionOptions>)] [--botHistoryMessageId(String)] --evaluate(Boolean) --culture(Culture) --text(String) (String)=answer (String)=tags (String)=context (Numeric)=score (List<String>)=options (DataTable)=data (Boolean)=success

Dependencies

You must have a knowledge base model published in your tenant. For more information about how to create knowledge base models, see Knowledge Base.

Input parameter

The following table displays the list of input parameters available in this command. In the table, you can see the parameter name when working in IBM RPA Studio's Script mode and its Designer mode equivalent label.

Designer mode label Script mode name Required Accepted variable types Description
Knowledge Base kb Required Text Knowledge base to query the answer based on the question.
Version version Optional Number Knowledge base version. If empty, it uses the production version of the knowledge base on the tenant.
Evaluate evaluate Optional Boolean Enable to replace a reference variable with the value that is assigned to it.

See the evaluate parameter options.
Context (Obsolete) context Optional Text The context of the answer to look for on the question.
The context provides patterns and themes that the Chatbot looks for, which gives more accurate answers to the user.

🛈 Note: This parameter is obsolete. Use the Context output parameter instead.
Minimum Score minimumscore Optional Number Minimum score of the answer to the question.
Answers Quantity topn Optional Number Quantity of answers that are expected to a question. Questions can have more than one answer defined on a knowledge base.
Options options Optional AnswerQuestionOptions Option to use when querying answers from a knowledge base.

See the options parameter options.
Bot Ask History botHistoryMessageId Optional Text Identifier of the chat history.
This identifier uniquely identifies a piece of communication for each Bot Ask command. It establishes and enforces a link between the data that is generated in the Bot Ask command in context and the Answer Question command to register what data relates to each piece of communication in a chatbot scope.
Language culture Required Culture Language to analyze the question.

See the culture parameter options.
Text text Required Text Question to answer.

evaluate parameter options

This parameter evaluates references to variables in answers. A reference to a variable is any text that follows the ${variableName} pattern, where the variable is replaced by a value that is defined by you on the script.

For example, if the knowledge base contains a sentence such as "To contact us send an email to ${email}, or access our site at ${website}.", both ${email} and ${website} are references to variables in the current script.

If this parameter is enabled, the user sees these variables with the values that the email and website variables contain in the current script, like contact@example.com and www.example.com.

If this parameter is disabled, you must use the Evaluate (evaluate) command to evaluate the variables, so they can receive the values from their respective variables.

Notice that when you use the Evaluate command, it replaces all references to variables to their values. Variables without values also are evaluated. For a sample usage, see the example 2.

options parameter options

The following table displays the options available for the options input parameter. The table shows the options available when working in Script mode and the equivalent label in the Designer mode.

Designer mode label Script mode name Description
Use Synonyms UseSynonyms Check the synonyms of the words in the question sent to the KB. Option available for Machine Learning algorithm version 2.

culture parameter options

The following table displays the options available for the culture input parameter. The table shows the options available when working in Script mode and the equivalent label in the Designer mode.

Designer mode label Script mode name Description
en-US en-US English (United States)
pt-BR pt-BR Portuguese (Brazil)
pt-PT pt-PT Portuguese (Portugal)

Output parameter

Designer mode label Script mode name Accepted variable types Description
Answer answer Text Returns the answer with the highest score.
Tags tags Text Returns the tags that are assigned to the answer, which are defined in the knowledge base.
Context context Text Returns the context that is assigned to the answer, which is defined in the knowledge base.
Score score Number Returns the score for the answer.
Additional Answers options List<Text> Returns a list of additional answers to the question.
Data data Data Table Returns a data table with detailed information about the question like answers, contexts, scores, and tags.
Success success Boolean Returns True if the script runs successfully, or False otherwise.

Example

Example 1: The following code example demonstrates how to get an answer to the question: "I couldn't download my payslip from the finance system. How can I solve it?"

Consider a sample knowledge base called "KB-Finance", which supports agents in a Call Center. These agents want to look for information on it to resolve customer queries. This knowledge base contains a set of answers and questions, which involves a sample finance support system.

The Answer Question command takes the question, set by the Set Var command, and analyze it based on the "KB-Finance" knowledge base model. Then, it returns the most appropriate answer to the question.

🛈 Remember: The knowledge base that is used in the following examples is a sample one, which represents a fictitious scenario. You need to create and publish a knowledge base to your tenant before you use it. For more information about how to create knowledge base models, see Knowledge Base.

defVar --name question --type String
defVar --name answer --type String
defVar --name culture --type Language
// Sets a value to the "${question}" variable.
setVar --name "${question}" --value "I couldn\'t download my payslip from the finance system. How can I solve it?"
// Answer the question, considering the "KB-Finance" knowledge base, and a minimum score of 500.
answerQuestion --kb "KB-Finance" --minimumscore 500 --culture "en-US" --text "${question}" answer=answer
// Logs the answer to the question.
logMessage --message "${answer}" --type "Info"

Example 2: The following example demonstrates how to evaluate reference to variables in answers by using the Evaluate command.

Consider that the ${email} and ${website} variables might return as part of an answer. In the example, values are assigned to them, so they can be used when needed.

The Answer Question command answers a question based on the knowledge base data. In this example, the answer to the expected question is "Sorry! You can contact us over email at ${email}, or access our site at ${website} to download your payslip."

Notice that it returns the raw reference to variables and not their values because the Evaluate input parameter is turned off. Then, after the Evaluate command, you get the answer with the values of the variables assigned to them. For example, "Sorry! You can contact us over email at contact@gmail.com, or access our site at www.example.com to download your payslip."

defVar --name question --type String
defVar --name answer --type String
defVar --name email --type String
defVar --name website --type String
// Sets a value to the "${question}" variable.
setVar --name "${question}" --value "I couldn\'t download my payslip from the finance system. How can I solve it?"
// Answer a question. It doesn't evaluate the expected variables' values in the answer because the Evaluate input parameter is turned off.
answerQuestion --kb "KB-Finance" --minimumscore 500 --culture "en-US" --text "${question}" answer=answer
// Consider that the KB returns the answer "Sorry! You can contact us over email at ${email}, or access our site at ${website} to download your payslip."
// The following command logs the previous answer before the Evaluate command.
logMessage --message "Answer before the Evaluate command: ${answer}\r\n" --type "Info"
// Sets a value to the "${email}" variable.
setVar --name "${email}" --value "contact@gmail.com"
// Sets a value to the "${website}" variable.
setVar --name "${website}" --value "www.example.com"
// Evaluate the answer to replace the reference variables to its values.
evaluate --expression "${answer}" answer=value
// The following command logs the previous answer after the Evaluate command. Now the variables' values are assigned to them.
// For example, "Sorry! You can contact us over email at contact@gmail.com, or access our site at www.example.com to download your payslip."
logMessage --message "Answer after the Evaluate command: ${answer}" --type "Info"