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

providers.bedrock.test.ts 21 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
  1. import dedent from 'dedent';
  2. import type {
  3. BedrockAI21GenerationOptions,
  4. BedrockClaudeMessagesCompletionOptions,
  5. LlamaMessage,
  6. } from '../src/providers/bedrock';
  7. import {
  8. addConfigParam,
  9. AwsBedrockGenericProvider,
  10. BEDROCK_MODEL,
  11. formatPromptLlama2Chat,
  12. formatPromptLlama3Instruct,
  13. getLlamaModelHandler,
  14. LlamaVersion,
  15. parseValue,
  16. } from '../src/providers/bedrock';
  17. jest.mock('@aws-sdk/client-bedrock-runtime', () => {
  18. return {
  19. BedrockRuntime: jest.fn().mockImplementation(() => {
  20. return {
  21. invokeModel: jest.fn(),
  22. };
  23. }),
  24. };
  25. });
  26. jest.mock(
  27. '@smithy/node-http-handler',
  28. () => {
  29. return {
  30. NodeHttpHandler: jest.fn(),
  31. };
  32. },
  33. { virtual: true },
  34. );
  35. jest.mock('proxy-agent', () => jest.fn());
  36. jest.mock('../src/cache', () => ({
  37. getCache: jest.fn(),
  38. isCacheEnabled: jest.fn(),
  39. }));
  40. jest.mock('../src/logger', () => ({
  41. debug: jest.fn(),
  42. warn: jest.fn(),
  43. error: jest.fn(),
  44. }));
  45. describe('AwsBedrockGenericProvider', () => {
  46. let BedrockRuntime: any;
  47. beforeEach(() => {
  48. jest.resetModules();
  49. // eslint-disable-next-line @typescript-eslint/no-require-imports
  50. BedrockRuntime = require('@aws-sdk/client-bedrock-runtime').BedrockRuntime;
  51. jest.clearAllMocks();
  52. });
  53. afterEach(() => {
  54. delete process.env.HTTP_PROXY;
  55. delete process.env.HTTPS_PROXY;
  56. });
  57. it('should create Bedrock instance without proxy settings', async () => {
  58. const provider = new (class extends AwsBedrockGenericProvider {
  59. constructor() {
  60. super('test-model', { config: { region: 'us-east-1' } });
  61. }
  62. })();
  63. await provider.getBedrockInstance();
  64. expect(BedrockRuntime).toHaveBeenCalledWith({
  65. region: 'us-east-1',
  66. });
  67. });
  68. it('should throw an error if NodeHttpHandler dependency is missing for proxy', async () => {
  69. process.env.HTTP_PROXY = 'http://localhost:8080';
  70. process.env.HTTPS_PROXY = 'https://localhost:8080';
  71. jest.doMock('@smithy/node-http-handler', () => {
  72. throw new Error('Missing dependency');
  73. });
  74. const provider = new (class extends AwsBedrockGenericProvider {
  75. constructor() {
  76. super('test-model', { config: { region: 'us-east-1' } });
  77. }
  78. })();
  79. await expect(provider.getBedrockInstance()).rejects.toThrow(
  80. 'The @smithy/node-http-handler package is required as a peer dependency. Please install it in your project or globally.',
  81. );
  82. });
  83. describe('BEDROCK_MODEL CLAUDE_MESSAGES', () => {
  84. const modelHandler = BEDROCK_MODEL.CLAUDE_MESSAGES;
  85. it('should include tools and tool_choice in params when provided', () => {
  86. const config: BedrockClaudeMessagesCompletionOptions = {
  87. region: 'us-east-1',
  88. tools: [
  89. {
  90. name: 'get_current_weather',
  91. description: 'Get the current weather in a given location',
  92. input_schema: {
  93. type: 'object',
  94. properties: {
  95. location: { type: 'string' },
  96. unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
  97. },
  98. required: ['location'],
  99. },
  100. },
  101. ],
  102. tool_choice: {
  103. type: 'auto',
  104. },
  105. };
  106. const params = modelHandler.params(config, 'Test prompt');
  107. expect(params).toHaveProperty('tools');
  108. expect(params.tools).toHaveLength(1);
  109. expect(params.tools[0]).toHaveProperty('name', 'get_current_weather');
  110. expect(params).toHaveProperty('tool_choice');
  111. expect(params.tool_choice).toEqual({ type: 'auto' });
  112. });
  113. it('should not include tools and tool_choice in params when not provided', () => {
  114. const config: BedrockClaudeMessagesCompletionOptions = {
  115. region: 'us-east-1',
  116. };
  117. const params = modelHandler.params(config, 'Test prompt');
  118. expect(params).not.toHaveProperty('tools');
  119. expect(params).not.toHaveProperty('tool_choice');
  120. });
  121. it('should include specific tool_choice when provided', () => {
  122. const config: BedrockClaudeMessagesCompletionOptions = {
  123. region: 'us-east-1',
  124. tools: [
  125. {
  126. name: 'get_current_weather',
  127. description: 'Get the current weather in a given location',
  128. input_schema: {
  129. type: 'object',
  130. properties: {
  131. location: { type: 'string' },
  132. unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
  133. },
  134. required: ['location'],
  135. },
  136. },
  137. ],
  138. tool_choice: {
  139. type: 'tool',
  140. name: 'get_current_weather',
  141. },
  142. };
  143. const params = modelHandler.params(config, 'Test prompt');
  144. expect(params).toHaveProperty('tool_choice');
  145. expect(params.tool_choice).toEqual({ type: 'tool', name: 'get_current_weather' });
  146. });
  147. });
  148. describe('BEDROCK_MODEL AI21', () => {
  149. const modelHandler = BEDROCK_MODEL.AI21;
  150. it('should include AI21-specific parameters', () => {
  151. const config: BedrockAI21GenerationOptions = {
  152. region: 'us-east-1',
  153. max_tokens: 100,
  154. temperature: 0.7,
  155. top_p: 0.9,
  156. stop: ['END'],
  157. frequency_penalty: 0.5,
  158. presence_penalty: 0.3,
  159. };
  160. const prompt = 'Write a short story about a robot.';
  161. const params = modelHandler.params(config, prompt);
  162. expect(params).toEqual({
  163. messages: [{ role: 'user', content: prompt }],
  164. max_tokens: 100,
  165. temperature: 0.7,
  166. top_p: 0.9,
  167. stop: ['END'],
  168. frequency_penalty: 0.5,
  169. presence_penalty: 0.3,
  170. });
  171. });
  172. it('should use default values when config is not provided', () => {
  173. const config: BedrockAI21GenerationOptions = { region: 'us-east-1' };
  174. const prompt = 'Tell me a joke.';
  175. const params = modelHandler.params(config, prompt);
  176. expect(params).toEqual({
  177. messages: [{ role: 'user', content: prompt }],
  178. max_tokens: 1024,
  179. temperature: 0,
  180. top_p: 1.0,
  181. });
  182. });
  183. it('should handle output correctly', () => {
  184. const mockResponse = {
  185. choices: [{ message: { content: 'This is a test response.' } }],
  186. };
  187. expect(modelHandler.output(mockResponse)).toBe('This is a test response.');
  188. });
  189. it('should throw an error for API errors', () => {
  190. const mockErrorResponse = { error: 'API Error' };
  191. expect(() => modelHandler.output(mockErrorResponse)).toThrow('AI21 API error: API Error');
  192. });
  193. });
  194. });
  195. describe('addConfigParam', () => {
  196. it('should add config value if provided', () => {
  197. const params: any = {};
  198. addConfigParam(params, 'key', 'configValue');
  199. expect(params.key).toBe('configValue');
  200. });
  201. it('should add env value if config value is not provided', () => {
  202. const params: any = {};
  203. process.env.TEST_ENV_KEY = 'envValue';
  204. addConfigParam(params, 'key', undefined, process.env.TEST_ENV_KEY);
  205. expect(params.key).toBe('envValue');
  206. delete process.env.TEST_ENV_KEY;
  207. });
  208. it('should add default value if neither config nor env value is provided', () => {
  209. const params: any = {};
  210. addConfigParam(params, 'key', undefined, undefined, 'defaultValue');
  211. expect(params.key).toBe('defaultValue');
  212. });
  213. it('should prioritize config value over env and default values', () => {
  214. const params: any = {};
  215. process.env.TEST_ENV_KEY = 'envValue';
  216. addConfigParam(params, 'key', 'configValue', process.env.TEST_ENV_KEY, 'defaultValue');
  217. expect(params.key).toBe('configValue');
  218. delete process.env.TEST_ENV_KEY;
  219. });
  220. it('should prioritize env value over default value if config value is not provided', () => {
  221. const params: any = {};
  222. process.env.TEST_ENV_KEY = 'envValue';
  223. addConfigParam(params, 'key', undefined, process.env.TEST_ENV_KEY, 'defaultValue');
  224. expect(params.key).toBe('envValue');
  225. delete process.env.TEST_ENV_KEY;
  226. });
  227. it('should parse env value if default value is a number', () => {
  228. const params: any = {};
  229. process.env.TEST_ENV_KEY = '42';
  230. addConfigParam(params, 'key', undefined, process.env.TEST_ENV_KEY, 0);
  231. expect(params.key).toBe(42);
  232. delete process.env.TEST_ENV_KEY;
  233. });
  234. it('should handle undefined config, env, and default values gracefully', () => {
  235. const params: any = {};
  236. addConfigParam(params, 'key', undefined, undefined, undefined);
  237. expect(params.key).toBeUndefined();
  238. });
  239. it('should correctly parse non-number string values', () => {
  240. const params: any = {};
  241. process.env.TEST_ENV_KEY = 'nonNumberString';
  242. addConfigParam(params, 'key', undefined, process.env.TEST_ENV_KEY, 0);
  243. expect(params.key).toBe(0);
  244. delete process.env.TEST_ENV_KEY;
  245. });
  246. it('should correctly parse empty string values', () => {
  247. const params: any = {};
  248. process.env.TEST_ENV_KEY = '';
  249. addConfigParam(params, 'key', undefined, process.env.TEST_ENV_KEY, 'defaultValue');
  250. expect(params.key).toBe('');
  251. delete process.env.TEST_ENV_KEY;
  252. });
  253. it('should handle env value not set', () => {
  254. const params: any = {};
  255. addConfigParam(params, 'key', undefined, process.env.UNSET_ENV_KEY, 'defaultValue');
  256. expect(params.key).toBe('defaultValue');
  257. });
  258. it('should handle config values that are objects', () => {
  259. const params: any = {};
  260. const configValue = { nestedKey: 'nestedValue' };
  261. addConfigParam(params, 'key', configValue);
  262. expect(params.key).toEqual(configValue);
  263. });
  264. it('should handle config values that are arrays', () => {
  265. const params: any = {};
  266. const configValue = ['value1', 'value2'];
  267. addConfigParam(params, 'key', configValue);
  268. expect(params.key).toEqual(configValue);
  269. });
  270. it('should handle special characters in env values', () => {
  271. const params: any = {};
  272. process.env.TEST_ENV_KEY = '!@#$%^&*()_+';
  273. addConfigParam(params, 'key', undefined, process.env.TEST_ENV_KEY, 'defaultValue');
  274. expect(params.key).toBe('!@#$%^&*()_+');
  275. delete process.env.TEST_ENV_KEY;
  276. });
  277. });
  278. describe('parseValue', () => {
  279. it('should return the original value if defaultValue is not a number', () => {
  280. expect(parseValue('stringValue', 'defaultValue')).toBe('stringValue');
  281. });
  282. it('should return parsed float value if defaultValue is a number', () => {
  283. expect(parseValue('42.5', 0)).toBe(42.5);
  284. });
  285. it('should return NaN for non-numeric strings if defaultValue is a number', () => {
  286. expect(parseValue('notANumber', 0)).toBe(0);
  287. });
  288. it('should return 0 for an empty string if defaultValue is a number', () => {
  289. expect(parseValue('', 0)).toBe(0);
  290. });
  291. it('should return null for a null value if defaultValue is not a number', () => {
  292. expect(parseValue(null as never, 'defaultValue')).toBeNull();
  293. });
  294. it('should return undefined for an undefined value if defaultValue is not a number', () => {
  295. expect(parseValue(undefined as never, 'defaultValue')).toBeUndefined();
  296. });
  297. });
  298. describe('llama', () => {
  299. describe('getLlamaModelHandler', () => {
  300. describe('LLAMA2', () => {
  301. const handler = getLlamaModelHandler(LlamaVersion.V2);
  302. it('should generate correct prompt for a single user message', () => {
  303. const config = { temperature: 0.5, top_p: 0.9, max_gen_len: 512 };
  304. const prompt = 'Describe the purpose of a "hello world" program in one sentence.';
  305. expect(handler.params(config, prompt)).toEqual({
  306. prompt: `<s>[INST] Describe the purpose of a \"hello world\" program in one sentence. [/INST]`,
  307. temperature: 0.5,
  308. top_p: 0.9,
  309. max_gen_len: 512,
  310. });
  311. });
  312. it('should handle a system message followed by a user message', () => {
  313. const config = {};
  314. const prompt = JSON.stringify([
  315. { role: 'system', content: 'You are a helpful assistant.' },
  316. { role: 'user', content: 'What is the capital of France?' },
  317. ]);
  318. expect(handler.params(config, prompt)).toEqual({
  319. prompt: dedent`<s>[INST] <<SYS>>
  320. You are a helpful assistant.
  321. <</SYS>>
  322. What is the capital of France? [/INST]`,
  323. temperature: 0.01,
  324. top_p: 1,
  325. max_gen_len: 1024,
  326. });
  327. });
  328. it('should handle multiple turns of conversation', () => {
  329. const config = {};
  330. const prompt = JSON.stringify([
  331. { role: 'user', content: 'Hello' },
  332. { role: 'assistant', content: 'Hi there! How can I assist you today?' },
  333. { role: 'user', content: "What's the weather like?" },
  334. ]);
  335. expect(handler.params(config, prompt)).toEqual({
  336. prompt:
  337. "<s>[INST] Hello [/INST] Hi there! How can I assist you today? </s><s>[INST] What's the weather like? [/INST]",
  338. temperature: 0.01,
  339. top_p: 1,
  340. max_gen_len: 1024,
  341. });
  342. });
  343. });
  344. describe('LLAMA3', () => {
  345. const handler = getLlamaModelHandler(LlamaVersion.V3);
  346. it('should generate correct prompt for a single user message', () => {
  347. const config = { temperature: 0.5, top_p: 0.9, max_gen_len: 512 };
  348. const prompt = 'Describe the purpose of a "hello world" program in one sentence.';
  349. expect(handler.params(config, prompt)).toEqual({
  350. prompt: dedent`<|begin_of_text|><|start_header_id|>user<|end_header_id|>
  351. Describe the purpose of a "hello world" program in one sentence.<|eot_id|><|start_header_id|>assistant<|end_header_id|>`,
  352. temperature: 0.5,
  353. top_p: 0.9,
  354. max_gen_len: 512,
  355. });
  356. });
  357. it('should handle a system message followed by a user message', () => {
  358. const config = {};
  359. const prompt = JSON.stringify([
  360. { role: 'system', content: 'You are a helpful assistant.' },
  361. { role: 'user', content: 'What is the capital of France?' },
  362. ]);
  363. expect(handler.params(config, prompt)).toEqual({
  364. prompt: dedent`<|begin_of_text|><|start_header_id|>system<|end_header_id|>
  365. You are a helpful assistant.<|eot_id|><|start_header_id|>user<|end_header_id|>
  366. What is the capital of France?<|eot_id|><|start_header_id|>assistant<|end_header_id|>`,
  367. temperature: 0.01,
  368. top_p: 1,
  369. max_gen_len: 1024,
  370. });
  371. });
  372. it('should handle multiple turns of conversation', () => {
  373. const config = {};
  374. const prompt = JSON.stringify([
  375. { role: 'user', content: 'Hello' },
  376. { role: 'assistant', content: 'Hi there! How can I assist you today?' },
  377. { role: 'user', content: "What's the weather like?" },
  378. ]);
  379. expect(handler.params(config, prompt)).toEqual({
  380. prompt: dedent`<|begin_of_text|><|start_header_id|>user<|end_header_id|>
  381. Hello<|eot_id|><|start_header_id|>assistant<|end_header_id|>
  382. Hi there! How can I assist you today?<|eot_id|><|start_header_id|>user<|end_header_id|>
  383. What's the weather like?<|eot_id|><|start_header_id|>assistant<|end_header_id|>`,
  384. temperature: 0.01,
  385. top_p: 1,
  386. max_gen_len: 1024,
  387. });
  388. });
  389. });
  390. describe('LLAMA3_1', () => {
  391. const handler = getLlamaModelHandler(LlamaVersion.V3_1);
  392. it('should generate correct prompt for a single user message', () => {
  393. const config = { temperature: 0.5, top_p: 0.9, max_gen_len: 512 };
  394. const prompt = 'Describe the purpose of a "hello world" program in one sentence.';
  395. expect(handler.params(config, prompt)).toEqual({
  396. prompt: dedent`<|begin_of_text|><|start_header_id|>user<|end_header_id|>
  397. Describe the purpose of a "hello world" program in one sentence.<|eot_id|><|start_header_id|>assistant<|end_header_id|>`,
  398. temperature: 0.5,
  399. top_p: 0.9,
  400. max_gen_len: 512,
  401. });
  402. });
  403. });
  404. describe('LLAMA3_2', () => {
  405. const handler = getLlamaModelHandler(LlamaVersion.V3_2);
  406. it('should generate correct prompt for a single user message', () => {
  407. const config = { temperature: 0.5, top_p: 0.9, max_new_tokens: 512 };
  408. const prompt = 'Describe the purpose of a "hello world" program in one sentence.';
  409. expect(handler.params(config, prompt)).toEqual({
  410. prompt: dedent`<|begin_of_text|><|start_header_id|>user<|end_header_id|>
  411. Describe the purpose of a "hello world" program in one sentence.<|eot_id|><|start_header_id|>assistant<|end_header_id|>`,
  412. temperature: 0.5,
  413. top_p: 0.9,
  414. max_new_tokens: 512,
  415. });
  416. });
  417. it('should use max_new_tokens instead of max_gen_len', () => {
  418. const config = { max_new_tokens: 1000 };
  419. const prompt = 'Test prompt';
  420. const params = handler.params(config, prompt);
  421. expect(params).toHaveProperty('max_new_tokens', 1000);
  422. expect(params).not.toHaveProperty('max_gen_len');
  423. });
  424. });
  425. it('should throw an error for unsupported LLAMA version', () => {
  426. expect(() => getLlamaModelHandler(1 as LlamaVersion)).toThrow('Unsupported LLAMA version: 1');
  427. });
  428. it('should handle output correctly', () => {
  429. const handler = getLlamaModelHandler(LlamaVersion.V2);
  430. expect(handler.output({ generation: 'Test response' })).toBe('Test response');
  431. expect(handler.output({})).toBeUndefined();
  432. });
  433. });
  434. describe('formatPromptLlama2Chat', () => {
  435. it('should format a single user message correctly', () => {
  436. const messages: LlamaMessage[] = [
  437. {
  438. role: 'user',
  439. content: 'Describe the purpose of a "hello world" program in one sentence.',
  440. },
  441. ];
  442. const expectedPrompt =
  443. '<s>[INST] Describe the purpose of a "hello world" program in one sentence. [/INST]';
  444. expect(formatPromptLlama2Chat(messages)).toBe(expectedPrompt);
  445. });
  446. it('should handle a system message followed by a user message', () => {
  447. const messages: LlamaMessage[] = [
  448. { role: 'system', content: 'You are a helpful assistant.' },
  449. { role: 'user', content: 'What is the capital of France?' },
  450. ];
  451. const expectedPrompt = dedent`
  452. <s>[INST] <<SYS>>
  453. You are a helpful assistant.
  454. <</SYS>>
  455. What is the capital of France? [/INST]
  456. `;
  457. expect(formatPromptLlama2Chat(messages)).toBe(expectedPrompt);
  458. });
  459. it('should handle a system message, user message, and assistant response', () => {
  460. const messages: LlamaMessage[] = [
  461. { role: 'system', content: 'You are a helpful assistant.' },
  462. { role: 'user', content: 'What is the capital of France?' },
  463. { role: 'assistant', content: 'The capital of France is Paris.' },
  464. ];
  465. const expectedPrompt = dedent`
  466. <s>[INST] <<SYS>>
  467. You are a helpful assistant.
  468. <</SYS>>
  469. What is the capital of France? [/INST] The capital of France is Paris. </s>
  470. `;
  471. expect(formatPromptLlama2Chat(messages)).toBe(expectedPrompt);
  472. });
  473. it('should handle multiple turns of conversation', () => {
  474. // see https://huggingface.co/blog/llama2#how-to-prompt-llama-2
  475. const messages: LlamaMessage[] = [
  476. { role: 'system', content: 'You are a helpful assistant.' },
  477. { role: 'user', content: 'Hello' },
  478. { role: 'assistant', content: 'Hi there! How can I assist you today?' },
  479. { role: 'user', content: "What's the weather like?" },
  480. ];
  481. const expectedPrompt = dedent`
  482. <s>[INST] <<SYS>>
  483. You are a helpful assistant.
  484. <</SYS>>
  485. Hello [/INST] Hi there! How can I assist you today? </s><s>[INST] What's the weather like? [/INST]
  486. `;
  487. expect(formatPromptLlama2Chat(messages)).toBe(expectedPrompt);
  488. });
  489. it('should handle only a system message correctly', () => {
  490. const messages: LlamaMessage[] = [
  491. { role: 'system', content: 'You are a helpful assistant.' },
  492. ];
  493. const expectedPrompt = `${dedent`
  494. <s>[INST] <<SYS>>
  495. You are a helpful assistant.
  496. <</SYS>>
  497. `}\n\n`;
  498. expect(formatPromptLlama2Chat(messages)).toBe(expectedPrompt);
  499. });
  500. });
  501. describe('formatPromptLlama3Instruct', () => {
  502. it('should format a single user message correctly', () => {
  503. const messages: LlamaMessage[] = [{ role: 'user', content: 'Hello, how are you?' }];
  504. const expected = dedent`
  505. <|begin_of_text|><|start_header_id|>user<|end_header_id|>
  506. Hello, how are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|>`;
  507. expect(formatPromptLlama3Instruct(messages)).toBe(expected);
  508. });
  509. it('should format multiple messages correctly', () => {
  510. const messages: LlamaMessage[] = [
  511. { role: 'user', content: 'Hello' },
  512. { role: 'assistant', content: 'Hi there! How can I help you?' },
  513. { role: 'user', content: "What's the weather like?" },
  514. ];
  515. const expected = dedent`
  516. <|begin_of_text|><|start_header_id|>user<|end_header_id|>
  517. Hello<|eot_id|><|start_header_id|>assistant<|end_header_id|>
  518. Hi there! How can I help you?<|eot_id|><|start_header_id|>user<|end_header_id|>
  519. What's the weather like?<|eot_id|><|start_header_id|>assistant<|end_header_id|>`;
  520. expect(formatPromptLlama3Instruct(messages)).toBe(expected);
  521. });
  522. it('should handle system messages correctly', () => {
  523. const messages: LlamaMessage[] = [
  524. { role: 'system', content: 'You are a helpful assistant.' },
  525. { role: 'user', content: 'Hello' },
  526. ];
  527. const expected = dedent`
  528. <|begin_of_text|><|start_header_id|>system<|end_header_id|>
  529. You are a helpful assistant.<|eot_id|><|start_header_id|>user<|end_header_id|>
  530. Hello<|eot_id|><|start_header_id|>assistant<|end_header_id|>`;
  531. expect(formatPromptLlama3Instruct(messages)).toBe(expected);
  532. });
  533. });
  534. });
Tip!

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

Comments

Loading...