Function calling in AI commands

Let the AI pick the next action, then branch your command on it.

On this page

Function calling lets a command's GPT step choose what happens next. You describe a few functions, the AI picks the one that fits the input and fills in its arguments, and the command runs the steps you set up for that function. It is how you build small agents out of custom commands.

# Declare functions in a GPT step

  1. Type / in the chat and click Create, or open a command you made and edit it.
  2. In a GPT step, under run options, click the wrench button, Functions.
  3. Write your declaration in function declaration: one JSON object, or several in an array. HARPA starts you off with a get_stock_price example.
  4. Pick the function call mode:
ModeWhat the AI does
NoneDoesn't call a function
AutoCalls a function or answers with text, as it sees fit
RequiredAlways calls the first function

gpt step functions

Each declaration needs a name and a description, and usually parameters, a JSON Schema of the arguments. The format is OpenAI's: see OpenAI's function calling guide. The line under the field says whether your JSON is valid; an invalid declaration is ignored.

{
  "name": "get_current_weather",
  "description": "Get the current weather in a city",
  "parameters": {
    "type": "object",
    "properties": {
      "location": { "type": "string", "description": "The city, e.g. Berlin" },
      "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }
    },
    "required": ["location", "unit"]
  }
}

enum limits a value to a fixed list. The description fields matter most: the AI chooses by them.

# Use the function the AI picked

The step stores the AI's choice in two parameters, named after the step's parameter (gpt by default):

ParameterHolds
{{gptFunc}}The name of the function the AI called
{{gptArgs}}Its arguments, e.g. {{gptArgs.location}}
{{gpt}}The text answer, if the AI answered instead

Rename the step's parameter to weather and they become {{weatherFunc}} and {{weatherArgs}}.

To act on the choice, add a Group step per function and give it a run condition (the condition icon on the step):

{{gptFunc}} = get_current_weather

Inside the group, add the steps that do the work: fetch a page with {{page URL}}, search with {{serp query}}, call a Request step, or send the arguments to another GPT step. More on groups and conditions in Conditions and jumps.

# Example: a Spanish tutor

The command takes any text. English gets translated into Spanish; Spanish gets a grammar check.

  1. Add an Ask step that stores your text in {{text}}.
  2. Add a GPT step with the prompt {{text}}, turn on Functions, set function call to Auto, and declare two functions:
[
  {
    "name": "translate",
    "description": "The text is in English: translate it into Spanish.",
    "parameters": {
      "type": "object",
      "properties": { "text": { "type": "string", "description": "The text I sent." } },
      "required": ["text"]
    }
  },
  {
    "name": "grammar_check",
    "description": "The text is in Spanish: check its grammar and spelling.",
    "parameters": {
      "type": "object",
      "properties": { "text": { "type": "string", "description": "The text I sent." } },
      "required": ["text"]
    }
  }
]
  1. Add two Group steps with the conditions {{gptFunc}} = translate and {{gptFunc}} = grammar_check.
  2. In each group, add a GPT step that works on {{gptArgs.text}}, for example:
Act as a Spanish teacher. Translate the text into Spanish, show the translation in a
code block, then explain the main grammar choices in short bullet points.

Text: "{{gptArgs.text}}"
Act as a Spanish teacher. Show the corrected text in a code block, then list each
mistake in English in short bullet points.

Text: "{{gptArgs.text}}"
  1. Save the command and run it with an English sentence, then with a Spanish one.

# Good to know

  • Functions work on HARPA's cloud models that support function calling and on your own API keys. Web session connections, and models without function calling, ignore them.
  • A GPT step with functions can also answer in text when the mode is Auto: check {{gptFunc}} before you use the arguments.
  • A chunked step keeps only the text answers: use functions on a step that sends one request.