Wanted to share idea that’s at the heart of a new OSS library I’m building called AgentM. I say library because the last thing we need is another Agent framework. The intent of AgentM is to create a library of useful functions that I call “Micro Agents”. Using AgentM you can easily add intelligence to any application. It’s intended to be the AI equivalent of a library like Lodash.
To illustrate the idea here’s a short example that uses AgentM’s reduceList() function (Micro Agent) to sum a couple of columns in shopping cart:
import { openai, reduceList } from "agentm";
import * as dotenv from "dotenv";
// Load environment variables from .env file
dotenv.config();
// Initialize OpenAI
const apiKey = process.env.apiKey!;
const model = 'gpt-4o-2024-08-06';
const completePrompt = openai({ apiKey, model });
// Create cancellation token
const shouldContinue = () => true;
// Mock up shopping cart data
const list = [
{ description: 'graphic tee', quantity: 2, unit_price: 19.95, total: 39.90 },
{ description: 'jeans', quantity: 1, unit_price: 59.95, total: 59.95 },
{ description: 'sneakers', quantity: 1, unit_price: 79.95, total: 79.95 },
{ description: 'jacket', quantity: 1, unit_price: 99.95, total: 99.95 }
];
// Sum up the total quantity and price
const goal = `Sum the quantity and total columns.`;
const initialValue = { quantity: 0, total: 0 };
reduceList({goal, list, initialValue, completePrompt, shouldContinue }).then(result => {;
if (result.completed) {
console.log(result.value);
} else {
console.error(result.error);
}
});
The output of that call is the correct value of { quantity: 5, total: 279.75 }
. While that’s not an overly practical example (you don’t need AI for that), what it represents is an attempt to solve the task of counting using first principles that are very similar to what humans do. Humans count things by working our way through a list one item at a time and accumulating the individual values along the way. The reduceList() micro agent is counting the same way. It takes the list and evaluates each item one-by-one creating a chain-of-thought to help keep it on track. To see what this enables I have another example here which takes a list of orders as input and counts the number orders where the customer purchased a complete outfit consisting of at least a shirt and pair of pants. You could probably do that in code as well but it’s trivial to get AI to perform tasks like this. While it’s true that LLM’s generally can’t count, Micro Agents can.
When I started thinking about this last week I quickly realized that I can actually implement pretty much any standard data structure algorithm as a micro agent. I have reduce implemented and I have the prompts all fleshed out for map, filter, and sort (merge sort.) Plus all of the standard gen AI stuff like summarize and classify. The sort algorithm is interesting because you can give it a list of historical events that are in random order and it will correctly sort them to be in chronological order (even using gpt-4o-mini.) And since these are all well known algorithms they’re all computationally efficient. You could use the sort micro agent to sort a list of 10,000 items and it will take O (n log n) time but never consume more then 1k tokens per step. Plus, many of the algorithms can be run in parallel for added speed ups.
Anyway, this is very early work but thought I’d share the idea for input while I’m actively in the process of coding everything…