-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathllm-provider.test.js
More file actions
269 lines (228 loc) · 9.76 KB
/
Copy pathllm-provider.test.js
File metadata and controls
269 lines (228 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import {
PROVIDER_PRESETS,
detectProvider,
clampTemperature,
resolveLLMConfig,
buildRequestBody,
stripThinkTags,
} from '../scripts/llm-provider.js';
/* ------------------------------------------------------------------ */
/* PROVIDER_PRESETS */
/* ------------------------------------------------------------------ */
describe('PROVIDER_PRESETS', () => {
it('should contain openai, minimax, deepseek, and ollama presets', () => {
expect(PROVIDER_PRESETS).toHaveProperty('openai');
expect(PROVIDER_PRESETS).toHaveProperty('minimax');
expect(PROVIDER_PRESETS).toHaveProperty('deepseek');
expect(PROVIDER_PRESETS).toHaveProperty('ollama');
});
it('minimax preset should use api.minimax.io base URL', () => {
expect(PROVIDER_PRESETS.minimax.baseUrl).toBe('https://api.minimax.io/v1');
});
it('minimax preset should default to MiniMax-M3 model', () => {
expect(PROVIDER_PRESETS.minimax.defaultModel).toBe('MiniMax-M3');
});
it('minimax preset should have temperature range [0, 1]', () => {
expect(PROVIDER_PRESETS.minimax.temperatureRange).toEqual([0, 1]);
});
it('minimax preset should list available models', () => {
expect(PROVIDER_PRESETS.minimax.models).toContain('MiniMax-M3');
expect(PROVIDER_PRESETS.minimax.models).toContain('MiniMax-M2.7');
});
it('minimax preset should use MINIMAX_API_KEY env var', () => {
expect(PROVIDER_PRESETS.minimax.envKey).toBe('MINIMAX_API_KEY');
});
it('openai preset should have temperature range [0, 2]', () => {
expect(PROVIDER_PRESETS.openai.temperatureRange).toEqual([0, 2]);
});
it('each preset should have required fields', () => {
for (const [key, preset] of Object.entries(PROVIDER_PRESETS)) {
expect(preset).toHaveProperty('name');
expect(preset).toHaveProperty('baseUrl');
expect(preset).toHaveProperty('defaultModel');
expect(preset).toHaveProperty('temperatureRange');
expect(preset.temperatureRange).toHaveLength(2);
}
});
});
/* ------------------------------------------------------------------ */
/* detectProvider */
/* ------------------------------------------------------------------ */
describe('detectProvider', () => {
const envBackup = { ...process.env };
afterEach(() => {
process.env = { ...envBackup };
});
it('should detect minimax from base URL containing "minimax"', () => {
expect(detectProvider('key', 'https://api.minimax.io/v1')).toBe('minimax');
});
it('should detect deepseek from base URL', () => {
expect(detectProvider('key', 'https://api.deepseek.com/v1')).toBe('deepseek');
});
it('should detect openai from base URL', () => {
expect(detectProvider('key', 'https://api.openai.com/v1')).toBe('openai');
});
it('should detect ollama from localhost URL', () => {
expect(detectProvider('key', 'http://127.0.0.1:11434/v1')).toBe('ollama');
expect(detectProvider('key', 'http://localhost:11434/v1')).toBe('ollama');
});
it('should detect minimax from MINIMAX_API_KEY env var', () => {
process.env.MINIMAX_API_KEY = 'test-key';
expect(detectProvider('', '')).toBe('minimax');
});
it('should detect deepseek from DEEPSEEK_API_KEY env var', () => {
delete process.env.MINIMAX_API_KEY;
process.env.DEEPSEEK_API_KEY = 'test-key';
expect(detectProvider('', '')).toBe('deepseek');
});
it('should default to openai when API key is provided without URL', () => {
delete process.env.MINIMAX_API_KEY;
delete process.env.DEEPSEEK_API_KEY;
expect(detectProvider('sk-test', '')).toBe('openai');
});
it('should default to ollama when no API key or URL', () => {
delete process.env.MINIMAX_API_KEY;
delete process.env.DEEPSEEK_API_KEY;
expect(detectProvider('', '')).toBe('ollama');
expect(detectProvider('local-fallback', '')).toBe('ollama');
});
});
/* ------------------------------------------------------------------ */
/* clampTemperature */
/* ------------------------------------------------------------------ */
describe('clampTemperature', () => {
it('should clamp temperature to minimax range [0, 1]', () => {
expect(clampTemperature(1.5, 'minimax')).toBe(1);
expect(clampTemperature(0.5, 'minimax')).toBe(0.5);
expect(clampTemperature(0, 'minimax')).toBe(0);
expect(clampTemperature(-0.1, 'minimax')).toBe(0);
});
it('should allow temperature up to 2 for openai', () => {
expect(clampTemperature(1.5, 'openai')).toBe(1.5);
expect(clampTemperature(2, 'openai')).toBe(2);
expect(clampTemperature(2.5, 'openai')).toBe(2);
});
it('should return original value for unknown provider', () => {
expect(clampTemperature(5, 'unknown-provider')).toBe(5);
});
});
/* ------------------------------------------------------------------ */
/* resolveLLMConfig */
/* ------------------------------------------------------------------ */
describe('resolveLLMConfig', () => {
const envBackup = { ...process.env };
beforeEach(() => {
// Clear all relevant env vars
delete process.env.LLM_PROVIDER;
delete process.env.LLM_API_KEY;
delete process.env.LLM_BASE_URL;
delete process.env.LLM_MODEL;
delete process.env.MINIMAX_API_KEY;
delete process.env.OPENAI_API_KEY;
delete process.env.DEEPSEEK_API_KEY;
});
afterEach(() => {
process.env = { ...envBackup };
});
it('should use explicit LLM_PROVIDER when set', () => {
process.env.LLM_PROVIDER = 'minimax';
process.env.LLM_API_KEY = 'test-key';
const config = resolveLLMConfig();
expect(config.provider).toBe('minimax');
expect(config.baseUrl).toBe('https://api.minimax.io/v1');
expect(config.model).toBe('MiniMax-M3');
});
it('should use provider-specific env key when LLM_API_KEY is empty', () => {
process.env.LLM_PROVIDER = 'minimax';
process.env.MINIMAX_API_KEY = 'mm-key-123';
const config = resolveLLMConfig();
expect(config.apiKey).toBe('mm-key-123');
});
it('should override model with LLM_MODEL env var', () => {
process.env.LLM_PROVIDER = 'minimax';
process.env.LLM_MODEL = 'MiniMax-M2.7';
const config = resolveLLMConfig();
expect(config.model).toBe('MiniMax-M2.7');
});
it('should override base URL with LLM_BASE_URL env var', () => {
process.env.LLM_PROVIDER = 'minimax';
process.env.LLM_BASE_URL = 'https://custom-proxy.example.com/v1';
const config = resolveLLMConfig();
expect(config.baseUrl).toBe('https://custom-proxy.example.com/v1');
});
it('should fallback to ollama when no keys set', () => {
const config = resolveLLMConfig();
expect(config.provider).toBe('ollama');
expect(config.model).toBe('llama3');
expect(config.apiKey).toBe('local-fallback');
});
it('should auto-detect minimax from MINIMAX_API_KEY', () => {
process.env.MINIMAX_API_KEY = 'mm-key';
const config = resolveLLMConfig();
expect(config.provider).toBe('minimax');
expect(config.apiKey).toBe('mm-key');
});
});
/* ------------------------------------------------------------------ */
/* buildRequestBody */
/* ------------------------------------------------------------------ */
describe('buildRequestBody', () => {
it('should clamp temperature for minimax provider', () => {
const body = buildRequestBody('minimax', 'MiniMax-M3', [], {
temperature: 1.5,
});
expect(body.temperature).toBe(1);
});
it('should keep temperature within range for openai', () => {
const body = buildRequestBody('openai', 'gpt-4o-mini', [], {
temperature: 1.5,
});
expect(body.temperature).toBe(1.5);
});
it('should include response_format when provided', () => {
const body = buildRequestBody('minimax', 'MiniMax-M3', [], {
responseFormat: { type: 'json_object' },
});
expect(body.response_format).toEqual({ type: 'json_object' });
});
it('should not include response_format when not provided', () => {
const body = buildRequestBody('minimax', 'MiniMax-M3', []);
expect(body).not.toHaveProperty('response_format');
});
it('should set model and messages correctly', () => {
const msgs = [{ role: 'user', content: 'Hello' }];
const body = buildRequestBody('minimax', 'MiniMax-M3', msgs);
expect(body.model).toBe('MiniMax-M3');
expect(body.messages).toBe(msgs);
});
it('should default temperature to 0.1 when not specified', () => {
const body = buildRequestBody('minimax', 'MiniMax-M3', []);
expect(body.temperature).toBe(0.1);
});
});
/* ------------------------------------------------------------------ */
/* stripThinkTags */
/* ------------------------------------------------------------------ */
describe('stripThinkTags', () => {
it('should remove think tags and their content', () => {
const input = '<think>reasoning here</think>{"result": true}';
expect(stripThinkTags(input)).toBe('{"result": true}');
});
it('should handle multiline think tags', () => {
const input = '<think>\nStep 1: ...\nStep 2: ...\n</think>\n{"evaluations": []}';
expect(stripThinkTags(input)).toBe('{"evaluations": []}');
});
it('should return original text when no think tags present', () => {
const input = '{"evaluations": []}';
expect(stripThinkTags(input)).toBe('{"evaluations": []}');
});
it('should handle multiple think tag blocks', () => {
const input = '<think>a</think>hello<think>b</think>world';
expect(stripThinkTags(input)).toBe('helloworld');
});
it('should handle empty think tags', () => {
const input = '<think></think>result';
expect(stripThinkTags(input)).toBe('result');
});
});