HARPA.AI
LIBRARYUSE CASESGUIDESAI COMMANDSBLOG

Flow Control Steps

CONTENTS

icon

Conditions, GROUP, LOOP and JUMP steps are advanced features, used to control the flow of your AI commands.

# GROUP

GROUP step is used to combine multiple steps together. For example, if you have an ASK step with multiple options, you can create a group per option and add distinct run conditions.

To create a group of steps, click the ADD STEP button and select GROUP.

Grouping steps into a single item can be useful for:

  • Simplifying the creation of complex commands consisting of multiple steps.
  • Crafting iterative commands (using a LOOP step) or procedures that can be referred to using a JUMP step.

You can assign a label to each group of steps, making it easier to navigate within your command:

ChatML declaration

Group is used to combine multiple steps together. For example, if you have a prompt with multiple action options, each one can be a group:

- condition: '{{change}} = refine'
  type: group
  steps:
    - type: ask
      param: instructions
      message: >-
        What changes would you like me to make?
    - type: gpt
      prompt: >-
        Please rewrite your answer, taking into account my
        additional instructions. Provide a rewritten response.

        [Additional instructions]: {{instructions}}

        Rewritten response:

Grouping steps helps simplify your code and avoid errors, making it easier and more convenient when crafting complex commands.

The STOP step allows you to interrupt the current group, as demonstrated in the following example:

- condition: '{{change}} = refine'
  type: group
  steps:
    - type: ask
      param: instructions
      message: >-
        What changes would you like me to make?
    - type: gpt
      prompt: >-
        Please rewrite your answer, taking into account my
        additional instructions. Provide a rewritten response.

        [Additional instructions]: {{instructions}}

        Rewritten response:
    - type: stop

# JUMP

Jump will send execution to the specified step. The target step is resolved by the jump label. Here is an example:

# full version
- type: jump
  to: label-a

# short version
- jump: label-a

Jump action can be used to bypass multiple steps or create loops in dialogues. Whenever a jump step is invoked, HARPA engine resolves step by the given label and runs it next. Here is an example of a label declaration:

- say: Hello world!
  label: infinite
- jump: infinite

Be cautious with JUMP steps, as they may create infinite loops, such as the one above.

JUMPs are often utilized with conditions. You can learn more about the JUMP step and conditions here.

# LOOP

LOOPs are similar to GROUPs.

LOOPs are used to iterate over parameters and allow building mass-automation agents, monitor or extract data from multiple pages, process or enrich contacts. LOOPs are limited to OpenAI API key connection to adhere to OpenAI’s Terms of Service.

LOOP steps operate over list parameters (arrays). By default, the parameter is named {{list}}.

Here's an example of an array with URLs, video titles, view counts, and publication times:

[{"url":"https://www.youtube.com/watch?v=B-8Is-2ocv0","title":"The 'Most Creative' Place In The World","views":470,"publishedTimeAgo":"2 weeks"},{"url":"https://www.youtube.com/watch?v=TAgLw1KKE34","title":"The Science of Storytelling","views":1221,"publishedTimeAgo":"3 months"},{"url":"https://www.youtube.com/watch?v=WRMncYn2bRg","title":"$695 Travel Credit Card: Worth the hype?","views":614,"publishedTimeAgo":"4 months"},{"url":"https://www.youtube.com/watch?v=uFuMzP9HE_E","title":"9/11 Survivor describes her escape from the 95th floor","views":2083495,"publishedTimeAgo":"5 months"},{"url":"https://www.youtube.com/watch?v=tbbo1pjA5j0","title":"I Bought A Billboard On Times Square","views":1334,"publishedTimeAgo":"6 months"},{"url":"https://www.youtube.com/watch?v=vrcQRq676Jg","title":"8h In United's Polaris Business Class (B777)","views":2352,"publishedTimeAgo":"7 months"}]

A single item of the array contains the following information: {"url":"https://www.youtube.com/watch?v=B-8Is-2ocv0","title":"The 'Most Creative' Place In The World","views":470,"publishedTimeAgo":"2 weeks"}

Another example of a list parameter may look like this:

[
    {"fruit": "Mango"},
    {"fruit": "Pomegranate"},
    {"fruit": "Lychee"},
    {"fruit": "Kiwi"},
    {"fruit": "Durian"},
    {"fruit": "Feijoa"},
    {"fruit": "Guava"},
    {"fruit": "Papaya"},
    {"fruit": "Carambola"},
    {"fruit": "Longan"}
]

In this case, one item of the array is a single fruit with a name: {"fruit": "Mango"}

# Setting Array Values

You can set the value of the data array through the CALC step (set parameter function): specify the parameter value and name your array (by default in the LOOP step, it is {{list}}).

If you're familiar with JavaScript, you can automatically generate an array. In fact, GPT-4 can assist you in crafting the JS code for that. For instance, obtaining the array of YouTube URLs mentioned earlier can be achieved through the following RunJS step:

return Array
  .from(document.querySelectorAll('a[href*="/watch"]'))
  .map(a => {
    const ariaLabel = a.getAttribute('aria-label') || '';
    const viewsMatch = ariaLabel.match(/(\d[\d,]*) views/);
    const timeAgoMatch = ariaLabel.match(/views (.+) ago/);
    const url = new URL(a.href);

    return {
      url: 'https://www.youtube.com' + url.pathname + '?v=' + url.searchParams.get('v'),
      title: a.getAttribute('title') || a.innerText.trim(),
      views: viewsMatch ? parseInt(viewsMatch[1].replace(/,/g, '')) : null,
      publishedTimeAgo: timeAgoMatch ? timeAgoMatch[1].trim() : null
    };
  })
  .filter(item =>
    item.url.includes('/watch?v=') &&
    item.views !== null &&
    item.publishedTimeAgo !== null);

To generate a similar JavaScript code, ask GPT: "What JS code will help me extract a link from the HTML code Elon Musk: War, AI, Aliens, Politics, Physics, Video Games, and Humanity | Lex Fridman Podcast #400 ?".

To inspect the web page HTML code, right-click on any element on page and select "Inspect.". This will highlight the element on the left side in the inspection window.

Hover over the code with your mouse cursor, click on "Edit as HTML" and the code will become available for copying.

# Setting up the LOOP step

Let's move onto creating a LOOP step. Add LOOP step to your custom command and set the list parameter value. By default, it will be {{list}}.

Then, click EDIT STEPS button and drill down into the LOOP to edit child steps, which operate on the list item.

For instance, if you add a SAY step with "🔄 LOOP: {{item}}", the result will be as follows:

You can use dot notation to access properties on items, such as: "🔄 LOOP: {{item.fruit}}".

LOOPs can be used for a variety of use-cases. For example, navigating over multiple URL:

LOOPs let you develop complex automation systems. For example, a custom command could navigate through social media posts, liking each, which will increase the traffic to your profile.

NEXT POST
Contact us
HomeUse CasesGuidesPrivacy PolicyTerms of Service