Table of Contents | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
Prolog and Patterns
This section is about implementing the dialog management capabilities of the conversational agent but also about implementing the domain logic that the agent needs to be able to perform its task. As our conversational agent is a task-based agent for recipe recommendation, the conversational competence of our agent focuses on patterns and responses for recipe recommendation. The logic that we need to implement concerns the capabilities our agent needs to reason about user requests about recipes that are available in the agent’s database. We will first focus on adding some conversational capabilities for our agent to respond to a user request to provide a (random) recipe. As before, the approach to dialog management that we use here will require the definition of a pattern and the specification of a (textual) response to the user request. When we have completed that part, we will continue with the part focusing on implementing the reasoning capabilities for extracting such a recipe from the database in Prolog. This will require the definition of several rules for extracting a suitable recipe from the agent’s recipe database.
Patterns and responses
Conversational patterns represent natural patterns of interaction in a conversation (cf. Moore and Arar, 2019). A conversation essentially consists of a sequence of dialog moves that accomplish certain conversational and domain-specific aims of the conversational partners or actors. A pattern is a part of a conversation that is repeated and more often and is generalizable to other conversations. The parts that are repeated are often short. Patterns therefore are typically also short sequences of dialog moves that can be reused in various dialog contexts. The part of a dialog between the user and agent that we focus on for this capability provides an example:
...
Warning |
---|
To test the pattern you just added, as before, you still need to do one more thing: In the You can now [TBU]Run your Conversational Agent again to hear your agent ask you for your recipe preferences. Note that your team must have also added the recipe recommendation page (but not the recipe confirmation page; see Visuals section below) to enable you to respond to the agent’s inquiry. This page is needed to display a microphone icon that you will need to respond to the agent. |
Extracting a recipe from the database
We will now proceed with implementing the logic for retrieving recipes from the agent’s recipe database. All of the recipes the agent knows about can be found in the recipe_database.pl
file. This file that is provided to you at the start of the project stores about 900 recipes. To get an understanding of how this file is organized, each team member should add one of their favorite recipes to the database (your team thus should add a total of six recipes).
Tip |
---|
Each team member: add one of your favorite recipes to the end of the There are two details that you should think about:
After adding your recipe to the database, commit the file to your team's git repository. You will need to merge your changes. For more on how to do that, check out the https://socialrobotics.atlassian.net/wiki/spaces/PM2/pages/2709487788/2025+Project+Background+Knowledge#Git-Commands section on how to merge and resolve conflicts. (You should not have conflicts but just in case, that section should help you resolve those.) |
Updating the memory of the MARBEL agent when a recommendation request is made
Before we add Prolog code for retrieving recipes from the database, we will first add an action rule to the MARBEL agent for updating its conversational memory. The agent uses a memory(KeyValuePairList)
fact for recording user parameter input (the predicate is declared in the dialog.pl
file). It is used to store a list of entity key-value pairs that are updated during the conversation. We have already seen one example of such a key-value pair before the button='start'
. We now want to add another key-value pair to keep track of the recipe that has been selected of the form recipe= RecipeName
. Here, the idea is to add such a key-value pair whenever a user requests a recommendation for a recipe (when they do not want to provide more features that can be used to select a recipe, for example). When a user asks for just a recommendation, we want the agent to randomly select a recipe from all the recipes that remain after filtering all recipes using the features that have been provided already (in our case, for now, that is no feature).
...
The action rule that we just introduced sets a clear agenda for what we still need to do. If you check the Problems console in Eclipse, you should see that recipesFiltered(RecipeIDs)
has not been defined yet. We will also still need to define a predicate for retrieving all the recipe IDs from the database, and introduce a predicate for retrieving the selected recipe from the agent’s conversational memory. We will now do so, starting with the predicate for retrieving the selected recipe from the conversational memory.
Retrieving recipes
The logic for retrieving recipes from the agent’s database should be implemented in the recipe_selection.pl
file. For this capability, we will add some basic rules to retrieve the recipe that has been selected and make a start with the rules the agent needs for retrieving only those recipes that satisfy the requests a user made (their preferences, constraints).
Retrieving a recipe from conversational memory
The first rule you will be working on is a rule for defining currentRecipe(RecipeID)
. This rule will first retrieve a recipe name from the agent’s conversational memory. The idea is to look up the the recipe name in the memory (assuming it is there, otherwise the rule will simply fail) using the predefined memoryKeyValue("recipe", RecipeName)
predicate (it is defined in the dialog.pl
file, check it out). We then use recipeName(RecipeID, RecipeName)
to retrieve the RecipeID
matching the recipe’s name from the database. This matching or mapping is essential as it translates a user's choice, i.e. a selected recipe stored with its recipe name in conversational memory, into an identifier of a recipe that can subsequently be used to retrieve other recipe features from the recipe database too (these features have been indexed using recipe identifiers, not the recipe’s name!).
...
The head of the rule should be:
currentRecipe(RecipeID)
;RecipeID
is a variable that will hold the ID of the recipe stored in the conversational memory.The body of the rule consists of two consecutive queries:
memoryKeyValue('recipe', RecipeName)
; this query fetches the name of the recipe stored in memory under the key'recipe'
.recipeName(RecipeID, RecipeName)
; this query matches the recipe name fetched from memory to a recipe ID.
Fixing the response for the recommend
intent
Now that we have a way to get the recipe the agent thinks the user would like by retrieving the recipe stored in conversational memory, you can also fix the issue with our previous ad hoc solution for defining a textual response for the recommend
intent. Using the currentRecipe(ID), recipeName(ID, Name)
, look again at the rule for the textual response for the selfIdentification
intent, and introduce a similar rule that makes the agent say “What about ___” where the “___” is replaced with the recipe name.
Retrieving recipes from the recipe database
The second rule will define recipeIDs(RecipeIDs)
for collecting all available recipe IDs from the recipe database. We can implement this rule using the Prolog built-in predicate setof/3
. The idea is to use a setof(+Template, +Goal, -Set)
query with recipeID(RecipeID)
as the goal, to fetch every unique instance of RecipeID
by using that as the template. The result will be collected in RecipeIDs
by instantiating the set argument with that variable. In other words, this query will go through the database, look up every distinct RecipeID
, and then add these IDs to a list that is returned in RecipeIDs
. Now add a rule for recipeIDs(RecipeIDs)
:
...
This clause specifies that if there are no filters to apply (i.e., the filter list is empty), the output of this filtering of a list of RecipeIDs
thus remains unchanged and is that same list of recipe identifiers RecipeIDs
.
Visuals
Recipe recommendation page
The objective of this capability is to create a page that facilitates users in having a conversation about recipes with the agent. We will walk you through the detailed steps to illustrate how to implement the code for such a page below. You should add the code for a page to the html.pl
file.
At this stage, for this capability, we will only provide code for a very basic skeleton page that provides the minimal functionality needed to implement the main requirement for this page: enable a user to talk to the agent using a microphone button. The design is kept minimal below and in the step-by-step walkthrough below we will only use a single and simple alert element to style the page. As the recipe recommendation page will be the first interaction point for users who are actually interested in finding a recipe, we leave it up to you to try and make this page more inviting and add clear and useful content that the agent will show. Note that we will also ask you to extend this page at a later stage when you are implementing [TBU]Filtering by Inclusion.
Step 1: Adding the head of a Prolog rule for a recipe recommendation page.
The Prolog predicate that we use for displaying a webpage is the
page/3
predicate. We usepage(+Name, +Txt, -Html)
as the head of a rule for generating the HTML code returned in the output argumentHtml
for a page calledName
. For the name of the page that we need to specify in the first argument of the predicate, as a convention, we choose to use the pattern ID of a conversational pattern in the agenda. The idea is that the page is shown when the conversational pattern is ongoing. We want to show our recipe recommendation page when thea50recipeSelect
pattern is active. As we do not use the second argument here, you should add the following code to thehtml.pl
file (as indicated also in that file itself):page(a50recipeSelect, _, Html) :-
Step 2: Adding a condition for when to display the page.
The condition that we always will add to a rule for generating pages is a condition to only show a page when the associated pattern, in our case
a50recipeSelect
, is the currently active top-level pattern. We can use the pre-defined predicatecurrentTopLevel/1
for this (check out its definition in thedialog.pl
file). Add the following condition to the rule that we are implementing:currentTopLevel(a50recipeSelect),
Step 3: Generating the HTML code for the page.
Implement the code for generating the HTML code for the page after the condition we just added. For now, we will just add a simple https://www.w3schools.com/bootstrap4/bootstrap_alerts.asp to the page to inform the user about the recipe recommendation task the agent and the user should engage in. We use the
div/4
predicate (see the [TBU]Visual Support Guide) to add an alert class with a centered heading text. Add the following code to the rule that we are implementing:div('<center><h1>What recipe are you looking for?</h1></center>', 'alert alert-success', '', MainElementContent),
Step 4: Combining the different HTML elements (rows).
We have kept things here very simple and just added one HTML element to our page above. When you add more elements, typically you will have to combine them using a built-in Prolog predicate such as
atomic_list_concat/2
to piece everything together. This time we have nothing to do here.
Step 5: Create the main element for the HTML page.
Finally, use the
html/4
predicate to create the main element for your HTML page that will be added to the body of the HTML page. You should feed the HTML code generated above (and pieced together, if needed), into the first argument of this predicate. In our case, we should use the output argument of thediv/4
predicateMainElementContent
that we introduced above. The second is used to indicate whether you want your page to have a header (usetrue
when you do,false
if not) and the third argument is used to indicate whether you want your page to have a footer. The header is predefined in thehtml.pl
file and displays a microphone button. Finally, the generated code for the main element is returned in the fourth output argumentHtml
. Add the following code to the rule to finish implementing it:html(MainElementContent, true, true, Html).
Warning |
---|
If your team has implemented the Dialogflow intents and patterns for this capability, you can already test the page you just implemented for the So try and [TBU]Run your Conversational Agent again to see the page you just created. Also, check out the MARBEL agent’s state using the introspector. You should see that the top-level pattern that is at the front of the session is |
Recipe confirmation page
The next page that we want you to add is a recipe confirmation page. At the end of the pattern enabling a user to request a (random) recipe, the agent inserts the a50recipeConfirm
pattern ID in the agenda. No pattern has been added for this pattern ID yet, but because it will appear at the top level in the agent’s agenda, we can already create a confirmation page for the recipe that is selected. The idea is that this page allows the user to preview the recipe and indicate whether they are satisfied with the recipe (confirm) or not (disconfirm). To enable a user to make this decision, the main requirement for this page is that it shows the recipe’s name, and what the result of cooking the recipe will look like (a picture of the recipe). A second requirement is that the page shows a microphone button to enable the user to inform the agent about whether they want to (dis)confirm the recipe. Note that at a later stage, we will ask you to extend the recipe confirmation page when you implement [TBU]Filtering by Inclusion .
...
currentRecipe(-RecipeID)
: predicate for retrieving the currently selected recipe ID from the agent’s memory; you are asked to implement this predicate above in the Prolog and patterns section.recipeName(ID, Name)
: Retrieves theName
of a recipe uniquely identified byID
from the agent’s recipe databaserecipe_database.pl
.picture(ID, URL)
: Retrieves theURL
to a(n online) recipe picture for the recipe uniquely identified byID
from the agent’s databaserecipe_database.pl
.
Step 1: Adding the head of a Prolog rule for a recipe confirmation page.
We use the
page/3
predicate again to define the head of a rule for generating the recipe confirmation page HTML code. Add the following code to thehtml.pl
file (as indicated also in that file itself):page(a50recipeConfirm, _, Html) :-
Step 2: Adding a condition for when to display the page.
You should add a condition such that the recipe confirmation page is only shown when the associated pattern
a50recipeConfirm
is the currently active top-level pattern. Add the following condition to the rule that we are implementing:currentTopLevel(a50recipeConfirm),
Step 3: Generating the HTML code for the page.
Before we can even begin to create the recipe card that we have in mind, we need to retrieve the ID for that recipe. So we begin by doing that. Add the following code to the rule:
currentRecipe(ID),
Now we can begin to generate the HTML code for this recipe. We first generate the code for the recipe picture and use the
picture(ID, URL)
to retrieve the URL to the recipe’s picture to fill in the...
for the src attribute in the HTML card template above and theimg/4
predicate defined inhtml.pl
for creating an image element. Add the following code to the rule:picture(ID, URL),
img(URL, 'card-img-bottom', '', ImageElement),
Next, we generate some HTML code for a card title heading. We want the recipe name to appear as the card’s title and will retrieve that using the
recipeName(ID, Title)
predicate and use the predefined to_upper_case/2 predicate (seeutils.pl
file) to make sure the recipe title starts with a capital. To fill in the recipe title in the right place in the card title heading template, we useapplyTemplate/3
to replace the placeholder~a
in a template for a card title. Add the following code to the rule:recipeName(ID, Title), to_upper_case(Title, UpperTitle),
applyTemplate('<h4 class="card-title">~a</h4>', UpperTitle, TitleHeading),
The next part that we will generate is the card body element using the
div/4
predicate. We want our card title to be the content of that element and use that as the first argument of our div/4 predicate (the first arguments of our predefined predicates by convention are the content of an element, see also the [TBU]Visual Support Guide). Add the following code to the rule:div(TitleHeading, 'card-body', '', CardBodyElement),
Finally, we need to combine the card body and image elements (check for yourself to see that they are combined within the card element HTML code above) and create the card element. We use the
atomic_list_concat/2
predicate for combining the elements together and thediv/4
predicate again to create the card element. Add the following code to the rule:atomic_list_concat([CardBodyElement, ImageElement], CardContent),
div(CardContent, 'card', 'width:400px', MainElementContent),
Step 4: Combining the different HTML elements (rows).
We have kept things again simple here and just created one HTML element for our recipe confirmation page. We, therefore, have nothing to do here either.
Step 5: Create the main element for the HTML page.
We use the
html/4
predicate again to create the main element for your HTML page that will be added to the body of the HTML page. You should feed the HTML code generated above (and pieced together, if needed), into the first argument of this predicate. In our case, we should use the output argument of thediv/4
predicateMainElementContent
that we introduced above. As before, we include a header and footer. Add the following code to the rule to finish implementing it:html(MainElementContent, true, true, Html).
We have introduced the bare essentials for these pages but you should feel free to modify it in any way you like to make it look more appealing. However, please note that the a50recipeConfirm
page is extended later to include recipe information. More information about these extensions can be found here: https://socialrobotics.atlassian.net/wiki/spaces/PM2/pages/2229600267#Visuals.
It is important that you make sure that the requirements marked in bold in the text are met.
Test it Out
If you have not yet done this, add the a50recipeSelect
pattern ID to the agent's agenda in the dialog_init.mod2g
file. When you finished implementing this capability and you [TBU]Run your Conversational Agent again, you should be able to conduct the following conversational interaction and go through the following dialog moves:
...