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

moderation.test.ts 2.3 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
  1. import { handleModeration } from '../../src/assertions/moderation';
  2. import { matchesModeration } from '../../src/matchers';
  3. import type {
  4. ApiProvider,
  5. Assertion,
  6. AssertionParams,
  7. AssertionValueFunctionContext,
  8. TestCase,
  9. } from '../../src/types';
  10. jest.mock('../../src/matchers', () => ({
  11. matchesModeration: jest.fn(),
  12. }));
  13. const mockedMatchesModeration = jest.mocked(matchesModeration);
  14. describe('handleModeration', () => {
  15. const mockTest: TestCase = {
  16. description: 'Test case',
  17. vars: {},
  18. assert: [],
  19. options: {},
  20. };
  21. const mockAssertion: Assertion = {
  22. type: 'moderation',
  23. value: ['harassment'],
  24. };
  25. const mockProvider: ApiProvider = {
  26. id: () => 'test-provider',
  27. config: {},
  28. callApi: jest.fn(),
  29. };
  30. const mockContext: AssertionValueFunctionContext = {
  31. prompt: 'test prompt',
  32. vars: {},
  33. test: mockTest,
  34. logProbs: undefined,
  35. provider: mockProvider,
  36. providerResponse: { output: 'output' },
  37. };
  38. const baseParams: AssertionParams = {
  39. assertion: mockAssertion,
  40. test: mockTest,
  41. outputString: 'output',
  42. prompt: 'prompt',
  43. baseType: 'moderation',
  44. context: mockContext,
  45. inverse: false,
  46. output: 'output',
  47. providerResponse: { output: 'output' },
  48. };
  49. beforeEach(() => {
  50. jest.clearAllMocks();
  51. });
  52. it('should pass moderation check', async () => {
  53. mockedMatchesModeration.mockResolvedValue({
  54. pass: true,
  55. score: 1,
  56. reason: 'Safe content',
  57. });
  58. const result = await handleModeration({
  59. ...baseParams,
  60. providerResponse: { output: 'output' },
  61. });
  62. expect(result).toEqual({
  63. pass: true,
  64. score: 1,
  65. reason: 'Safe content',
  66. assertion: mockAssertion,
  67. });
  68. });
  69. it('should use redteam final prompt when available', async () => {
  70. mockedMatchesModeration.mockResolvedValue({
  71. pass: true,
  72. score: 1,
  73. reason: 'Safe content',
  74. });
  75. await handleModeration({
  76. ...baseParams,
  77. providerResponse: {
  78. output: 'output',
  79. metadata: { redteamFinalPrompt: 'modified prompt' },
  80. },
  81. });
  82. expect(mockedMatchesModeration).toHaveBeenCalledWith(
  83. {
  84. userPrompt: 'modified prompt',
  85. assistantResponse: 'output',
  86. categories: ['harassment'],
  87. },
  88. {},
  89. );
  90. });
  91. });
Tip!

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

Comments

Loading...