Skip to main content

How AI Builders can process AI Tasks?

note

This feature is specific for AI Agents Services using the Nevermined AI Hub.

Benefits of delegating the API to Nevermined AI Infrastructure

When a AI Builder creates an AI Agent and delegates the maintenance and execution of the HTTP API to Nevermined, they can just focus in the AI piece. This has several benefits for them:

  • Focus on the AI: The AI Builder can focus on the AI model and the AI Agent logic. Nevermined will take care of the execution of the API, authorization, etc.
  • Generic Interface: The AI Agent will be accessible via a generic interface, the Nevermined Query Protocol.
  • Availability: Nevermined will take care of having the AI Agent API available 24/7.
  • Scalability: Nevermined will take care of the scaling of the AI Agent HTTP API.

How to process AI Tasks?

The AI Builders can create simple worker AI process subscribing to Nevermined to retrieving the AI tasks sent by the users.

import asyncio

asyncio.create_task(builder.ai_protocol.subscribe(callback_function, join_account_room=True, join_agent_rooms=[], subscribe_event_types=['step-updated'], get_pending_events_on_subscribe=True))

callback_function = (step) => {
print('Step received', step)
await payments.ai_protocol.update_step(did=step['did'],
task_id=step['task_id'],
step_id=step['step_id'],
step={'step_id': step['step_id'],
'task_id': step['task_id'],
'step_status': AgentExecutionStatus.Completed.value,
'output': 'success',
'is_last': True,
'cost': 1
})
}

Using this, create an AI Agent is as simple as calling a subscribe and resolving the steps part of the tasks.

As you can see in the above code, the agent can subscribe to the events related with AI tasks created by the users and process them. For doing that it just needs to implement a callback function that will get the AI task input parameters and process it.

AI Tasks and Steps

The AI tasks are composed by several steps. Each step is a part of the task that the AI Agent needs to process. The steps are processed sequentially and the agent can update the status of the step and the output of the step. You can see the steps as a workflow that the AI Agent needs to process to be completed.

The AI Agent only needs to process the steps and update the status of the step and the output of the step. The rest of the logic is managed by Nevermined AI Hub.

When a user requests a task to an AI Agent, by default is created one task with one step. This first step has the "init" name. When the AI agent receives the init step can:

  • If the AI task is simple and doesn't require several steps, it can process the step and update the status of the step to Completed and the is_last attribute as true. This will mark the whole task as completed.

  • If the AI task requires multiple steps, the AI Agent can create more steps and setup the order of execution and configuration of them. This can be done by defining the name, order and predecessor step.

We will see different code examples about how to do this in the following section.

Sending task logs dynamically

The execution of an AI task can be long and the user can want to know the status of the task. The AI Agent can send logs to the user about the status and some other relevant output of the task. This can be done using the log_task (python) or logTask (typescript) methods.

tip

When your task finishes with a Completed or Failed status, you can send a log message witht that status. That will allow the user (or agent) to know the final status of the task in real-time.

Let's see an example:

await self.payment.ai_protocol.log_task(
TaskLog(
task_id=step['task_id'],
message='Summary ready.',
level='info',
task_status=AgentExecutionStatus.Completed.value
)
)

These logs will be send via websocket and the user who sent the task can see them in real time.