llmjs2 1.0.8 → 1.1.0
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/README.md +306 -78
- package/example.js +298 -0
- package/index.js +183 -420
- package/package.json +18 -18
- package/providers/ollama.js +88 -0
- package/providers/openrouter.js +79 -0
- package/test.js +296 -0
- package/index.d.ts +0 -43
package/example.js
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { completion } from './index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Real usage test program for vs code
|
|
5
|
+
* This demonstrates actual API calls to Ollama and OpenRouter
|
|
6
|
+
* Shows all three calling conventions including simple API with model
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
async function testSimpleAPI() {
|
|
10
|
+
console.log('=== Testing Simple API (Auto-Detection) ===\n');
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const prompt = 'Explain the use of vs code in 2-3 sentences.';
|
|
14
|
+
console.log(`Prompt: ${prompt}\n`);
|
|
15
|
+
|
|
16
|
+
const result = await completion(prompt);
|
|
17
|
+
|
|
18
|
+
console.log('Response:');
|
|
19
|
+
console.log(result);
|
|
20
|
+
console.log('\n✓ Simple API test completed successfully\n');
|
|
21
|
+
return true;
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error('✗ Simple API test failed:', error.message);
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function testSimpleAPIWithModel() {
|
|
29
|
+
console.log('=== Testing Simple API with Model ===\n');
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const model = 'ollama/qwen3.5:397b-cloud';
|
|
33
|
+
const prompt = 'Explain the use of vs code in 2-3 sentences.';
|
|
34
|
+
console.log(`Model: ${model}`);
|
|
35
|
+
console.log(`Prompt: ${prompt}\n`);
|
|
36
|
+
|
|
37
|
+
const result = await completion(model, prompt);
|
|
38
|
+
|
|
39
|
+
console.log('Response:');
|
|
40
|
+
console.log(result);
|
|
41
|
+
console.log('\n✓ Simple API with model test completed successfully\n');
|
|
42
|
+
return true;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error('✗ Simple API with model test failed:', error.message);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function testOllamaFunctionAPI() {
|
|
50
|
+
console.log('=== Testing Ollama with Function-Based API ===\n');
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
const prompt = 'Explain the use of vs code in 2-3 sentences.';
|
|
54
|
+
console.log(`Prompt: ${prompt}\n`);
|
|
55
|
+
|
|
56
|
+
const result = await completion('ollama/minimax-m2.5:cloud', prompt);
|
|
57
|
+
|
|
58
|
+
console.log('Response:');
|
|
59
|
+
console.log(result);
|
|
60
|
+
console.log('\n✓ Ollama function-based API test completed successfully\n');
|
|
61
|
+
return true;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.error('✗ Ollama function-based API test failed:', error.message);
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function testOllamaObjectAPI() {
|
|
69
|
+
console.log('=== Testing Ollama with Object-Based API ===\n');
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const result = await completion({
|
|
73
|
+
model: 'ollama/minimax-m2.5:cloud',
|
|
74
|
+
messages: [
|
|
75
|
+
{ role: 'system', content: 'You are a helpful AI assistant.' },
|
|
76
|
+
{ role: 'user', content: 'Explain the use of vs code in 2-3 sentences.' }
|
|
77
|
+
]
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
console.log('Response:');
|
|
81
|
+
console.log(result);
|
|
82
|
+
console.log('\n✓ Ollama object-based API test completed successfully\n');
|
|
83
|
+
return true;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error('✗ Ollama object-based API test failed:', error.message);
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function testOllamaWithTools() {
|
|
91
|
+
console.log('=== Testing Ollama with Tools ===\n');
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
const result = await completion({
|
|
95
|
+
model: 'ollama/minimax-m2.5:cloud',
|
|
96
|
+
messages: [
|
|
97
|
+
{ role: 'user', content: 'What is the weather like in Paris?' }
|
|
98
|
+
],
|
|
99
|
+
tools: [
|
|
100
|
+
{
|
|
101
|
+
type: 'function',
|
|
102
|
+
function: {
|
|
103
|
+
name: 'get_weather',
|
|
104
|
+
description: 'Get the current weather in a given location',
|
|
105
|
+
parameters: {
|
|
106
|
+
type: 'object',
|
|
107
|
+
properties: {
|
|
108
|
+
location: {
|
|
109
|
+
type: 'string',
|
|
110
|
+
description: 'The city and state, e.g. San Francisco, CA'
|
|
111
|
+
},
|
|
112
|
+
unit: {
|
|
113
|
+
type: 'string',
|
|
114
|
+
enum: ['celsius', 'fahrenheit'],
|
|
115
|
+
description: 'The temperature unit to use'
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
required: ['location']
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
]
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
console.log('Response:');
|
|
126
|
+
console.log(JSON.stringify(result, null, 2));
|
|
127
|
+
console.log('\n✓ Ollama tools test completed successfully\n');
|
|
128
|
+
return true;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error('✗ Ollama tools test failed:', error.message);
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function testOpenRouterFunctionAPI() {
|
|
136
|
+
console.log('=== Testing OpenRouter with Function-Based API ===\n');
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
const prompt = 'What is the capital of France? Answer in one sentence.';
|
|
140
|
+
console.log(`Prompt: ${prompt}\n`);
|
|
141
|
+
|
|
142
|
+
const result = await completion('openrouter/openrouter/free', prompt);
|
|
143
|
+
|
|
144
|
+
console.log('Response:');
|
|
145
|
+
console.log(result);
|
|
146
|
+
console.log('\n✓ OpenRouter function-based API test completed successfully\n');
|
|
147
|
+
return true;
|
|
148
|
+
} catch (error) {
|
|
149
|
+
console.error('✗ OpenRouter function-based API test failed:', error.message);
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function testOpenRouterObjectAPI() {
|
|
155
|
+
console.log('=== Testing OpenRouter with Object-Based API ===\n');
|
|
156
|
+
|
|
157
|
+
try {
|
|
158
|
+
const result = await completion({
|
|
159
|
+
model: 'openrouter/openrouter/free',
|
|
160
|
+
messages: [
|
|
161
|
+
{ role: 'system', content: 'You are a helpful AI assistant.' },
|
|
162
|
+
{ role: 'user', content: 'What is the capital of France? Answer in one sentence.' }
|
|
163
|
+
]
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
console.log('Response:');
|
|
167
|
+
console.log(result);
|
|
168
|
+
console.log('\n✓ OpenRouter object-based API test completed successfully\n');
|
|
169
|
+
return true;
|
|
170
|
+
} catch (error) {
|
|
171
|
+
console.error('✗ OpenRouter object-based API test failed:', error.message);
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async function testOpenRouterWithTools() {
|
|
177
|
+
console.log('=== Testing OpenRouter with Tools ===\n');
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
const result = await completion({
|
|
181
|
+
model: 'openrouter/openrouter/free',
|
|
182
|
+
messages: [
|
|
183
|
+
{ role: 'user', content: 'What is the weather like in Paris?' }
|
|
184
|
+
],
|
|
185
|
+
tools: [
|
|
186
|
+
{
|
|
187
|
+
type: 'function',
|
|
188
|
+
function: {
|
|
189
|
+
name: 'get_weather',
|
|
190
|
+
description: 'Get the current weather in a given location',
|
|
191
|
+
parameters: {
|
|
192
|
+
type: 'object',
|
|
193
|
+
properties: {
|
|
194
|
+
location: {
|
|
195
|
+
type: 'string',
|
|
196
|
+
description: 'The city and state, e.g. San Francisco, CA'
|
|
197
|
+
},
|
|
198
|
+
unit: {
|
|
199
|
+
type: 'string',
|
|
200
|
+
enum: ['celsius', 'fahrenheit'],
|
|
201
|
+
description: 'The temperature unit to use'
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
required: ['location']
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
]
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
console.log('Response:');
|
|
212
|
+
console.log(JSON.stringify(result, null, 2));
|
|
213
|
+
console.log('\n✓ OpenRouter tools test completed successfully\n');
|
|
214
|
+
return true;
|
|
215
|
+
} catch (error) {
|
|
216
|
+
console.error('✗ OpenRouter tools test failed:', error.message);
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async function main() {
|
|
222
|
+
console.log('vs code Real Usage Test Program');
|
|
223
|
+
console.log('==============================\n');
|
|
224
|
+
|
|
225
|
+
// Check for API keys
|
|
226
|
+
const hasOllamaKey = !!process.env.OLLAMA_API_KEY;
|
|
227
|
+
const hasOpenRouterKey = !!process.env.OPEN_ROUTER_API_KEY;
|
|
228
|
+
|
|
229
|
+
console.log('API Key Status:');
|
|
230
|
+
console.log(` Ollama: ${hasOllamaKey ? '✓ Set' : '✗ Not set'}`);
|
|
231
|
+
console.log(` OpenRouter: ${hasOpenRouterKey ? '✓ Set' : '✗ Not set'}\n`);
|
|
232
|
+
|
|
233
|
+
if (!hasOllamaKey && !hasOpenRouterKey) {
|
|
234
|
+
console.log('⚠ No API keys found. Please set environment variables:');
|
|
235
|
+
console.log(' export OLLAMA_API_KEY=your-ollama-api-key');
|
|
236
|
+
console.log(' export OPEN_ROUTER_API_KEY=your-openrouter-api-key\n');
|
|
237
|
+
console.log('Or modify this script to pass API keys directly.\n');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const results = {
|
|
241
|
+
simpleAPI: false,
|
|
242
|
+
simpleAPIWithModel: false,
|
|
243
|
+
ollamaFunction: false,
|
|
244
|
+
ollamaObject: false,
|
|
245
|
+
ollamaTools: false,
|
|
246
|
+
openrouterFunction: false,
|
|
247
|
+
openrouterObject: false,
|
|
248
|
+
openrouterTools: false
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// Test simple API if any key is available
|
|
252
|
+
if (hasOllamaKey || hasOpenRouterKey) {
|
|
253
|
+
results.simpleAPI = await testSimpleAPI();
|
|
254
|
+
results.simpleAPIWithModel = await testSimpleAPIWithModel();
|
|
255
|
+
} else {
|
|
256
|
+
console.log('=== Skipping Simple API tests (no API key) ===\n');
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Test Ollama if key is available
|
|
260
|
+
if (hasOllamaKey) {
|
|
261
|
+
results.ollamaFunction = await testOllamaFunctionAPI();
|
|
262
|
+
results.ollamaObject = await testOllamaObjectAPI();
|
|
263
|
+
results.ollamaTools = await testOllamaWithTools();
|
|
264
|
+
} else {
|
|
265
|
+
console.log('=== Skipping Ollama tests (no API key) ===\n');
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Test OpenRouter if key is available
|
|
269
|
+
if (hasOpenRouterKey) {
|
|
270
|
+
results.openrouterFunction = await testOpenRouterFunctionAPI();
|
|
271
|
+
results.openrouterObject = await testOpenRouterObjectAPI();
|
|
272
|
+
results.openrouterTools = await testOpenRouterWithTools();
|
|
273
|
+
} else {
|
|
274
|
+
console.log('=== Skipping OpenRouter tests (no API key) ===\n');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Summary
|
|
278
|
+
console.log('==============================');
|
|
279
|
+
console.log('Test Summary:');
|
|
280
|
+
console.log(` Simple API: ${results.simpleAPI ? '✓ Passed' : '✗ Failed/Skipped'}`);
|
|
281
|
+
console.log(` Simple API with Model: ${results.simpleAPIWithModel ? '✓ Passed' : '✗ Failed/Skipped'}`);
|
|
282
|
+
console.log(` Ollama Function API: ${results.ollamaFunction ? '✓ Passed' : '✗ Failed/Skipped'}`);
|
|
283
|
+
console.log(` Ollama Object API: ${results.ollamaObject ? '✓ Passed' : '✗ Failed/Skipped'}`);
|
|
284
|
+
console.log(` Ollama Tools: ${results.ollamaTools ? '✓ Passed' : '✗ Failed/Skipped'}`);
|
|
285
|
+
console.log(` OpenRouter Function API: ${results.openrouterFunction ? '✓ Passed' : '✗ Failed/Skipped'}`);
|
|
286
|
+
console.log(` OpenRouter Object API: ${results.openrouterObject ? '✓ Passed' : '✗ Failed/Skipped'}`);
|
|
287
|
+
console.log(` OpenRouter Tools: ${results.openrouterTools ? '✓ Passed' : '✗ Failed/Skipped'}`);
|
|
288
|
+
console.log('==============================\n');
|
|
289
|
+
|
|
290
|
+
if (results.simpleAPI || results.simpleAPIWithModel || results.ollamaFunction || results.ollamaObject || results.ollamaTools ||
|
|
291
|
+
results.openrouterFunction || results.openrouterObject || results.openrouterTools) {
|
|
292
|
+
console.log('✓ At least one test passed!');
|
|
293
|
+
} else {
|
|
294
|
+
console.log('✗ No tests passed. Check your API keys and try again.');
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
main().catch(console.error);
|