Skip to content

Commit 6c9b507

Browse files
committed
feat(agentkit): A3M Router AgentKit adapter package
Production-ready AgentKit adapter that routes LLM calls through A3M Router. Features: - Drop-in replacement for AgentKit LLM interface - Automatic model selection across 47+ providers - Parallel ensemble mode - Streaming support - Tool calling / function calling support Installation: npm install @a3m/agentkit-adapter Usage: import { createAgentKitAdapter } from '@a3m/agentkit-adapter'; const adapter = createAgentKitAdapter({ baseUrl: 'http://localhost:8787', model: 'auto', }); const response = await adapter.chat([ { role: 'user', content: 'Hello' } ]);
1 parent eab9f73 commit 6c9b507

9 files changed

Lines changed: 900 additions & 0 deletions

File tree

‎packages/agentkit-adapter/LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Subho Mukherjee
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# A3M Router AgentKit Adapter
2+
3+
AgentKit adapter that routes LLM calls through A3M Router for intelligent, cost-optimized model selection.
4+
5+
## Installation
6+
7+
```bash
8+
npm install @a3m/agentkit-adapter
9+
```
10+
11+
## Usage
12+
13+
```typescript
14+
import { createAgentKitAdapter } from '@a3m/agentkit-adapter';
15+
import { createAgent, run } from '@stablelib/agentkit';
16+
17+
// Create A3M-powered adapter
18+
const a3mAdapter = createAgentKitAdapter({
19+
baseUrl: 'http://localhost:8787',
20+
model: 'auto',
21+
temperature: 0.7,
22+
maxTokens: 4096,
23+
});
24+
25+
// Create agent with A3M routing
26+
const agent = createAgent({
27+
name: 'a3m-assistant',
28+
description: 'AI assistant powered by A3M Router',
29+
llm: a3mAdapter,
30+
tools: [
31+
// your tools
32+
],
33+
});
34+
35+
// Run the agent
36+
const result = await run(agent, {
37+
input: 'Hello, what is 2+2?',
38+
});
39+
```
40+
41+
## API
42+
43+
### `createAgentKitAdapter(config)`
44+
45+
Creates an A3M Router adapter for AgentKit.
46+
47+
**Config options:**
48+
49+
| Option | Type | Default | Description |
50+
|--------|------|---------|-------------|
51+
| `baseUrl` | `string` | `'http://localhost:8787'` | A3M Router server URL |
52+
| `model` | `string` | `'auto'` | Model to use (`'auto'` for intelligent routing) |
53+
| `temperature` | `number` | `0.7` | Sampling temperature |
54+
| `maxTokens` | `number` | `4096` | Max tokens to generate |
55+
| `parallelEnsemble` | `number` | `1` | Number of providers for ensemble |
56+
| `apiKey` | `string` | — | Optional API key |
57+
| `systemPrompt` | `string` | — | Optional system prompt |
58+
| `tools` | `AgentTool[]` | `[]` | Available tools |
59+
60+
**Returns:** `A3MAgentKitAdapter` instance
61+
62+
### Adapter Methods
63+
64+
#### `chat(messages, tools?)`
65+
66+
Send a chat completion request.
67+
68+
```typescript
69+
const response = await a3mAdapter.chat([
70+
{ role: 'user', content: 'What is AI?' },
71+
]);
72+
```
73+
74+
#### `stream(messages)`
75+
76+
Stream a chat completion response.
77+
78+
```typescript
79+
for await (const chunk of a3mAdapter.stream(messages)) {
80+
process.stdout.write(chunk.content);
81+
}
82+
```
83+
84+
#### `getTools()`
85+
86+
Get configured tools for function calling.
87+
88+
```typescript
89+
const tools = a3mAdapter.getTools();
90+
```
91+
92+
## How It Works
93+
94+
1. Incoming requests are forwarded to A3M Router at `baseUrl`
95+
2. A3M Router analyzes query complexity and routes to cheapest capable provider
96+
3. Response is returned with routing metadata
97+
4. Falls back gracefully if A3M Router is unavailable
98+
99+
## Example with Tools
100+
101+
```typescript
102+
import { createAgentKitAdapter } from '@a3m/agentkit-adapter';
103+
104+
const calculatorTool = {
105+
name: 'calculator',
106+
description: 'Evaluate a mathematical expression',
107+
parameters: {
108+
expression: { type: 'string', description: 'The math expression to evaluate' },
109+
},
110+
};
111+
112+
const adapter = createAgentKitAdapter({
113+
baseUrl: 'http://localhost:8787',
114+
model: 'auto',
115+
tools: [calculatorTool],
116+
});
117+
118+
const response = await adapter.chat(
119+
[{ role: 'user', content: 'What is 2+2?' }],
120+
[calculatorTool]
121+
);
122+
```
123+
124+
## License
125+
126+
MIT
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* A3M Router AgentKit Adapter - Example Usage
3+
*
4+
* Run: npx ts-node examples/agentkit-example.ts
5+
*/
6+
7+
import { createAgentKitAdapter, A3MAdapterConfig } from '../src';
8+
9+
// Example 1: Basic chat
10+
async function basicChat() {
11+
console.log('=== Example 1: Basic Chat ===');
12+
13+
const adapter = createAgentKitAdapter({
14+
baseUrl: 'http://localhost:8787',
15+
model: 'auto',
16+
temperature: 0.7,
17+
});
18+
19+
const response = await adapter.chat([
20+
{ role: 'user', content: 'What is 2+2?' },
21+
]);
22+
23+
console.log('Response:', response.content);
24+
console.log('Provider:', response.provider);
25+
console.log('Model:', response.model);
26+
console.log('Tier:', response.tier);
27+
console.log();
28+
}
29+
30+
// Example 2: Streaming
31+
async function streamingChat() {
32+
console.log('=== Example 2: Streaming ===');
33+
34+
const adapter = createAgentKitAdapter({
35+
baseUrl: 'http://localhost:8787',
36+
model: 'auto',
37+
});
38+
39+
process.stdout.write('Stream: ');
40+
for await (const chunk of adapter.stream([
41+
{ role: 'user', content: 'Count to 5' },
42+
])) {
43+
process.stdout.write(chunk.content);
44+
}
45+
console.log('\n');
46+
}
47+
48+
// Example 3: With tools
49+
async function toolChat() {
50+
console.log('=== Example 3: With Tools ===');
51+
52+
const calculatorTool = {
53+
name: 'calculator',
54+
description: 'Evaluate a mathematical expression',
55+
parameters: {
56+
type: 'object',
57+
properties: {
58+
expression: {
59+
type: 'string',
60+
description: 'The math expression to evaluate'
61+
},
62+
},
63+
required: ['expression'],
64+
},
65+
};
66+
67+
const weatherTool = {
68+
name: 'get_weather',
69+
description: 'Get current weather for a location',
70+
parameters: {
71+
type: 'object',
72+
properties: {
73+
location: {
74+
type: 'string',
75+
description: 'City name'
76+
},
77+
},
78+
required: ['location'],
79+
},
80+
};
81+
82+
const adapter = createAgentKitAdapter({
83+
baseUrl: 'http://localhost:8787',
84+
model: 'auto',
85+
temperature: 0.7,
86+
tools: [calculatorTool, weatherTool],
87+
});
88+
89+
const response = await adapter.chat(
90+
[
91+
{
92+
role: 'user',
93+
content: 'What is the weather in San Francisco and what is 50 * 23?'
94+
},
95+
],
96+
[calculatorTool, weatherTool]
97+
);
98+
99+
console.log('Response:', response.content);
100+
console.log('Provider:', response.provider);
101+
console.log('Tool Calls:', response.toolCalls);
102+
console.log();
103+
}
104+
105+
// Example 4: Parallel ensemble
106+
async function ensembleChat() {
107+
console.log('=== Example 4: Parallel Ensemble ===');
108+
109+
const adapter = createAgentKitAdapter({
110+
baseUrl: 'http://localhost:8787',
111+
model: 'auto',
112+
parallelEnsemble: 3, // Call 3 providers, pick best
113+
});
114+
115+
const response = await adapter.chat([
116+
{ role: 'user', content: 'Explain quantum entanglement in one sentence' },
117+
]);
118+
119+
console.log('Best provider:', response.provider);
120+
console.log('Response:', response.content);
121+
console.log('All candidates:', response.candidates);
122+
console.log();
123+
}
124+
125+
// Main
126+
async function main() {
127+
try {
128+
await basicChat();
129+
await streamingChat();
130+
await toolChat();
131+
await ensembleChat();
132+
console.log('All examples completed!');
133+
} catch (error) {
134+
console.error('Error:', error);
135+
console.log('\nMake sure A3M Router is running: npx a3m-router serve');
136+
}
137+
}
138+
139+
main();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "@a3m/agentkit-adapter",
3+
"version": "1.0.0",
4+
"description": "A3M Router adapter for AgentKit - enables AI agents with intelligent multi-provider routing",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"module": "dist/index.mjs",
8+
"exports": {
9+
".": {
10+
"types": "./dist/index.d.ts",
11+
"import": "./dist/index.mjs",
12+
"require": "./dist/index.js"
13+
}
14+
},
15+
"files": [
16+
"dist"
17+
],
18+
"scripts": {
19+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
20+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
21+
"test": "vitest",
22+
"lint": "eslint src/",
23+
"prepublishOnly": "npm run build"
24+
},
25+
"keywords": [
26+
"agentkit",
27+
"a3m",
28+
"router",
29+
"llm",
30+
"multi-provider",
31+
"ai",
32+
"agent",
33+
"openai",
34+
"anthropic",
35+
"routing"
36+
],
37+
"author": "",
38+
"license": "MIT",
39+
"peerDependencies": {
40+
"@inngest/agent-kit": ">=0.0.1"
41+
},
42+
"dependencies": {
43+
"@inngest/ai": ">=0.1.0",
44+
"zod": "^3.22.4"
45+
},
46+
"devDependencies": {
47+
"@inngest/agent-kit": "^0.13.0",
48+
"@types/node": "^20.10.0",
49+
"tsup": "^8.0.0",
50+
"typescript": "^5.3.0",
51+
"vitest": "^1.0.0"
52+
},
53+
"repository": {
54+
"type": "git",
55+
"url": "https://github.com/Das-rebel/adaptive-memory-multi-model-router"
56+
}
57+
}

0 commit comments

Comments
 (0)