Agents and Tools
The OpenAI Responses API supports the use of tools and agents. Tools are pre-defined functions that can be invoked by the LLM or agent to access information or perform actions. Agents are automated AI entities that can independently accomplish tasks by invoking tools to achieve a goal.
The OpenAI Responses API provides cloud-side tools for web searches, file retrievals, and computer operations. The Ioto Agent framework extends the Responses API to provide an environment for local tools that can be used to augment the pre-trained knowledge of the LLM. Ioto also provides a suite of built-in tools, such as: upgrade-device, reboot-device, and signal-condition.
API Tour
Here is a simple example of an AI agentic workflow. The example demonstrates a patient monitor that "measures" a patient's body temperature and will call an ambulance if the patient needs urgent care.
The example is triggered by running the patient.html
web page. The page will ask the cloud LLM to evaluate the patient's state. The LLM will respond and request the device to get the patient's temperature. The device will respond with the temperature and the LLM will evaluate the patient's state again and reccomend treatment. If an emergency ambulance is required, the LLM will respond with a request to call an ambulance via the callEmergency() function.
#include "ioto.h"
void example(void)
{
Json *request, *response;
char *text;
request = jsonAlloc(0);
jsonSetString(request, 0, "input", "How is the patient doing?");
jsonSetString(request, 0, "instructions",
"Your are a doctor. You are given a patient temperature and you need to determine if the patient is in urgent need of medical attention. If so, call emergency response by using the callEmergency() function. In your response, state the patient's temperature in C and the result of your assessment. Do not give any other information.");
jsonSetJsonFmt(request, 0, "tools", "%s", SDEF([{
type: 'function',
name: 'getTemp',
description: 'Get the patient temperature',
parameters: { type: 'object', properties: {} },
}, {
type: 'function',
name: 'callEmergency',
description: 'Call emergency response as the patient is critically ill',
parameters: { type: 'object', properties: {} },
}]));
if ((response = openaiResponses(request, agentCallback, NULL)) == NULL) {
jsonFree(request);
return sclone("Cannot determine treatment for patient.");
}
printf("Response: %s\n", jsonGet(response, 0, "output_text", 0));
jsonFree(request);
jsonFree(response);
}
static char *agentCallback(cchar *name, Json *request, Json *response, void *arg)
{
if (smatch(name, "getTemp")) {
return getTemp();
} else if (smatch(name, "callEmergency")) {
return callEmergency();
}
return sclone("Unknown function, cannot comply with request.");
}
static char *getTemp(void)
{
static cchar *temps[] = { "36", "37", "38", "39", "40", "41", "42" };
static int index = 0;
if (index >= sizeof(temps) / sizeof(temps[0])) {
index = 0;
}
return sclone(temps[index++]);
}
static char *callEmergency(void)
{
return sclone("Ambulance dispatched");
}
The openaiResponses
API takes a JSON object which represents the Response parameters. The SDEF
macro is a convenience to make it easier to define JSON objects in C code. The SFMT macro expands printf
style expressions. The jsonParse
API parses the supplied string and returns an Ioto Json object which can be passed to the openaiResponses
API.
The agentCallback
parameter is invoked when the LLM needs to invoke a local tool. The callback is passed the tool name and the request and response JSON objects. The callback should return a string containing the results of the tool call.
The response object returned by the openaiResponses
API is a JSON object that can be queried using the Ioto JSON library jsonGet
API. The output_text
field contains the complete response output text.
Consult the Responses API for parameter details.
See the ai
app in the Ioto Agent source download for this example. The patient.html
web page initiates the demo. The src/apps/ai/aiApp.c file contains the example code and the web action to invoke the example.
References
Consult the OpenAI documentation for API details: