|
| 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 |
0 commit comments