@amitdeshmukh/ax-crew 3.3.4 → 3.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -4
- package/README.md +49 -13
- package/dist/agents/agentUseCosts.d.ts +18 -3
- package/dist/agents/agentUseCosts.js +78 -4
- package/dist/agents/index.d.ts +6 -2
- package/dist/agents/index.js +27 -4
- package/examples/README.md +162 -0
- package/examples/basic-researcher-writer.ts +79 -0
- package/examples/solve-math-problem.ts +74 -0
- package/examples/write-post-and-publish-to-wordpress.ts +146 -0
- package/index.d.ts +3 -3
- package/package.json +3 -2
- package/src/agents/agentUseCosts.ts +104 -7
- package/src/agents/index.ts +36 -5
- package/axllm.js +0 -68
- package/test.js +0 -41
- package/test1.js +0 -130
- package/test1.ts +0 -67
package/CHANGELOG.md
CHANGED
|
@@ -5,11 +5,23 @@ This Changelog format is based on [Keep a Changelog]
|
|
|
5
5
|
adheres to [Semantic Versioning](https://semver.org/spec/
|
|
6
6
|
v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
## [3.
|
|
8
|
+
## [3.5.2] - 2025-02-03
|
|
9
9
|
|
|
10
|
-
###
|
|
11
|
-
-
|
|
12
|
-
-
|
|
10
|
+
### Added
|
|
11
|
+
- Enhanced cost tracking with precise decimal calculations for agent and crew usage
|
|
12
|
+
- Improved token metrics aggregation across multiple agent runs
|
|
13
|
+
- Added support for per-agent and total crew cost analysis
|
|
14
|
+
|
|
15
|
+
### Fixed
|
|
16
|
+
- Added missing decimal.js dependency installation to resolve type declarations error
|
|
17
|
+
|
|
18
|
+
## [3.3.4] - 2025-02-03
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
- Fixed sub-agent function calling in StatefulAxAgent to properly handle AI configuration
|
|
22
|
+
- Improved TypeScript function overloads for the forward method to provide better type safety and readability
|
|
23
|
+
- Removed redundant AI parameter passing in sub-agent calls
|
|
24
|
+
- Added an [example](examples/write-post-and-publish-to-wordpress.ts) of using AxCrew to write a post and publish it to WordPress
|
|
13
25
|
|
|
14
26
|
## [3.3.3] - 2024-12-12
|
|
15
27
|
|
package/README.md
CHANGED
|
@@ -16,12 +16,10 @@ Install this package:
|
|
|
16
16
|
```bash
|
|
17
17
|
npm install @amitdeshmukh/ax-crew
|
|
18
18
|
```
|
|
19
|
-
AxLLM is a peer dependency, so you will need to install it separately.
|
|
20
|
-
|
|
21
|
-
**Note:** Currently, we support AxLLM v10.0.9. We are working on updating this package to support the latest version of AxLLM as it introduces some breaking changes.
|
|
19
|
+
AxLLM is a peer dependency, so you will need to install it separately.
|
|
22
20
|
|
|
23
21
|
```bash
|
|
24
|
-
npm install @ax-llm/ax@
|
|
22
|
+
npm install @ax-llm/ax@latest
|
|
25
23
|
```
|
|
26
24
|
|
|
27
25
|
### TypeScript Support
|
|
@@ -76,17 +74,22 @@ const myFunctions: FunctionRegistryType = {
|
|
|
76
74
|
// Create crew with type checking
|
|
77
75
|
const crew = new AxCrew(config, myFunctions);
|
|
78
76
|
|
|
79
|
-
//
|
|
77
|
+
// Set and get state
|
|
80
78
|
crew.state.set('key', 'value');
|
|
81
79
|
const value: string = crew.state.get('key');
|
|
82
80
|
|
|
83
|
-
//
|
|
81
|
+
// Add agents to the crew
|
|
84
82
|
const agents = crew.addAgentsToCrew(['Planner']);
|
|
85
83
|
const planner = agents?.get('Planner');
|
|
86
84
|
|
|
87
85
|
if (planner) {
|
|
88
|
-
//
|
|
86
|
+
// Agent usage with function overloads
|
|
87
|
+
// Direct usage - AI config from agent construction is used
|
|
89
88
|
const response = await planner.forward({ task: "Plan something" });
|
|
89
|
+
|
|
90
|
+
// Sub-agent usage - when used by another agent (AI is ignored and agent's own config is used)
|
|
91
|
+
const subAgentResponse = await planner.forward(ai, { task: "Plan something" });
|
|
92
|
+
|
|
90
93
|
const cost = planner.getUsageCost();
|
|
91
94
|
|
|
92
95
|
if (cost) {
|
|
@@ -313,19 +316,20 @@ console.log(`\n\nAnswer: ${answer}`);
|
|
|
313
316
|
|
|
314
317
|
### Tracking Usage Costs
|
|
315
318
|
|
|
316
|
-
|
|
319
|
+
The package provides precise cost tracking capabilities for monitoring API usage across individual agents and the entire crew. Costs are calculated using high-precision decimal arithmetic to ensure accuracy.
|
|
317
320
|
|
|
318
321
|
```javascript
|
|
319
322
|
// After running an agent's forward method
|
|
320
323
|
const response = await Planner.forward({ task: userQuery });
|
|
321
|
-
const cost = Planner.getUsageCost();
|
|
322
324
|
|
|
323
|
-
|
|
325
|
+
// Get individual agent costs
|
|
326
|
+
const agentCost = Planner.getUsageCost();
|
|
327
|
+
console.log(agentCost);
|
|
324
328
|
/* Output example:
|
|
325
329
|
{
|
|
326
|
-
promptCost: 0.
|
|
327
|
-
completionCost: 0.
|
|
328
|
-
totalCost: 0.
|
|
330
|
+
promptCost: "0.0003637500000",
|
|
331
|
+
completionCost: "0.0006100000000",
|
|
332
|
+
totalCost: "0.0009737500000",
|
|
329
333
|
tokenMetrics: {
|
|
330
334
|
promptTokens: 291,
|
|
331
335
|
completionTokens: 122,
|
|
@@ -333,8 +337,40 @@ console.log(cost);
|
|
|
333
337
|
}
|
|
334
338
|
}
|
|
335
339
|
*/
|
|
340
|
+
|
|
341
|
+
// Get aggregated costs for all agents in the crew
|
|
342
|
+
const crewCosts = crew.getAggregatedCosts();
|
|
343
|
+
console.log(crewCosts);
|
|
344
|
+
/* Output example:
|
|
345
|
+
{
|
|
346
|
+
totalCost: "0.0025482500000",
|
|
347
|
+
byAgent: {
|
|
348
|
+
"Planner": { ... },
|
|
349
|
+
"Calculator": { ... },
|
|
350
|
+
"Manager": { ... }
|
|
351
|
+
},
|
|
352
|
+
aggregatedMetrics: {
|
|
353
|
+
promptTokens: 850,
|
|
354
|
+
completionTokens: 324,
|
|
355
|
+
totalTokens: 1174,
|
|
356
|
+
promptCost: "0.0010625000000",
|
|
357
|
+
completionCost: "0.0014857500000"
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
*/
|
|
361
|
+
|
|
362
|
+
// Reset cost tracking if needed
|
|
363
|
+
crew.resetCosts();
|
|
336
364
|
```
|
|
337
365
|
|
|
366
|
+
Cost tracking features:
|
|
367
|
+
- High-precision decimal calculations using decimal.js
|
|
368
|
+
- Per-agent cost breakdown
|
|
369
|
+
- Aggregated crew-wide metrics
|
|
370
|
+
- Token usage statistics
|
|
371
|
+
- Support for different pricing tiers per model
|
|
372
|
+
- Persistent cost tracking across multiple agent runs
|
|
373
|
+
|
|
338
374
|
## Changelog
|
|
339
375
|
|
|
340
376
|
See [CHANGELOG.md](CHANGELOG.md) for a list of changes and version updates.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { StateInstance } from '../state/index.js';
|
|
1
2
|
export interface ModelUsage {
|
|
2
3
|
promptTokens: number;
|
|
3
4
|
completionTokens: number;
|
|
@@ -7,9 +8,9 @@ export interface ModelInfo {
|
|
|
7
8
|
completionTokenCostPer1M: number;
|
|
8
9
|
}
|
|
9
10
|
export interface UsageCost {
|
|
10
|
-
promptCost:
|
|
11
|
-
completionCost:
|
|
12
|
-
totalCost:
|
|
11
|
+
promptCost: string;
|
|
12
|
+
completionCost: string;
|
|
13
|
+
totalCost: string;
|
|
13
14
|
tokenMetrics: {
|
|
14
15
|
promptTokens: number;
|
|
15
16
|
completionTokens: number;
|
|
@@ -17,5 +18,19 @@ export interface UsageCost {
|
|
|
17
18
|
};
|
|
18
19
|
}
|
|
19
20
|
export declare class StateFulAxAgentUsage {
|
|
21
|
+
static STATE_KEY_PREFIX: string;
|
|
20
22
|
static calculateCost(modelUsage: ModelUsage, modelInfo: ModelInfo): UsageCost;
|
|
23
|
+
static trackCostInState(agentName: string, cost: UsageCost, state: StateInstance): void;
|
|
24
|
+
static getAggregatedCosts(state: StateInstance): {
|
|
25
|
+
totalCost: string;
|
|
26
|
+
byAgent: Record<string, UsageCost>;
|
|
27
|
+
aggregatedMetrics: {
|
|
28
|
+
promptTokens: number;
|
|
29
|
+
completionTokens: number;
|
|
30
|
+
totalTokens: number;
|
|
31
|
+
promptCost: string;
|
|
32
|
+
completionCost: string;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
static resetCosts(state: StateInstance): void;
|
|
21
36
|
}
|
|
@@ -1,13 +1,23 @@
|
|
|
1
|
+
import { Decimal } from 'decimal.js';
|
|
1
2
|
// Utility class to handle usage related functionality
|
|
2
3
|
export class StateFulAxAgentUsage {
|
|
3
4
|
static calculateCost(modelUsage, modelInfo) {
|
|
4
5
|
const { promptTokens, completionTokens } = modelUsage;
|
|
5
6
|
const { promptTokenCostPer1M, completionTokenCostPer1M } = modelInfo;
|
|
7
|
+
// Use Decimal for precise calculations
|
|
8
|
+
const promptCost = new Decimal(promptTokens)
|
|
9
|
+
.div(1000000)
|
|
10
|
+
.mul(promptTokenCostPer1M)
|
|
11
|
+
.toDP(10); // Keep 10 decimal places
|
|
12
|
+
const completionCost = new Decimal(completionTokens)
|
|
13
|
+
.div(1000000)
|
|
14
|
+
.mul(completionTokenCostPer1M)
|
|
15
|
+
.toDP(10);
|
|
16
|
+
const totalCost = promptCost.plus(completionCost).toDP(10);
|
|
6
17
|
return {
|
|
7
|
-
promptCost: (
|
|
8
|
-
completionCost: (
|
|
9
|
-
totalCost: (
|
|
10
|
-
((completionTokens / 1000000) * completionTokenCostPer1M),
|
|
18
|
+
promptCost: promptCost.toString(),
|
|
19
|
+
completionCost: completionCost.toString(),
|
|
20
|
+
totalCost: totalCost.toString(),
|
|
11
21
|
tokenMetrics: {
|
|
12
22
|
promptTokens,
|
|
13
23
|
completionTokens,
|
|
@@ -15,4 +25,68 @@ export class StateFulAxAgentUsage {
|
|
|
15
25
|
}
|
|
16
26
|
};
|
|
17
27
|
}
|
|
28
|
+
static trackCostInState(agentName, cost, state) {
|
|
29
|
+
const stateKey = `${this.STATE_KEY_PREFIX}${agentName}`;
|
|
30
|
+
const existingCost = state.get(stateKey);
|
|
31
|
+
if (existingCost) {
|
|
32
|
+
// Aggregate with existing cost using Decimal
|
|
33
|
+
const aggregatedCost = {
|
|
34
|
+
promptCost: new Decimal(existingCost.promptCost).plus(cost.promptCost).toDP(10).toString(),
|
|
35
|
+
completionCost: new Decimal(existingCost.completionCost).plus(cost.completionCost).toDP(10).toString(),
|
|
36
|
+
totalCost: new Decimal(existingCost.totalCost).plus(cost.totalCost).toDP(10).toString(),
|
|
37
|
+
tokenMetrics: {
|
|
38
|
+
promptTokens: existingCost.tokenMetrics.promptTokens + cost.tokenMetrics.promptTokens,
|
|
39
|
+
completionTokens: existingCost.tokenMetrics.completionTokens + cost.tokenMetrics.completionTokens,
|
|
40
|
+
totalTokens: existingCost.tokenMetrics.totalTokens + cost.tokenMetrics.totalTokens
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
state.set(stateKey, aggregatedCost);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
// First time tracking this agent's cost
|
|
47
|
+
state.set(stateKey, cost);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
static getAggregatedCosts(state) {
|
|
51
|
+
const allState = state.getAll();
|
|
52
|
+
const agentCosts = {};
|
|
53
|
+
let totalPromptTokens = 0;
|
|
54
|
+
let totalCompletionTokens = 0;
|
|
55
|
+
let totalPromptCost = new Decimal(0);
|
|
56
|
+
let totalCompletionCost = new Decimal(0);
|
|
57
|
+
// Aggregate costs from all agents
|
|
58
|
+
Object.entries(allState).forEach(([key, value]) => {
|
|
59
|
+
if (key.startsWith(this.STATE_KEY_PREFIX)) {
|
|
60
|
+
const agentName = key.replace(this.STATE_KEY_PREFIX, '');
|
|
61
|
+
const cost = value;
|
|
62
|
+
agentCosts[agentName] = cost;
|
|
63
|
+
totalPromptTokens += cost.tokenMetrics.promptTokens;
|
|
64
|
+
totalCompletionTokens += cost.tokenMetrics.completionTokens;
|
|
65
|
+
totalPromptCost = totalPromptCost.plus(cost.promptCost);
|
|
66
|
+
totalCompletionCost = totalCompletionCost.plus(cost.completionCost);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
// Calculate total cost from the sum of prompt and completion costs
|
|
70
|
+
const totalCost = totalPromptCost.plus(totalCompletionCost).toDP(10);
|
|
71
|
+
return {
|
|
72
|
+
totalCost: totalCost.toString(),
|
|
73
|
+
byAgent: agentCosts,
|
|
74
|
+
aggregatedMetrics: {
|
|
75
|
+
promptTokens: totalPromptTokens,
|
|
76
|
+
completionTokens: totalCompletionTokens,
|
|
77
|
+
totalTokens: totalPromptTokens + totalCompletionTokens,
|
|
78
|
+
promptCost: totalPromptCost.toDP(10).toString(),
|
|
79
|
+
completionCost: totalCompletionCost.toDP(10).toString()
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
static resetCosts(state) {
|
|
84
|
+
const allState = state.getAll();
|
|
85
|
+
Object.keys(allState).forEach(key => {
|
|
86
|
+
if (key.startsWith(this.STATE_KEY_PREFIX)) {
|
|
87
|
+
state.set(key, undefined);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
18
91
|
}
|
|
92
|
+
StateFulAxAgentUsage.STATE_KEY_PREFIX = 'agent_usage_';
|
package/dist/agents/index.d.ts
CHANGED
|
@@ -3,10 +3,11 @@ import type { AxSignature, AxAgentic, AxFunction, AxProgramForwardOptions } from
|
|
|
3
3
|
import type { AgentConfigInput } from "./agentConfig.js";
|
|
4
4
|
import { FunctionRegistryType } from "../functions/index.js";
|
|
5
5
|
import { StateInstance } from "../state/index.js";
|
|
6
|
-
import { UsageCost } from "./agentUseCosts.js";
|
|
6
|
+
import { StateFulAxAgentUsage, UsageCost } from "./agentUseCosts.js";
|
|
7
7
|
declare class StatefulAxAgent extends AxAgent<any, any> {
|
|
8
8
|
state: StateInstance;
|
|
9
9
|
axai: any;
|
|
10
|
+
private agentName;
|
|
10
11
|
constructor(ai: AxAI, options: Readonly<{
|
|
11
12
|
name: string;
|
|
12
13
|
description: string;
|
|
@@ -15,8 +16,11 @@ declare class StatefulAxAgent extends AxAgent<any, any> {
|
|
|
15
16
|
functions?: (AxFunction | (() => AxFunction))[] | undefined;
|
|
16
17
|
examples?: Array<Record<string, any>> | undefined;
|
|
17
18
|
}>, state: StateInstance);
|
|
18
|
-
forward(
|
|
19
|
+
forward(values: Record<string, any>, options?: Readonly<AxProgramForwardOptions>): Promise<Record<string, any>>;
|
|
20
|
+
forward(ai: AxAI, values: Record<string, any>, options?: Readonly<AxProgramForwardOptions>): Promise<Record<string, any>>;
|
|
19
21
|
getUsageCost(): UsageCost | null;
|
|
22
|
+
getAggregatedCosts(): ReturnType<typeof StateFulAxAgentUsage.getAggregatedCosts>;
|
|
23
|
+
resetCosts(): void;
|
|
20
24
|
}
|
|
21
25
|
/**
|
|
22
26
|
* Represents a crew of agents with shared state functionality.
|
package/dist/agents/index.js
CHANGED
|
@@ -14,14 +14,29 @@ class StatefulAxAgent extends AxAgent {
|
|
|
14
14
|
super(formattedOptions);
|
|
15
15
|
this.state = state;
|
|
16
16
|
this.axai = ai;
|
|
17
|
+
this.agentName = options.name;
|
|
17
18
|
// Set examples if provided
|
|
18
19
|
if (examples && examples.length > 0) {
|
|
19
|
-
|
|
20
|
+
this.setExamples(examples);
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
|
-
//
|
|
23
|
-
async forward(
|
|
24
|
-
|
|
23
|
+
// Implementation
|
|
24
|
+
async forward(first, second, third) {
|
|
25
|
+
// Sub-agent case (called with AI service)
|
|
26
|
+
let result;
|
|
27
|
+
if ('apiURL' in first) {
|
|
28
|
+
result = await super.forward(this.axai, second, third);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
// Direct call case
|
|
32
|
+
result = await super.forward(this.axai, first, second);
|
|
33
|
+
}
|
|
34
|
+
// Track costs in state after the call
|
|
35
|
+
const cost = this.getUsageCost();
|
|
36
|
+
if (cost) {
|
|
37
|
+
StateFulAxAgentUsage.trackCostInState(this.agentName, cost, this.state);
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
25
40
|
}
|
|
26
41
|
// Get the usage cost for a run of the agent
|
|
27
42
|
getUsageCost() {
|
|
@@ -32,6 +47,14 @@ class StatefulAxAgent extends AxAgent {
|
|
|
32
47
|
}
|
|
33
48
|
return StateFulAxAgentUsage.calculateCost(modelUsage, currentModelInfo);
|
|
34
49
|
}
|
|
50
|
+
// Get aggregated costs for all agents in the crew
|
|
51
|
+
getAggregatedCosts() {
|
|
52
|
+
return StateFulAxAgentUsage.getAggregatedCosts(this.state);
|
|
53
|
+
}
|
|
54
|
+
// Reset all cost tracking
|
|
55
|
+
resetCosts() {
|
|
56
|
+
StateFulAxAgentUsage.resetCosts(this.state);
|
|
57
|
+
}
|
|
35
58
|
}
|
|
36
59
|
/**
|
|
37
60
|
* Represents a crew of agents with shared state functionality.
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# AxCrew Examples
|
|
2
|
+
|
|
3
|
+
This directory contains example implementations showing how to use AxCrew in different scenarios. Each example demonstrates specific features and use cases.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
Before running any example, make sure you have:
|
|
8
|
+
|
|
9
|
+
1. Node.js >= 21.0.0 installed
|
|
10
|
+
2. Required API keys set in your `.env` file:
|
|
11
|
+
```env
|
|
12
|
+
ANTHROPIC_API_KEY=your_anthropic_key
|
|
13
|
+
OPENAI_API_KEY=your_openai_key
|
|
14
|
+
GEMINI_API_KEY=your_gemini_key
|
|
15
|
+
# Add other provider keys as needed
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Additional Dependencies
|
|
19
|
+
|
|
20
|
+
Some examples require additional npm modules. Install them based on which examples you want to run:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
# For WordPress example
|
|
24
|
+
npm install node-fetch https
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Examples
|
|
28
|
+
|
|
29
|
+
### 1. Basic Researcher-Writer
|
|
30
|
+
[`basic-researcher-writer.ts`](./basic-researcher-writer.ts)
|
|
31
|
+
|
|
32
|
+
Demonstrates how to create a simple crew with two agents:
|
|
33
|
+
- A researcher agent that finds information
|
|
34
|
+
- A writer agent that creates content based on the research
|
|
35
|
+
|
|
36
|
+
**Required Dependencies:** None beyond core package
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { AxCrew } from "@amitdeshmukh/ax-crew";
|
|
40
|
+
|
|
41
|
+
const crew = new AxCrew({
|
|
42
|
+
crew: [
|
|
43
|
+
{
|
|
44
|
+
name: "researcher",
|
|
45
|
+
// ... configuration
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "writer",
|
|
49
|
+
// ... configuration
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Usage example in the file
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### 2. Solve Math Problem
|
|
58
|
+
[`solve-math-problem.ts`](./solve-math-problem.ts)
|
|
59
|
+
|
|
60
|
+
Shows how to use the math-solving capabilities with step-by-step explanations:
|
|
61
|
+
- Uses Gemini Pro for code execution
|
|
62
|
+
- Demonstrates how to handle complex mathematical queries
|
|
63
|
+
- Shows usage cost tracking
|
|
64
|
+
|
|
65
|
+
**Required Dependencies:**
|
|
66
|
+
```bash
|
|
67
|
+
npm install mathjs
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
const userQuery = "who is considered as the father of the iphone and what is the 7th root of their year of birth?";
|
|
72
|
+
// See file for implementation
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. Write and Publish to WordPress
|
|
76
|
+
[`write-post-and-publish-to-wordpress.ts`](./write-post-and-publish-to-wordpress.ts)
|
|
77
|
+
|
|
78
|
+
Demonstrates how to:
|
|
79
|
+
- Create content using AI
|
|
80
|
+
- Format it for WordPress
|
|
81
|
+
- Publish directly to a WordPress site
|
|
82
|
+
- Handle API interactions
|
|
83
|
+
|
|
84
|
+
**Required Dependencies:**
|
|
85
|
+
```bash
|
|
86
|
+
npm install @wordpress/api-fetch @wordpress/url
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Additional Configuration:**
|
|
90
|
+
```env
|
|
91
|
+
WORDPRESS_URL=your_wordpress_site_url
|
|
92
|
+
WORDPRESS_USERNAME=your_username
|
|
93
|
+
WORDPRESS_APP_PASSWORD=your_application_password
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 4. AxCrew Basic Usage
|
|
97
|
+
[`axcrew.ts`](./axcrew.ts)
|
|
98
|
+
|
|
99
|
+
A basic example showing core AxCrew features:
|
|
100
|
+
- Setting up a crew
|
|
101
|
+
- Adding agents
|
|
102
|
+
- Using shared state
|
|
103
|
+
- Cost tracking
|
|
104
|
+
- Error handling
|
|
105
|
+
|
|
106
|
+
**Required Dependencies:** None beyond core package
|
|
107
|
+
|
|
108
|
+
## Running the Examples
|
|
109
|
+
|
|
110
|
+
1. Clone the repository:
|
|
111
|
+
```bash
|
|
112
|
+
git clone https://github.com/amitdeshmukh/ax-crew.git
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
2. Install dependencies:
|
|
116
|
+
```bash
|
|
117
|
+
cd ax-crew
|
|
118
|
+
npm install
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
3. Set up your environment variables in `.env`
|
|
122
|
+
|
|
123
|
+
4. Run an example:
|
|
124
|
+
```bash
|
|
125
|
+
# Using ts-node
|
|
126
|
+
npx ts-node examples/basic-researcher-writer.ts
|
|
127
|
+
|
|
128
|
+
# Or after building
|
|
129
|
+
npm run build
|
|
130
|
+
node dist/examples/basic-researcher-writer.js
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Cost Tracking
|
|
134
|
+
|
|
135
|
+
All examples include cost tracking. You can monitor API usage costs:
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
// After running an agent
|
|
139
|
+
const cost = agent.getUsageCost();
|
|
140
|
+
console.log("Usage cost:", cost);
|
|
141
|
+
|
|
142
|
+
// Get aggregated costs for all agents
|
|
143
|
+
const totalCosts = agent.getAggregatedCosts();
|
|
144
|
+
console.log("Total costs:", totalCosts);
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Best Practices
|
|
148
|
+
|
|
149
|
+
1. Always check for and handle errors appropriately
|
|
150
|
+
2. Monitor costs using the built-in cost tracking
|
|
151
|
+
3. Use appropriate model temperatures based on your use case
|
|
152
|
+
4. Leverage shared state for better agent coordination
|
|
153
|
+
5. Consider rate limits of the AI providers
|
|
154
|
+
|
|
155
|
+
## Contributing
|
|
156
|
+
|
|
157
|
+
Feel free to contribute more examples by submitting a pull request. Make sure to:
|
|
158
|
+
1. Include clear documentation
|
|
159
|
+
2. Follow the existing code style
|
|
160
|
+
3. Add appropriate error handling
|
|
161
|
+
4. Include cost tracking
|
|
162
|
+
5. Add your example to this README
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
;import { AxCrew } from "../dist/index.js";
|
|
2
|
+
|
|
3
|
+
// Example agent configuration
|
|
4
|
+
const agentConfig = {
|
|
5
|
+
crew: [
|
|
6
|
+
{
|
|
7
|
+
name: "researcher",
|
|
8
|
+
description: "A research agent that finds information",
|
|
9
|
+
signature: "query:string -> research:string",
|
|
10
|
+
provider: "anthropic",
|
|
11
|
+
providerKeyName: "ANTHROPIC_API_KEY",
|
|
12
|
+
ai: {
|
|
13
|
+
model: "claude-3-opus-20240229"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: "writer",
|
|
18
|
+
description: "A writing agent that creates content",
|
|
19
|
+
signature: "topic:string, research:string -> article:string",
|
|
20
|
+
provider: "anthropic",
|
|
21
|
+
providerKeyName: "ANTHROPIC_API_KEY",
|
|
22
|
+
ai: {
|
|
23
|
+
model: "claude-3-sonnet-20240229"
|
|
24
|
+
},
|
|
25
|
+
agents: ["researcher"] // This makes writer use researcher as a sub-agent
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// Example usage
|
|
31
|
+
async function main() {
|
|
32
|
+
// Initialize the crew
|
|
33
|
+
const crew = new AxCrew(agentConfig);
|
|
34
|
+
|
|
35
|
+
// Add agents to the crew
|
|
36
|
+
crew.addAgentsToCrew(["researcher", "writer"]);
|
|
37
|
+
|
|
38
|
+
// Get references to our agents
|
|
39
|
+
const writer = crew.agents?.get("writer");
|
|
40
|
+
const researcher = crew.agents?.get("researcher");
|
|
41
|
+
|
|
42
|
+
if (!writer || !researcher) {
|
|
43
|
+
throw new Error("Failed to initialize agents");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
// Use the researcher agent
|
|
48
|
+
const { research } = await researcher.forward({
|
|
49
|
+
query: "What are the key benefits of quantum computing?"
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Use the writer agent (which will internally use the researcher)
|
|
53
|
+
const article = await writer.forward({
|
|
54
|
+
topic: "Quantum Computing Benefits",
|
|
55
|
+
research: research
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Print the article
|
|
59
|
+
console.log("Article:", article);
|
|
60
|
+
|
|
61
|
+
// Get costs for individual agents
|
|
62
|
+
const researcherCost = researcher.getUsageCost();
|
|
63
|
+
console.log("Researcher cost:", researcherCost);
|
|
64
|
+
|
|
65
|
+
// Get aggregated costs for all agents (includes both direct calls and sub-agent calls)
|
|
66
|
+
const totalCosts = writer.getAggregatedCosts();
|
|
67
|
+
console.log("Total cost breakdown:", JSON.stringify(totalCosts, null, 2));
|
|
68
|
+
|
|
69
|
+
// If you want to start fresh with cost tracking
|
|
70
|
+
writer.resetCosts();
|
|
71
|
+
|
|
72
|
+
} finally {
|
|
73
|
+
// Clean up
|
|
74
|
+
crew.destroy();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Run the example
|
|
79
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { AxCrew } from "../dist/index.js";
|
|
2
|
+
|
|
3
|
+
// Define the crew configuration
|
|
4
|
+
const config = {
|
|
5
|
+
crew: [
|
|
6
|
+
{
|
|
7
|
+
name: "MathAgent",
|
|
8
|
+
description: "Solves math problems",
|
|
9
|
+
signature:
|
|
10
|
+
'mathProblem:string "a sentence describing a math problem to be solved using Python code" -> solution:string "the answer to the math problem"',
|
|
11
|
+
provider: "google-gemini",
|
|
12
|
+
providerKeyName: "GEMINI_API_KEY",
|
|
13
|
+
ai: {
|
|
14
|
+
model: "gemini-1.5-pro",
|
|
15
|
+
temperature: 0,
|
|
16
|
+
},
|
|
17
|
+
options: {
|
|
18
|
+
debug: false,
|
|
19
|
+
codeExecution: true,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "ManagerAgent",
|
|
24
|
+
description: "Completes a user specified task",
|
|
25
|
+
signature:
|
|
26
|
+
'question:string "a question to be answered" -> answer:string "the answer to the question"',
|
|
27
|
+
provider: "openai",
|
|
28
|
+
providerKeyName: "OPENAI_API_KEY",
|
|
29
|
+
ai: {
|
|
30
|
+
model: "gpt-4o-mini",
|
|
31
|
+
maxTokens: 1000,
|
|
32
|
+
temperature: 0,
|
|
33
|
+
},
|
|
34
|
+
options: {
|
|
35
|
+
debug: true,
|
|
36
|
+
},
|
|
37
|
+
agents: ["MathAgent"]
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Create a new instance of AxCrew with the config
|
|
43
|
+
const crew = new AxCrew(config);
|
|
44
|
+
|
|
45
|
+
// Add the agents to the crew
|
|
46
|
+
const agents = crew.addAgentsToCrew(["MathAgent", "ManagerAgent"]);
|
|
47
|
+
|
|
48
|
+
// Get agent instances
|
|
49
|
+
const managerAgent = agents?.get("ManagerAgent");
|
|
50
|
+
|
|
51
|
+
const userQuery: string =
|
|
52
|
+
"who is considered as the father of the iphone and what is the 7th root of their year of birth (precision to minimum 5 decimal places)";
|
|
53
|
+
console.log(`\n\nQuestion: ${userQuery}`);
|
|
54
|
+
|
|
55
|
+
const main = async (): Promise<void> => {
|
|
56
|
+
if (managerAgent) {
|
|
57
|
+
// Try with manager agent first
|
|
58
|
+
const managerResponse = await managerAgent.forward({
|
|
59
|
+
question: userQuery,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
console.log(
|
|
63
|
+
`\nManager's Answer: ${JSON.stringify(managerResponse.answer, null, 2)}\n`
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// Print usage costs
|
|
67
|
+
const managerCost = managerAgent.getUsageCost();
|
|
68
|
+
|
|
69
|
+
console.log("\nUsage Costs:");
|
|
70
|
+
console.log("Manager Agent:", managerCost);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
main().catch(console.error);
|