cancel
Showing results for 
Search instead for 
Did you mean: 

Looping through an Array

mberghoefer04
Participant
0 Kudos

Hello, once again i need help with scripting in CAI...

i have an array of objects (a list of values, organized by categories)

facts = [
{ cat:0, id:0, value:"blabla", seen:0}
, {cat:0, id:1, value:"xyz", seen:0}
, {cat:0, id:2, value:"this and that", seen:0}
, {cat:1, id:0, value:"whatever", seen:0}
, ... ]

i also have a counter that specifies which item in this array i'm currently at (=which one is supposed to be used/checked next) and a variable that defines what the current category of interest is

facts_counter = 0

facts_cat = 1

two questions:

A. how do i loop through the facts-array, starting at the item defined by facts_counter, to show the value of the next object that matches the current category defined in facts_cat?

B. and how do i set the value of "seen" to 1, for this specific item just found?

all hints&help highly appreciated!

thx+best regards

matthias

Accepted Solutions (1)

Accepted Solutions (1)

Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

This seems like the kind of thing that you should just create a webhook -- which is designed to do this sort of calculation and can easily return data to the memory. The scripting is really designed to read data, select pieces of it, and display it -- as opposed to updating data.

Given all that, you could do something like this:

  • Set all your configuration parameters (e.g., facts_counter).
  • Find all items in your array that meet your criteria, and store in variable (e.g., answer).
"{{#each memory.facts}}{{#gte @index ../memory.facts_counter}}
{{#eq ../memory.facts_cat this.cat}}{{@index}};{{/eq}}{{/gte}}{{/each}}"
  • Store the index of the first item, which is the item you are interested in.
{{{{first (split memory.answer ";")}}
  • You can display the value with something like this:
{{#with (itemAt memory.facts memory.answer2)}} {{value}} {{/with}}
  • And finally, to update the memory, my understanding for now is that you need to recreate the entire value, so you would need something like this:
[{{#eachJoin memory.facts}}{{#eq @index ../memory.answer2}} 
... here you would recreate this object manually updating seen property{{else}}{{this}}{{/eq}}{{/eachJoin}}]

Answers (3)

Answers (3)

Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

Just because it was fun and I wanted to show it is easy:

  1. I enabled Kyma.
  2. I created a function to do what you needed (code below).
  3. I exposed the function via API rule.
  4. In SAP Conversational AI, I updated the memory to include your data, then created a webhook.

The Kyma function was easy to create and update (debugging was not so good but maybe I don;t know how to set it up). Here's the code of the function:

module.exports = { 
  main: function (event, context) {
    var memory = event.data["conversation"]["memory"]
    var facts_counter =  memory["facts_counter"]
    var facts_cat = memory["facts_cat"];

    for (i = facts_counter; i < memory["facts"].length; i++) {
      if (memory["facts"][i]["cat"] == facts_cat) {
          memory["facts"][i]["seen"] = 1;
          break;
      }
    }

    var reply = {replies : [{type : "text", content : "No match"}]};

    if (i < memory["facts"].length) {
      reply["replies"][0]["content"] = memory["facts"][i]["value"];    
      reply["conversation"] = { "memory" : memory  };
    }
    return JSON.stringify(reply);
  }
}

I'm sure I could have coded it better 🙂

mberghoefer04
Participant
0 Kudos

Sure, BTP would be our choice, but so far we haven't found anyone who'd give us access to a non-trial version. (we are a no-budget, no-time, no-knowledge project 🙂

mberghoefer04
Participant
0 Kudos

Yes, thank you Daniel, in the meantime i implemented the whole thing slightly differently, also including copying over the whole structure as i simply couldn't figure out how assigning values inside a script would be done. But, as always, i learned something from your response that i didn't know before (hadn't seen the ../ syntax before - that's probably the reason while my attempts failed. a variable that outside the loop had a value, didn't exist inside the loop..)

And yes, certainly: If we did have access to a Platform where we could develop logic to be used by CAI as a Webhook or API-call, that would simplify thing enormously. Unfortunately we don't have such a Platform, so for the time being we have to do everything inside CAI.

Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

Yes, I just learned about the ../ syntax myself only recently. As for platform, what about ... BTP and simple Python webhook 🙂