Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

ollama.test.ts 15 KB

You have to be logged in to leave a comment. Sign In
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
  1. import { fetchWithCache } from '../../src/cache';
  2. import {
  3. OllamaChatProvider,
  4. OllamaCompletionProvider,
  5. OllamaEmbeddingProvider,
  6. } from '../../src/providers/ollama';
  7. import type { CallApiContextParams } from '../../src/types';
  8. jest.mock('../../src/cache');
  9. describe('OllamaCompletionProvider', () => {
  10. beforeEach(() => {
  11. jest.resetAllMocks();
  12. });
  13. it('should construct with model name and options', () => {
  14. const provider = new OllamaCompletionProvider('llama3.3', {
  15. id: 'custom-id',
  16. config: { temperature: 0.7 },
  17. });
  18. expect(provider.modelName).toBe('llama3.3');
  19. expect(provider.config.temperature).toBe(0.7);
  20. expect(provider.id()).toBe('custom-id');
  21. });
  22. it('should call API and return response', async () => {
  23. const mockResponse = {
  24. data: '{"response":"test response","done":true}\n',
  25. cached: false,
  26. status: 200,
  27. statusText: 'OK',
  28. headers: {},
  29. };
  30. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  31. const provider = new OllamaCompletionProvider('llama3.3');
  32. const result = await provider.callApi('test prompt');
  33. expect(result).toEqual({
  34. output: 'test response',
  35. });
  36. });
  37. it('should handle multiple response chunks', async () => {
  38. const mockResponse = {
  39. data: '{"response":"test response","done":false}\n{"response":" more","done":true}\n',
  40. cached: false,
  41. status: 200,
  42. statusText: 'OK',
  43. headers: {},
  44. };
  45. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  46. const provider = new OllamaCompletionProvider('llama3.3');
  47. const result = await provider.callApi('test prompt');
  48. expect(result).toEqual({
  49. output: 'test response more',
  50. });
  51. });
  52. it('should handle API errors', async () => {
  53. jest.mocked(fetchWithCache).mockRejectedValue(new Error('API error'));
  54. const provider = new OllamaCompletionProvider('llama3.3');
  55. const result = await provider.callApi('test prompt');
  56. expect(result.error).toContain('API call error: Error: API error');
  57. });
  58. it('should handle API response with error field', async () => {
  59. const mockResponse = {
  60. data: { error: 'some error occurred' },
  61. cached: false,
  62. status: 200,
  63. statusText: 'OK',
  64. headers: {},
  65. };
  66. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  67. const provider = new OllamaCompletionProvider('llama3.3');
  68. const result = await provider.callApi('test prompt');
  69. expect(result.error).toBe('Ollama error: some error occurred');
  70. });
  71. it('should handle invalid JSON response', async () => {
  72. const mockResponse = {
  73. data: 'invalid json',
  74. cached: false,
  75. status: 200,
  76. statusText: 'OK',
  77. headers: {},
  78. };
  79. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  80. const provider = new OllamaCompletionProvider('llama3.3');
  81. const result = await provider.callApi('test prompt');
  82. expect(result.error).toContain('Ollama API response error:');
  83. });
  84. it('should use default id when not provided', () => {
  85. const provider = new OllamaCompletionProvider('llama3.3');
  86. expect(provider.id()).toBe('ollama:completion:llama3.3');
  87. });
  88. it('should handle toString method', () => {
  89. const provider = new OllamaCompletionProvider('llama3.3');
  90. expect(provider.toString()).toBe('[Ollama Completion Provider llama3.3]');
  91. });
  92. it('should extract token usage from response', async () => {
  93. const mockResponse = {
  94. data: '{"response":"test response","done":false,"prompt_eval_count":26}\n{"response":" more","done":true,"prompt_eval_count":26,"eval_count":259}\n',
  95. cached: false,
  96. status: 200,
  97. statusText: 'OK',
  98. headers: {},
  99. };
  100. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  101. const provider = new OllamaCompletionProvider('llama3.3');
  102. const result = await provider.callApi('test prompt');
  103. expect(result).toEqual({
  104. output: 'test response more',
  105. tokenUsage: {
  106. prompt: 26,
  107. completion: 259,
  108. total: 285,
  109. },
  110. });
  111. });
  112. it('should handle missing token usage gracefully', async () => {
  113. const mockResponse = {
  114. data: '{"response":"test response","done":true}\n',
  115. cached: false,
  116. status: 200,
  117. statusText: 'OK',
  118. headers: {},
  119. };
  120. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  121. const provider = new OllamaCompletionProvider('llama3.3');
  122. const result = await provider.callApi('test prompt');
  123. expect(result).toEqual({
  124. output: 'test response',
  125. });
  126. });
  127. it('should handle partial token usage (only prompt_eval_count)', async () => {
  128. const mockResponse = {
  129. data: '{"response":"test response","done":true,"prompt_eval_count":26}\n',
  130. cached: false,
  131. status: 200,
  132. statusText: 'OK',
  133. headers: {},
  134. };
  135. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  136. const provider = new OllamaCompletionProvider('llama3.3');
  137. const result = await provider.callApi('test prompt');
  138. expect(result).toEqual({
  139. output: 'test response',
  140. tokenUsage: {
  141. prompt: 26,
  142. completion: 0,
  143. total: 26,
  144. },
  145. });
  146. });
  147. it('should handle partial token usage (only eval_count)', async () => {
  148. const mockResponse = {
  149. data: '{"response":"test response","done":true,"eval_count":259}\n',
  150. cached: false,
  151. status: 200,
  152. statusText: 'OK',
  153. headers: {},
  154. };
  155. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  156. const provider = new OllamaCompletionProvider('llama3.3');
  157. const result = await provider.callApi('test prompt');
  158. expect(result).toEqual({
  159. output: 'test response',
  160. tokenUsage: {
  161. prompt: 0,
  162. completion: 259,
  163. total: 259,
  164. },
  165. });
  166. });
  167. });
  168. describe('OllamaChatProvider', () => {
  169. beforeEach(() => {
  170. jest.resetAllMocks();
  171. });
  172. it('should construct with model name and options', () => {
  173. const provider = new OllamaChatProvider('llama3.3', {
  174. id: 'custom-id',
  175. config: { temperature: 0.7 },
  176. });
  177. expect(provider.modelName).toBe('llama3.3');
  178. expect(provider.config.temperature).toBe(0.7);
  179. expect(provider.id()).toBe('custom-id');
  180. });
  181. it('should call chat API and return response', async () => {
  182. const mockResponse = {
  183. data: '{"message":{"role":"assistant","content":"test response","images":null},"done":true}\n',
  184. cached: false,
  185. status: 200,
  186. statusText: 'OK',
  187. headers: {},
  188. };
  189. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  190. const provider = new OllamaChatProvider('llama3.3');
  191. const result = await provider.callApi('test prompt');
  192. expect(result).toEqual({
  193. output: 'test response',
  194. });
  195. });
  196. it('should handle multiple chat response chunks', async () => {
  197. const mockResponse = {
  198. data: '{"message":{"role":"assistant","content":"test response","images":null},"done":false}\n{"message":{"role":"assistant","content":" more","images":null},"done":true}\n',
  199. cached: false,
  200. status: 200,
  201. statusText: 'OK',
  202. headers: {},
  203. };
  204. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  205. const provider = new OllamaChatProvider('llama3.3');
  206. const result = await provider.callApi('test prompt');
  207. expect(result).toEqual({
  208. output: 'test response more',
  209. });
  210. });
  211. it('should handle chat API errors', async () => {
  212. jest.mocked(fetchWithCache).mockRejectedValue(new Error('API error'));
  213. const provider = new OllamaChatProvider('llama3.3');
  214. const result = await provider.callApi('test prompt');
  215. expect(result.error).toContain('API call error: Error: API error');
  216. });
  217. it('should handle chat API response with error field', async () => {
  218. const mockResponse = {
  219. data: { error: 'chat error occurred' },
  220. cached: false,
  221. status: 200,
  222. statusText: 'OK',
  223. headers: {},
  224. };
  225. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  226. const provider = new OllamaChatProvider('llama3.3');
  227. const result = await provider.callApi('test prompt');
  228. expect(result.error).toBe('Ollama error: chat error occurred');
  229. });
  230. it('should handle invalid JSON response', async () => {
  231. const mockResponse = {
  232. data: 'invalid json',
  233. cached: false,
  234. status: 200,
  235. statusText: 'OK',
  236. headers: {},
  237. };
  238. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  239. const provider = new OllamaChatProvider('llama3.3');
  240. const result = await provider.callApi('test prompt');
  241. expect(result.error).toContain('Ollama API response error:');
  242. });
  243. it('should use default id when not provided', () => {
  244. const provider = new OllamaChatProvider('llama3.3');
  245. expect(provider.id()).toBe('ollama:chat:llama3.3');
  246. });
  247. it('should handle toString method', () => {
  248. const provider = new OllamaChatProvider('llama3.3');
  249. expect(provider.toString()).toBe('[Ollama Chat Provider llama3.3]');
  250. });
  251. it('should handle tools configuration', async () => {
  252. const provider = new OllamaChatProvider('llama3.3', {
  253. config: {
  254. tools: [{ name: 'test-tool' }],
  255. },
  256. });
  257. const mockResponse = {
  258. data: '{"message":{"role":"assistant","content":"test response","images":null},"done":true}\n',
  259. cached: false,
  260. status: 200,
  261. statusText: 'OK',
  262. headers: {},
  263. };
  264. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  265. const context: CallApiContextParams = {
  266. prompt: { raw: 'test prompt', label: 'test' },
  267. vars: { test: 'value' },
  268. debug: true,
  269. };
  270. await provider.callApi('test prompt', context);
  271. expect(jest.mocked(fetchWithCache).mock.calls[0]).toBeDefined();
  272. const call = jest.mocked(fetchWithCache).mock.calls[0] as any;
  273. expect(JSON.parse(call[1].body)).toMatchObject({
  274. tools: [{ name: 'test-tool' }],
  275. });
  276. expect(call[4]).toBe(true);
  277. });
  278. it('should handle context bustCache parameter', async () => {
  279. const provider = new OllamaChatProvider('llama3.3');
  280. const mockResponse = {
  281. data: '{"message":{"role":"assistant","content":"test response","images":null},"done":true}\n',
  282. cached: false,
  283. status: 200,
  284. statusText: 'OK',
  285. headers: {},
  286. };
  287. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  288. const context: CallApiContextParams = {
  289. prompt: { raw: 'test prompt', label: 'test' },
  290. vars: {},
  291. bustCache: true,
  292. };
  293. await provider.callApi('test prompt', context);
  294. expect(jest.mocked(fetchWithCache).mock.calls[0]).toBeDefined();
  295. const call = jest.mocked(fetchWithCache).mock.calls[0] as any;
  296. expect(call[4]).toBe(true);
  297. });
  298. it('should extract token usage from chat response', async () => {
  299. const mockResponse = {
  300. data: '{"message":{"role":"assistant","content":"test response","images":null},"done":false,"prompt_eval_count":26}\n{"message":{"role":"assistant","content":" more","images":null},"done":true,"prompt_eval_count":26,"eval_count":259}\n',
  301. cached: false,
  302. status: 200,
  303. statusText: 'OK',
  304. headers: {},
  305. };
  306. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  307. const provider = new OllamaChatProvider('llama3.3');
  308. const result = await provider.callApi('test prompt');
  309. expect(result).toEqual({
  310. output: 'test response more',
  311. tokenUsage: {
  312. prompt: 26,
  313. completion: 259,
  314. total: 285,
  315. },
  316. });
  317. });
  318. it('should handle missing token usage gracefully in chat', async () => {
  319. const mockResponse = {
  320. data: '{"message":{"role":"assistant","content":"test response","images":null},"done":true}\n',
  321. cached: false,
  322. status: 200,
  323. statusText: 'OK',
  324. headers: {},
  325. };
  326. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  327. const provider = new OllamaChatProvider('llama3.3');
  328. const result = await provider.callApi('test prompt');
  329. expect(result).toEqual({
  330. output: 'test response',
  331. });
  332. });
  333. it('should handle partial token usage in chat (only prompt_eval_count)', async () => {
  334. const mockResponse = {
  335. data: '{"message":{"role":"assistant","content":"test response","images":null},"done":true,"prompt_eval_count":26}\n',
  336. cached: false,
  337. status: 200,
  338. statusText: 'OK',
  339. headers: {},
  340. };
  341. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  342. const provider = new OllamaChatProvider('llama3.3');
  343. const result = await provider.callApi('test prompt');
  344. expect(result).toEqual({
  345. output: 'test response',
  346. tokenUsage: {
  347. prompt: 26,
  348. completion: 0,
  349. total: 26,
  350. },
  351. });
  352. });
  353. it('should handle partial token usage in chat (only eval_count)', async () => {
  354. const mockResponse = {
  355. data: '{"message":{"role":"assistant","content":"test response","images":null},"done":true,"eval_count":259}\n',
  356. cached: false,
  357. status: 200,
  358. statusText: 'OK',
  359. headers: {},
  360. };
  361. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  362. const provider = new OllamaChatProvider('llama3.3');
  363. const result = await provider.callApi('test prompt');
  364. expect(result).toEqual({
  365. output: 'test response',
  366. tokenUsage: {
  367. prompt: 0,
  368. completion: 259,
  369. total: 259,
  370. },
  371. });
  372. });
  373. });
  374. describe('OllamaEmbeddingProvider', () => {
  375. beforeEach(() => {
  376. jest.resetAllMocks();
  377. });
  378. it('should call embeddings API and return response', async () => {
  379. const mockResponse = {
  380. data: {
  381. embedding: [0.1, 0.2, 0.3],
  382. },
  383. cached: false,
  384. status: 200,
  385. statusText: 'OK',
  386. headers: {},
  387. };
  388. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  389. const provider = new OllamaEmbeddingProvider('llama3.3');
  390. const result = await provider.callEmbeddingApi('test text');
  391. expect(result).toEqual({
  392. embedding: [0.1, 0.2, 0.3],
  393. });
  394. });
  395. it('should handle embeddings API errors', async () => {
  396. jest.mocked(fetchWithCache).mockRejectedValue(new Error('API error'));
  397. const provider = new OllamaEmbeddingProvider('llama3.3');
  398. const result = await provider.callEmbeddingApi('test text');
  399. expect(result.error).toBe('API call error: Error: API error');
  400. });
  401. it('should handle missing embedding in response', async () => {
  402. const mockResponse = {
  403. data: {},
  404. cached: false,
  405. status: 200,
  406. statusText: 'OK',
  407. headers: {},
  408. };
  409. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  410. const provider = new OllamaEmbeddingProvider('llama3.3');
  411. const result = await provider.callEmbeddingApi('test text');
  412. expect(result.error).toContain('No embedding found in Ollama embeddings API response');
  413. });
  414. it('should handle invalid JSON response', async () => {
  415. const mockResponse = {
  416. data: 'invalid json',
  417. cached: false,
  418. status: 200,
  419. statusText: 'OK',
  420. headers: {},
  421. };
  422. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  423. const provider = new OllamaEmbeddingProvider('llama3.3');
  424. const result = await provider.callEmbeddingApi('test text');
  425. expect(result.error).toContain('API response error:');
  426. });
  427. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...