Skip to content

[!WARNING] The Cognition Layer API has been replaced by FastAgentProtocol please refer to that documentation for updated docs.

Cognition Layer API

The Cognition Layer is the component of the ECM responsible for reasoning, planning, and resolving an Exelent file based on a user's query. User queries can vary widely, encompassing actions such as "write Hello World in Notepad" or "edit this image in Photoshop."

The ECM does not impose restrictions on the language or framework used to implement the cognition layer. We leverage the Agent Protocol REST API to facilitate communication with agents. In this tutorial, we will use Planex as an example agent for planning.

Planex is a 3-step planner agent utilizing LangChain to approximate the ECM problem. This approximation involves three agents focusing on Planning, Reduction, and Translation, based on the Theoretical Analysis. However, the REST API interface remains consistent across all agents.

Settling up

As Planex operates as an API-based agent, it requires starting the PlanexServer to initialize all necessary components. Building the server is a blocking call, so it can be run in a separate .py file or a different process:

import atexit
import multiprocessing
from cognition_layer.planexv2.api.server import PlanexV2Server

server = PlanexV2Server(verbose=True)
server_process = multiprocessing.Process(target=server.start)
server_process.start()

# We can manually join the proccess, but we will settle it as an atexit task for easing it.    
atexit.register(lambda p: p.join(), server_process)

Using the verbose argument prints all intermediate steps.

Running Planex

With everything set up, we can start passing queries to Planex. There are two options for this:

  • [Recommended] Using the Mediator class of the ECM, which simplifies API calls for Planex (or other Cognition Layer Agents).
  • Directly using the Agent Protocol. Planex has two handlers (task_handler and step_handler) that support all API calls from the AP, allowing free usage if preferred.

In this tutorial, we will use the Mediator class to run Planex. Note that this mediator acts as a client for the previously invoked RestAPI, so it must run within an APIClient from the Agent Protocol. First, we need to configure the API:

import agent_protocol_client
from cognition_layer.constants import API_ADDRESS
from cognition_layer.constants import API_PORT

host = f"http://{API_ADDRESS}:{API_PORT}"
configuration = agent_protocol_client.Configuration(host=host)

In this case, we use the default address and port of the ECM (0.0.0.0:8090), but feel free to change them as needed. Ensure the new address is updated in the ECM constants so the server adapts accordingly.

With the configuration set, we can query a task using the mediator. The mediator is an async function and should be called within an async definition.

from cognition_layer.api import CognitionMediator
import asyncio

async def main():
    async with agent_protocol_client.ApiClient(configuration) as api_client:

        mediator = CognitionMediator(api_client)
        while True:

            query = input("Request a Task: ")
            task = await mediator.get_task(input=query)
            result = await mediator.run_task(task)
            print ("Result: ", result)

asyncio.run(main())