Test implementation of OpenAI Swarm for Node.js โ a multi-agent system with support for finalization, custom agents, and asynchronous tools.
npm install nodejs-swarm-openaibash
npm install nodejs-swarm-openai
`
๐ฆ Usage
`js
import { Agent, Swarm, printMessagesPretty, getFinalAnswer } from 'nodejs-swarm-openai';
const needsAnalyst = new Agent({
name: 'needs_analyst',
description: 'Understands customer needs',
instructions: 'Identify what matters most to the customer: budget, quality, speed, etc.',
model: 'gpt-4o',
tools: []
});
const positioningAgent = new Agent({
name: 'positioning',
description: 'Describes company strengths',
instructions: 'Mention experience, materials, guarantees, and successful projects.',
model: 'gpt-4o',
tools: []
});
const sellerAgent = new Agent({
name: 'seller',
description: 'Invites to action',
instructions: 'Encourage user to leave a phone number or schedule a call.',
model: 'gpt-4o',
tools: []
});
const routerAgent = new Agent({
name: 'manager',
description: 'Manages tasks and delegates to agents',
instructions:
,
model: 'gpt-4o'
});
const swarm = new Swarm(process.env.OPENAI_API_KEY, {
proxyUrl: process.env.PROXY_GPT // Optional proxy support
});
(async () => {
const { messages } = await swarm.run({
routerAgent,
agents: [needsAnalyst, positioningAgent, sellerAgent],
finalizerAgent: new Agent({ //Optional
name: 'my_finalizer',
description: 'Custom summary',
instructions: 'Merge replies into 1 final answer. Use polite tone.',
model: 'gpt-4o',
tools: []
}),
messages: [
{ role: 'user', content: 'I want a cheap and high-quality wooden sauna' }
],
maxTurns: 5,
debug: true // Optional
});
printMessagesPretty(messages);
const final = getFinalAnswer(messages);
console.log('\nFinal Answer:\n', final || 'No final message.');
})();
`
โ
Use Cases
- Multi-step sales dialogs
- Lead qualification
- Information extraction and synthesis
- Decision trees based on roles
๐ Advanced
$3
By default, the library adds a final agent to summarize all assistant replies into one. You can override it:
`js
const swarm = new Swarm(apiKey);
swarm.run({
finalizerAgent: new Agent({
name: 'my_finalizer',
description: 'Custom summary',
instructions: 'Merge replies into 1 final answer. Use polite tone.',
model: 'gpt-4o',
tools: []
}),
...
});
`
$3
`js
const result = await swarm.run({
routerAgent,
agents: [needsAnalyst, positioningAgent, sellerAgent],
finalizerAgent,
messages: [
{ role: 'user', content: 'I want a cheap and high-quality wooden sauna' }
],
maxTurns: 6,
debug: false
});
console.log(result.totalTokens)
`
`js
{
messages:[...]
tokenUsage: {
manager: 1797,
needs_analyst: 262,
positioning: 415,
seller: 579,
finalizer: 595
},
totalTokens: 3648
}
`
$3
Pass a proxy URL using proxyUrl option:
`js
const swarm = new Swarm(apiKey, {
proxyUrl: 'http://127.0.0.1:8080'
});
`
This SDK is part of Neurounit AI โ my personal platform that helps businesses leverage artificial intelligence for automated lead generation.
I develop this project in my spare time.
๐ Support
If you'd like to support this project, you can:
- Donate via TRC20: TBJWkKyrQEmG1dr8rd3psDwWY4tEvUFnzw`