CQRS · Event Sourcing · Generative AI · Software Architecture

Use Generative AI with Commands and Events

this article is currently a work in progress.

In this post I’m going to discuss the use of Artificial Intelligence (AI) combined with messaging patterns like Event Sourcing and CQRS.

AI components can play different roles in different scenarios like pattern recognition, business insights, future predictions and more. Currently I’m more interested in building an AI component that make decisions or provide support for decisions as briefly described below.

Decision Support

An AI listen and it is triggered by events. When a certain event happen the AI component can send a notification to a human suggesting what the next step could be. The notification should provide the user of an action to review and accept or step in and change something before proceeding. Imagine working with a solution doing contract negotiations and the AI is implemented to help one of the sides involved.

Decision Making

Like the previous plus the AI component is allowed to take the next step in the business process based on coding algorithms. A notification for the human can also be part of this and eventually offer a window of time before the AI component does the operation.

In both Support and or Decision Making, the AI generative algorithms are combined with traditional heuristic conditional coding to make the system more predictable. First thing is to understand the meaning of a input from a user and the second thing is to decide to go one way or the other. The first is done using one of the latest generative AI Api/Sdk and the second is probably done using simple code conditions.

The AI Brain component

To discuss the role played by the AI Brain component, let’s first draw the simplest user interaction with a software application.

A traditional software application where users select actions and make changes. We can think of different types of applications like negotiating contracts, connect medic and patients, IOT components sending data from sensors or an e-commerce where users make orders.

AI Brain as a Process Manager

In an event driven messaging architecture the separation between commands and events is the right ground to then build an AI Brain.

In such architecture, we have components listening for commands, processing them and publishing events. An AI Brain does the opposite. It subscribes to Events, use AI algorithms to pass through heuristic logic and send Commands. It does what usually our users do, react to events and send commands to the application.

Let me rephrase the concept. A Process Manager listen to events and send commands. It react to events, like a human does, sending commands. The other application services listen to commands and publish events. In this post, I’m referring to a Process Manager as an AI Brain.

An AI Brain is formed by two separate components in its core.

The Brain Unit is the component with the logic to support or take decisions. It listens to events and send commands. When triggered by an event, it retrieve the current state of things from the read model following the CQRS pattern. Eventual Consistency need to be considered here and measure could be taken depending on requirements to get the latest changes when needed.

The Memory is constituted by an Aggregate of the BrainUnit Type (example “Negotiating”). It listens to commands and persists events. To be more precise, every time a new command comes in

  1. Get the existing stream of past events by the aggregate ID
  2. Applies the events to build the current state of the Aggregate instance
  3. Process the command and raise one or more events

This is basic Event Sourcing pattern mechanics.

Neurons inside the Brain Unit

A Neuron is an abstraction over a specific section of your domain logic that you want to supervise and deal with by the AI Brain. For example, for contract negotiation each contract section will become separate neurons. Another example for a IOT set of sensors each type of data could be looked after separate neurons. In technical words, a Neuron is a function that receive the data and any other necessary dependency injected from the outer Brain Unit. A Brain Unit is a much larger container of a relevant section or type of your specific behaviours.