llm-advanced-tools 0.1.0 β 0.1.2
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/LICENSE +21 -0
- package/README.md +10 -22
- package/dist/adapters/vercel-ai.d.ts.map +1 -1
- package/dist/adapters/vercel-ai.js +21 -12
- package/dist/adapters/vercel-ai.js.map +1 -1
- package/package.json +23 -7
- package/src/adapters/vercel-ai.ts +25 -14
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 hetpatel-11
|
|
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.
|
package/README.md
CHANGED
|
@@ -8,9 +8,10 @@ A TypeScript library that brings Anthropic's advanced tool use features to all m
|
|
|
8
8
|
Dynamically discover and load tools on-demand instead of loading everything upfront.
|
|
9
9
|
|
|
10
10
|
**Benefits:**
|
|
11
|
-
-
|
|
11
|
+
- Reduces token usage by deferring tool loading
|
|
12
12
|
- Improved accuracy with large tool sets
|
|
13
13
|
- Scale to hundreds or thousands of tools
|
|
14
|
+
- *Anthropic reports 85%+ token reduction in their testing*
|
|
14
15
|
|
|
15
16
|
### π Programmatic Tool Calling
|
|
16
17
|
Orchestrate tools through code execution rather than individual API calls.
|
|
@@ -19,26 +20,26 @@ Orchestrate tools through code execution rather than individual API calls.
|
|
|
19
20
|
- Keep intermediate results out of LLM context
|
|
20
21
|
- Parallel tool execution
|
|
21
22
|
- Better control flow with loops, conditionals, data transformations
|
|
22
|
-
- 37%+
|
|
23
|
+
- *Anthropic reports 37%+ token reduction on complex tasks in their testing*
|
|
23
24
|
|
|
24
25
|
### π Tool Use Examples
|
|
25
26
|
Provide sample invocations to improve tool call accuracy.
|
|
26
27
|
|
|
27
28
|
**Benefits:**
|
|
28
|
-
- 18%+ accuracy improvement on complex parameters
|
|
29
29
|
- Show proper usage patterns
|
|
30
30
|
- Clarify format conventions and optional parameters
|
|
31
|
+
- *Anthropic reports 18%+ accuracy improvement on complex parameters in their testing*
|
|
31
32
|
|
|
32
33
|
## Installation
|
|
33
34
|
|
|
34
35
|
```bash
|
|
35
|
-
npm install advanced-tools
|
|
36
|
+
npm install llm-advanced-tools
|
|
36
37
|
```
|
|
37
38
|
|
|
38
39
|
## Quick Start
|
|
39
40
|
|
|
40
41
|
```typescript
|
|
41
|
-
import { Client, ToolRegistry, OpenAIAdapter, ToolDefinition } from 'advanced-tools';
|
|
42
|
+
import { Client, ToolRegistry, OpenAIAdapter, ToolDefinition } from 'llm-advanced-tools';
|
|
42
43
|
|
|
43
44
|
// 1. Create a tool registry
|
|
44
45
|
const registry = new ToolRegistry({
|
|
@@ -92,7 +93,7 @@ console.log(response);
|
|
|
92
93
|
Use the latest Vercel AI SDK with any provider:
|
|
93
94
|
|
|
94
95
|
```typescript
|
|
95
|
-
import { Client, ToolRegistry, VercelAIAdapter } from 'advanced-tools';
|
|
96
|
+
import { Client, ToolRegistry, VercelAIAdapter } from 'llm-advanced-tools';
|
|
96
97
|
import { openai } from '@ai-sdk/openai';
|
|
97
98
|
import { anthropic } from '@ai-sdk/anthropic';
|
|
98
99
|
|
|
@@ -133,7 +134,7 @@ const response = await openaiClient.ask("What's the weather?");
|
|
|
133
134
|
β
|
|
134
135
|
βΌ
|
|
135
136
|
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
136
|
-
β Unified Tool Interface
|
|
137
|
+
β Unified Tool Interface β
|
|
137
138
|
β β’ ToolRegistry (search, defer loading) β
|
|
138
139
|
β β’ CodeExecutor (programmatic calling) β
|
|
139
140
|
β β’ ToolDefinition (with examples) β
|
|
@@ -141,7 +142,7 @@ const response = await openaiClient.ask("What's the weather?");
|
|
|
141
142
|
β
|
|
142
143
|
βΌ
|
|
143
144
|
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
144
|
-
β Provider Adapters
|
|
145
|
+
β Provider Adapters β
|
|
145
146
|
β ββββββββββββ¬βββββββββββ¬βββββββββββ¬βββββββββββ β
|
|
146
147
|
β β OpenAI βAnthropic β Google β Ollama β β
|
|
147
148
|
β ββββββββββββ΄βββββββββββ΄βββββββββββ΄βββββββββββ β
|
|
@@ -223,7 +224,6 @@ The LLM learns proper usage patterns from the examples.
|
|
|
223
224
|
| β³ Google | β (emulated) | β (emulated) | β (emulated) | β
via AI SDK |
|
|
224
225
|
| β³ Others | β (emulated) | β (emulated) | β (emulated) | β
via AI SDK |
|
|
225
226
|
| OpenAI (direct) | β (emulated) | β (emulated) | β (emulated) | β
Ready |
|
|
226
|
-
| Anthropic (direct) | β
| β
| β
| π§ Coming |
|
|
227
227
|
| Ollama | β (emulated) | β (emulated) | β (emulated) | π§ Coming |
|
|
228
228
|
|
|
229
229
|
## Configuration
|
|
@@ -311,17 +311,6 @@ class Client {
|
|
|
311
311
|
}
|
|
312
312
|
```
|
|
313
313
|
|
|
314
|
-
## Performance
|
|
315
|
-
|
|
316
|
-
Based on internal testing:
|
|
317
|
-
|
|
318
|
-
| Metric | Traditional | With Advanced Tools | Improvement |
|
|
319
|
-
|--------|------------|---------------------|-------------|
|
|
320
|
-
| Token usage (large tool set) | ~77K tokens | ~8.7K tokens | **88% reduction** |
|
|
321
|
-
| Token usage (complex workflows) | 43,588 tokens | 27,297 tokens | **37% reduction** |
|
|
322
|
-
| Tool selection accuracy | 79.5% | 88.1% | **+8.6 points** |
|
|
323
|
-
| Parameter accuracy | 72% | 90% | **+18 points** |
|
|
324
|
-
|
|
325
314
|
## When to Use Each Feature
|
|
326
315
|
|
|
327
316
|
### Tool Search Tool
|
|
@@ -376,7 +365,6 @@ The default VM executor is **NOT secure** for untrusted code. For production:
|
|
|
376
365
|
## Roadmap
|
|
377
366
|
|
|
378
367
|
- [x] Core library with OpenAI adapter
|
|
379
|
-
- [ ] Anthropic adapter (native features)
|
|
380
368
|
- [ ] Google/Gemini adapter
|
|
381
369
|
- [ ] Ollama adapter with model capability detection
|
|
382
370
|
- [ ] Docker-based executor
|
|
@@ -397,6 +385,6 @@ MIT
|
|
|
397
385
|
## Credits
|
|
398
386
|
|
|
399
387
|
This library implements features described in Anthropic's blog post:
|
|
400
|
-
[Introducing advanced tool use on the Claude Developer Platform](https://www.anthropic.com/
|
|
388
|
+
[Introducing advanced tool use on the Claude Developer Platform](https://www.anthropic.com/engineering/advanced-tool-use)
|
|
401
389
|
|
|
402
390
|
The implementation is provider-agnostic and works with any LLM that supports function calling.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vercel-ai.d.ts","sourceRoot":"","sources":["../../src/adapters/vercel-ai.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,aAAa,
|
|
1
|
+
{"version":3,"file":"vercel-ai.d.ts","sourceRoot":"","sources":["../../src/adapters/vercel-ai.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,aAAa,EAAQ,MAAM,IAAI,CAAC;AAEvD,OAAO,EACL,eAAe,EACf,oBAAoB,EACpB,cAAc,EACd,WAAW,EACX,YAAY,EACZ,QAAQ,EACR,UAAU,EAEX,MAAM,UAAU,CAAC;AAElB;;;GAGG;AACH,qBAAa,eAAgB,YAAW,eAAe;IACrD,QAAQ,CAAC,IAAI,eAAe;IAC5B,QAAQ,CAAC,YAAY,EAAE,oBAAoB,CAOzC;IAEF,OAAO,CAAC,KAAK,CAAgB;IAC7B,OAAO,CAAC,YAAY,CAAS;IAE7B;;;OAGG;gBACS,KAAK,EAAE,aAAa,EAAE,YAAY,SAAW;IAKzD;;OAEG;IACH,UAAU,CAAC,IAAI,EAAE,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAyBrD;;OAEG;IACH,OAAO,CAAC,eAAe;IAsBvB;;OAEG;IACH,OAAO,CAAC,eAAe;IAsDvB;;OAEG;IACG,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAwFvD;;OAEG;IACH,cAAc,CAAC,SAAS,EAAE,GAAG,EAAE,GAAG,QAAQ,EAAE;IAQ5C;;OAEG;IACH,iBAAiB,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,GAAG;IAK7C;;OAEG;IACH,OAAO,CAAC,eAAe;CAiBxB"}
|
|
@@ -135,31 +135,40 @@ class VercelAIAdapter {
|
|
|
135
135
|
// Convert schema to Zod
|
|
136
136
|
const parameters = this.jsonSchemaToZod(toolDef.inputSchema);
|
|
137
137
|
// Create the tool using AI SDK's tool() helper
|
|
138
|
-
|
|
138
|
+
// Store reference to toolDef in closure to avoid issues
|
|
139
|
+
const handler = toolDef.handler;
|
|
140
|
+
const name = toolDef.name;
|
|
141
|
+
tools[name] = {
|
|
139
142
|
description,
|
|
140
143
|
parameters,
|
|
141
144
|
execute: async (args) => {
|
|
142
145
|
try {
|
|
143
|
-
const result = await
|
|
146
|
+
const result = await handler(args);
|
|
144
147
|
return result;
|
|
145
148
|
}
|
|
146
149
|
catch (error) {
|
|
147
|
-
throw new Error(`Tool ${
|
|
150
|
+
throw new Error(`Tool ${name} failed: ${error.message}`);
|
|
148
151
|
}
|
|
149
152
|
}
|
|
150
|
-
}
|
|
153
|
+
};
|
|
151
154
|
}
|
|
152
155
|
// Generate text with tools
|
|
153
|
-
const
|
|
156
|
+
const generateOptions = {
|
|
154
157
|
model: this.model,
|
|
155
158
|
messages: messages.map(m => ({
|
|
156
159
|
role: m.role,
|
|
157
160
|
content: m.content
|
|
158
161
|
})),
|
|
159
|
-
tools: Object.keys(tools).length > 0 ? tools : undefined
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
162
|
+
tools: Object.keys(tools).length > 0 ? tools : undefined
|
|
163
|
+
};
|
|
164
|
+
// Add optional parameters if provided
|
|
165
|
+
if (request.temperature !== undefined) {
|
|
166
|
+
generateOptions.temperature = request.temperature;
|
|
167
|
+
}
|
|
168
|
+
if (request.maxTokens !== undefined) {
|
|
169
|
+
generateOptions.maxCompletionTokens = request.maxTokens;
|
|
170
|
+
}
|
|
171
|
+
const result = await (0, ai_1.generateText)(generateOptions);
|
|
163
172
|
// Parse tool calls from result
|
|
164
173
|
const toolCalls = [];
|
|
165
174
|
if (result.toolCalls && result.toolCalls.length > 0) {
|
|
@@ -179,9 +188,9 @@ class VercelAIAdapter {
|
|
|
179
188
|
},
|
|
180
189
|
usage: result.usage
|
|
181
190
|
? {
|
|
182
|
-
inputTokens: result.usage.promptTokens,
|
|
183
|
-
outputTokens: result.usage.completionTokens,
|
|
184
|
-
totalTokens: result.usage.totalTokens
|
|
191
|
+
inputTokens: result.usage.inputTokens || result.usage.promptTokens || 0,
|
|
192
|
+
outputTokens: result.usage.outputTokens || result.usage.completionTokens || 0,
|
|
193
|
+
totalTokens: result.usage.totalTokens || 0
|
|
185
194
|
}
|
|
186
195
|
: undefined,
|
|
187
196
|
stopReason: this.mapFinishReason(result.finishReason),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vercel-ai.js","sourceRoot":"","sources":["../../src/adapters/vercel-ai.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"vercel-ai.js","sourceRoot":"","sources":["../../src/adapters/vercel-ai.ts"],"names":[],"mappings":";;;AAAA,2BAAuD;AACvD,6BAAwB;AAYxB;;;GAGG;AACH,MAAa,eAAe;IAc1B;;;OAGG;IACH,YAAY,KAAoB,EAAE,YAAY,GAAG,QAAQ;QAjBhD,SAAI,GAAG,WAAW,CAAC;QACnB,iBAAY,GAAyB;YAC5C,wBAAwB,EAAE,KAAK;YAC/B,2BAA2B,EAAE,KAAK;YAClC,sBAAsB,EAAE,KAAK;YAC7B,iBAAiB,EAAE,IAAI;YACvB,yBAAyB,EAAE,IAAI;YAC/B,SAAS,EAAE,MAAM,CAAC,uCAAuC;SAC1D,CAAC;QAUA,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,UAAU,CAAC,IAAoB;QAC7B,wDAAwD;QACxD,IAAI,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC;QAElC,gDAAgD;QAChD,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,OAAO,IAAI,UAAU,EAAE,CAAC;YAC5D,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC;QAChC,CAAC;QAED,kDAAkD;QAClD,IAAI,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC;QACnC,IAAI,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACxD,WAAW,IAAI,oBAAoB,CAAC;YACpC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,EAAE;gBACxC,WAAW,IAAI,KAAK,CAAC,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3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Provider-agnostic advanced tool use library for LLMs",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -28,20 +28,36 @@
|
|
|
28
28
|
"author": "",
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"dependencies": {
|
|
31
|
+
"natural": "^6.0.0",
|
|
31
32
|
"openai": "^4.0.0",
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
"zod": "^3.22.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
35
36
|
"@ai-sdk/anthropic": "^1.0.0",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
37
|
+
"@ai-sdk/openai": "^1.0.0",
|
|
38
|
+
"ai": "^4.0.0 || ^5.0.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"ai": {
|
|
42
|
+
"optional": true
|
|
43
|
+
},
|
|
44
|
+
"@ai-sdk/openai": {
|
|
45
|
+
"optional": true
|
|
46
|
+
},
|
|
47
|
+
"@ai-sdk/anthropic": {
|
|
48
|
+
"optional": true
|
|
49
|
+
}
|
|
38
50
|
},
|
|
39
51
|
"devDependencies": {
|
|
40
|
-
"@
|
|
52
|
+
"@ai-sdk/anthropic": "^1.0.0",
|
|
53
|
+
"@ai-sdk/openai": "^1.0.0",
|
|
54
|
+
"@anthropic-ai/sdk": "^0.27.0",
|
|
41
55
|
"@types/jest": "^29.0.0",
|
|
42
56
|
"@types/natural": "^5.1.0",
|
|
57
|
+
"@types/node": "^20.0.0",
|
|
43
58
|
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
44
59
|
"@typescript-eslint/parser": "^6.0.0",
|
|
60
|
+
"ai": "^5.0.116",
|
|
45
61
|
"eslint": "^8.0.0",
|
|
46
62
|
"jest": "^29.0.0",
|
|
47
63
|
"prettier": "^3.0.0",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { generateText, LanguageModel,
|
|
1
|
+
import { generateText, LanguageModel, tool } from 'ai';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import {
|
|
4
4
|
ProviderAdapter,
|
|
@@ -155,7 +155,7 @@ export class VercelAIAdapter implements ProviderAdapter {
|
|
|
155
155
|
const messages = this.convertMessages(request.messages);
|
|
156
156
|
|
|
157
157
|
// Convert our tools to AI SDK format using the tool() helper
|
|
158
|
-
const tools: Record<string,
|
|
158
|
+
const tools: Record<string, any> = {};
|
|
159
159
|
const toolDefinitions = request.tools?.filter(t => !t.deferLoading) || [];
|
|
160
160
|
|
|
161
161
|
for (const toolDef of toolDefinitions) {
|
|
@@ -172,31 +172,42 @@ export class VercelAIAdapter implements ProviderAdapter {
|
|
|
172
172
|
const parameters = this.jsonSchemaToZod(toolDef.inputSchema);
|
|
173
173
|
|
|
174
174
|
// Create the tool using AI SDK's tool() helper
|
|
175
|
-
|
|
175
|
+
// Store reference to toolDef in closure to avoid issues
|
|
176
|
+
const handler = toolDef.handler;
|
|
177
|
+
const name = toolDef.name;
|
|
178
|
+
tools[name] = {
|
|
176
179
|
description,
|
|
177
180
|
parameters,
|
|
178
181
|
execute: async (args: any) => {
|
|
179
182
|
try {
|
|
180
|
-
const result = await
|
|
183
|
+
const result = await handler(args);
|
|
181
184
|
return result;
|
|
182
185
|
} catch (error: any) {
|
|
183
|
-
throw new Error(`Tool ${
|
|
186
|
+
throw new Error(`Tool ${name} failed: ${error.message}`);
|
|
184
187
|
}
|
|
185
188
|
}
|
|
186
|
-
}
|
|
189
|
+
} as any;
|
|
187
190
|
}
|
|
188
191
|
|
|
189
192
|
// Generate text with tools
|
|
190
|
-
const
|
|
193
|
+
const generateOptions: any = {
|
|
191
194
|
model: this.model,
|
|
192
195
|
messages: messages.map(m => ({
|
|
193
196
|
role: m.role,
|
|
194
197
|
content: m.content
|
|
195
198
|
})),
|
|
196
|
-
tools: Object.keys(tools).length > 0 ? tools : undefined
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
199
|
+
tools: Object.keys(tools).length > 0 ? tools : undefined
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
// Add optional parameters if provided
|
|
203
|
+
if (request.temperature !== undefined) {
|
|
204
|
+
generateOptions.temperature = request.temperature;
|
|
205
|
+
}
|
|
206
|
+
if (request.maxTokens !== undefined) {
|
|
207
|
+
generateOptions.maxCompletionTokens = request.maxTokens;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const result = await generateText(generateOptions);
|
|
200
211
|
|
|
201
212
|
// Parse tool calls from result
|
|
202
213
|
const toolCalls: ToolCall[] = [];
|
|
@@ -218,9 +229,9 @@ export class VercelAIAdapter implements ProviderAdapter {
|
|
|
218
229
|
},
|
|
219
230
|
usage: result.usage
|
|
220
231
|
? {
|
|
221
|
-
inputTokens: result.usage.promptTokens,
|
|
222
|
-
outputTokens: result.usage.completionTokens,
|
|
223
|
-
totalTokens: result.usage.totalTokens
|
|
232
|
+
inputTokens: result.usage.inputTokens || (result.usage as any).promptTokens || 0,
|
|
233
|
+
outputTokens: result.usage.outputTokens || (result.usage as any).completionTokens || 0,
|
|
234
|
+
totalTokens: result.usage.totalTokens || 0
|
|
224
235
|
}
|
|
225
236
|
: undefined,
|
|
226
237
|
stopReason: this.mapFinishReason(result.finishReason),
|