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

pi.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
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
  1. import { handlePiScorer } from '../../src/assertions/pi';
  2. import { matchesClosedQa, matchesPiScore } from '../../src/matchers';
  3. import { getNunjucksEngine } from '../../src/util/templates';
  4. import type nunjucks from 'nunjucks';
  5. import type { AssertionParams } from '../../src/types';
  6. jest.mock('../../src/matchers');
  7. jest.mock('../../src/util/templates');
  8. describe('handlePiScorer', () => {
  9. beforeEach(() => {
  10. jest.resetAllMocks();
  11. const mockNunjucksEnv = {
  12. options: { autoescape: true },
  13. render: jest.fn(),
  14. renderString: jest.fn().mockImplementation((str) => str),
  15. addFilter: jest.fn(),
  16. getFilter: jest.fn(),
  17. hasExtension: jest.fn(),
  18. addExtension: jest.fn(),
  19. removeExtension: jest.fn(),
  20. getExtension: jest.fn(),
  21. addGlobal: jest.fn(),
  22. getGlobal: jest.fn(),
  23. getTemplate: jest.fn(),
  24. express: jest.fn(),
  25. on: jest.fn(),
  26. } as unknown as nunjucks.Environment;
  27. jest.mocked(getNunjucksEngine).mockReturnValue(mockNunjucksEnv);
  28. jest.mocked(matchesClosedQa).mockResolvedValue({
  29. pass: true,
  30. score: 1,
  31. reason: 'test reason',
  32. });
  33. });
  34. it('should validate string value', async () => {
  35. const params: AssertionParams = {
  36. assertion: { type: 'pi' },
  37. baseType: 'pi',
  38. context: {
  39. prompt: 'test prompt',
  40. vars: {},
  41. test: { vars: {} },
  42. logProbs: undefined,
  43. provider: undefined,
  44. providerResponse: undefined,
  45. },
  46. inverse: false,
  47. output: 'test output',
  48. outputString: 'test output',
  49. prompt: 'test prompt',
  50. providerResponse: {},
  51. renderedValue: {},
  52. test: {
  53. options: {},
  54. vars: {},
  55. },
  56. };
  57. await expect(handlePiScorer(params)).rejects.toThrow(
  58. '"pi" assertion type must have a string value',
  59. );
  60. });
  61. it('should validate prompt exists', async () => {
  62. const params: AssertionParams = {
  63. assertion: { type: 'pi' },
  64. baseType: 'pi',
  65. context: {
  66. prompt: undefined,
  67. vars: {},
  68. test: { vars: {} },
  69. logProbs: undefined,
  70. provider: undefined,
  71. providerResponse: undefined,
  72. },
  73. inverse: false,
  74. output: 'test output',
  75. outputString: 'test output',
  76. prompt: undefined,
  77. providerResponse: {},
  78. renderedValue: 'test value',
  79. test: {
  80. options: {},
  81. vars: {},
  82. },
  83. };
  84. await expect(handlePiScorer(params)).rejects.toThrow(
  85. '"pi" assertion must have a prompt that is a string',
  86. );
  87. });
  88. it('should call handlePiScorer with correct parameters', async () => {
  89. const params: AssertionParams = {
  90. assertion: { type: 'pi', value: 'test question' },
  91. baseType: 'pi',
  92. context: {
  93. prompt: 'test prompt',
  94. vars: { var: 'value' },
  95. test: { vars: { var: 'value' } },
  96. logProbs: undefined,
  97. provider: undefined,
  98. providerResponse: undefined,
  99. },
  100. inverse: false,
  101. output: 'test output',
  102. outputString: 'test output',
  103. prompt: 'test prompt',
  104. providerResponse: {},
  105. renderedValue: 'test question',
  106. test: {
  107. options: {
  108. rubricPrompt: 'test rubric',
  109. },
  110. vars: {
  111. var: 'value',
  112. },
  113. },
  114. };
  115. await handlePiScorer(params);
  116. expect(matchesPiScore).toHaveBeenCalledWith('test question', 'test prompt', 'test output', {
  117. type: 'pi',
  118. value: 'test question',
  119. });
  120. });
  121. });
Tip!

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

Comments

Loading...