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

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

Comments

Loading...