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

rouge.test.ts 3.9 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
  1. import * as rouge from 'js-rouge';
  2. import { handleRougeScore } from '../../src/assertions/rouge';
  3. import type { Assertion, AssertionParams } from '../../src/types';
  4. jest.mock('js-rouge', () => ({
  5. n: jest.fn(),
  6. l: jest.fn(),
  7. s: jest.fn(),
  8. }));
  9. describe('handleRougeScore', () => {
  10. beforeEach(() => {
  11. jest.resetAllMocks();
  12. });
  13. const mockAssertion: Assertion = {
  14. type: 'rouge-n',
  15. value: 'expected text',
  16. };
  17. const baseParams: AssertionParams = {
  18. baseType: 'rouge-n' as any,
  19. assertion: mockAssertion,
  20. renderedValue: 'expected text',
  21. outputString: 'actual text',
  22. inverse: false,
  23. context: {
  24. prompt: 'test prompt',
  25. vars: {},
  26. test: { assert: [mockAssertion] },
  27. logProbs: undefined,
  28. provider: undefined,
  29. providerResponse: {
  30. raw: 'actual text',
  31. error: undefined,
  32. cached: false,
  33. cost: 0,
  34. tokenUsage: {},
  35. },
  36. },
  37. output: { text: 'actual text' },
  38. providerResponse: {
  39. raw: 'actual text',
  40. error: undefined,
  41. cached: false,
  42. cost: 0,
  43. tokenUsage: {},
  44. },
  45. test: { assert: [mockAssertion] },
  46. };
  47. it('should pass when score is above default threshold', () => {
  48. jest.mocked(rouge.n).mockReturnValue(0.8);
  49. const result = handleRougeScore(baseParams);
  50. expect(result.pass).toBe(true);
  51. expect(result.score).toBe(0.8);
  52. expect(result.reason).toBe('ROUGE-N score 0.80 is greater than or equal to threshold 0.75');
  53. expect(rouge.n).toHaveBeenCalledWith('actual text', 'expected text', {});
  54. });
  55. it('should fail when score is below default threshold', () => {
  56. jest.mocked(rouge.n).mockReturnValue(0.7);
  57. const result = handleRougeScore(baseParams);
  58. expect(result.pass).toBe(false);
  59. expect(result.score).toBe(0.7);
  60. expect(result.reason).toBe('ROUGE-N score 0.70 is less than threshold 0.75');
  61. expect(rouge.n).toHaveBeenCalledWith('actual text', 'expected text', {});
  62. });
  63. it('should use custom threshold when provided', () => {
  64. jest.mocked(rouge.n).mockReturnValue(0.6);
  65. const result = handleRougeScore({
  66. ...baseParams,
  67. assertion: { ...mockAssertion, threshold: 0.5 },
  68. });
  69. expect(result.pass).toBe(true);
  70. expect(result.score).toBe(0.6);
  71. expect(result.reason).toBe('ROUGE-N score 0.60 is greater than or equal to threshold 0.5');
  72. expect(rouge.n).toHaveBeenCalledWith('actual text', 'expected text', {});
  73. });
  74. it('should handle inverse scoring', () => {
  75. jest.mocked(rouge.n).mockReturnValue(0.8);
  76. const result = handleRougeScore({
  77. ...baseParams,
  78. inverse: true,
  79. });
  80. expect(result.pass).toBe(false);
  81. expect(result.score).toBeCloseTo(0.2, 5);
  82. expect(result.reason).toBe('ROUGE-N score 0.80 is less than threshold 0.75');
  83. expect(rouge.n).toHaveBeenCalledWith('actual text', 'expected text', {});
  84. });
  85. it('should use ROUGE-L method', () => {
  86. jest.mocked(rouge.l).mockReturnValue(0.8);
  87. const result = handleRougeScore({
  88. ...baseParams,
  89. baseType: 'rouge-l' as any,
  90. });
  91. expect(rouge.l).toHaveBeenCalledWith('actual text', 'expected text', {});
  92. expect(result.pass).toBe(true);
  93. expect(result.reason).toBe('ROUGE-L score 0.80 is greater than or equal to threshold 0.75');
  94. });
  95. it('should use ROUGE-S method', () => {
  96. jest.mocked(rouge.s).mockReturnValue(0.8);
  97. const result = handleRougeScore({
  98. ...baseParams,
  99. baseType: 'rouge-s' as any,
  100. });
  101. expect(rouge.s).toHaveBeenCalledWith('actual text', 'expected text', {});
  102. expect(result.pass).toBe(true);
  103. expect(result.reason).toBe('ROUGE-S score 0.80 is greater than or equal to threshold 0.75');
  104. });
  105. it('should throw error if renderedValue is not a string', () => {
  106. expect(() =>
  107. handleRougeScore({
  108. ...baseParams,
  109. renderedValue: 123 as any,
  110. }),
  111. ).toThrow('"rouge" assertion type must be a string value');
  112. });
  113. });
Tip!

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

Comments

Loading...