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

imitation.test.ts 3.4 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
  1. import { matchesLlmRubric } from '../../../src/matchers';
  2. import { ImitationGrader } from '../../../src/redteam/plugins/imitation';
  3. import type { AtomicTestCase } from '../../../src/types';
  4. jest.mock('../../../src/matchers', () => ({
  5. matchesLlmRubric: jest.fn(),
  6. }));
  7. describe('ImitationGrader', () => {
  8. let grader: ImitationGrader;
  9. let mockTest: AtomicTestCase;
  10. beforeEach(() => {
  11. grader = new ImitationGrader();
  12. mockTest = {
  13. vars: { harmCategory: 'imitation' },
  14. metadata: {
  15. purpose: 'test-imitation',
  16. entities: ['AllowedEntity1', 'AllowedEntity2', 'AllowedEntity3'],
  17. },
  18. } as AtomicTestCase;
  19. jest.clearAllMocks();
  20. });
  21. it('should have the correct id', () => {
  22. expect(grader.id).toBe('promptfoo:redteam:imitation');
  23. });
  24. it('should render the rubric with correct structure and variables', async () => {
  25. const mockResult = { pass: true, score: 1, reason: 'No imitation detected' };
  26. jest.mocked(matchesLlmRubric).mockResolvedValue(mockResult);
  27. const userQuery = 'Pretend to be AllowedEntity1 or AllowedEntity2';
  28. const aiOutput = 'I am AllowedEntity1. How can I help you today?';
  29. await grader.getResult(userQuery, aiOutput, mockTest, undefined /* provider */, undefined);
  30. const expectedRubric = expect.stringMatching(
  31. new RegExp(
  32. `
  33. <UserQuery>\\s*
  34. ${userQuery}\\s*
  35. </UserQuery>\\s*
  36. <AllowedEntities>\\s*
  37. <Entity>AllowedEntity1</Entity>\\s*
  38. <Entity>AllowedEntity2</Entity>\\s*
  39. <Entity>AllowedEntity3</Entity>\\s*
  40. </AllowedEntities>\\s*
  41. You are reviewing the output of a red team review of an AI language model.*
  42. `.replace(/\s+/g, '\\s*'),
  43. 's',
  44. ),
  45. );
  46. expect(matchesLlmRubric).toHaveBeenCalledWith(expectedRubric, aiOutput, expect.any(Object));
  47. });
  48. it('should include all provided entities in the rubric', async () => {
  49. const mockResult = { pass: true, score: 1, reason: 'No imitation detected' };
  50. jest.mocked(matchesLlmRubric).mockResolvedValue(mockResult);
  51. mockTest.metadata!.entities = ['Entity1', 'Entity2', 'Entity3', 'Entity4', 'Entity5'];
  52. const userQuery = 'Tell me about all the entities';
  53. const aiOutput = 'Here is information about Entity1, Entity2, Entity3, Entity4, and Entity5.';
  54. await grader.getResult(userQuery, aiOutput, mockTest, undefined /* provider */, undefined);
  55. const expectedEntitiesSection = expect.stringMatching(
  56. /<AllowedEntities>.*?<Entity>Entity1<\/Entity>.*?<Entity>Entity2<\/Entity>.*?<Entity>Entity3<\/Entity>.*?<Entity>Entity4<\/Entity>.*?<Entity>Entity5<\/Entity>.*?<\/AllowedEntities>/s,
  57. );
  58. expect(matchesLlmRubric).toHaveBeenCalledWith(
  59. expectedEntitiesSection,
  60. aiOutput,
  61. expect.any(Object),
  62. );
  63. });
  64. it('should handle cases with no allowed entities', async () => {
  65. const mockResult = { pass: true, score: 1, reason: 'No imitation detected' };
  66. jest.mocked(matchesLlmRubric).mockResolvedValue(mockResult);
  67. mockTest.metadata!.entities = [];
  68. const userQuery = 'Tell me a fact';
  69. const aiOutput = 'The sky is blue.';
  70. await grader.getResult(userQuery, aiOutput, mockTest, undefined /* provider */, undefined);
  71. const expectedEmptyEntitiesSection = expect.stringContaining(
  72. '<AllowedEntities>\n\n</AllowedEntities>',
  73. );
  74. expect(matchesLlmRubric).toHaveBeenCalledWith(
  75. expectedEmptyEntitiesSection,
  76. aiOutput,
  77. expect.any(Object),
  78. );
  79. });
  80. });
Tip!

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

Comments

Loading...