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

simulatedUser.test.ts 6.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  1. import { SimulatedUser } from '../../src/providers/simulatedUser';
  2. import * as timeUtils from '../../src/util/time';
  3. import type { ApiProvider } from '../../src/types';
  4. jest.mock('../../src/util/time', () => ({
  5. sleep: jest.fn().mockResolvedValue(undefined),
  6. }));
  7. jest.mock('../../src/fetch');
  8. // Mock PromptfooSimulatedUserProvider
  9. const mockUserProviderCallApi = jest.fn().mockResolvedValue({ output: 'user response' });
  10. jest.mock('../../src/providers/promptfoo', () => {
  11. return {
  12. PromptfooSimulatedUserProvider: jest.fn().mockImplementation(() => ({
  13. callApi: mockUserProviderCallApi,
  14. id: jest.fn().mockReturnValue('mock-user-provider'),
  15. options: {},
  16. })),
  17. };
  18. });
  19. describe('SimulatedUser', () => {
  20. let simulatedUser: SimulatedUser;
  21. let originalProvider: ApiProvider;
  22. beforeEach(() => {
  23. mockUserProviderCallApi.mockClear();
  24. mockUserProviderCallApi.mockResolvedValue({ output: 'user response' });
  25. originalProvider = {
  26. id: () => 'test-agent',
  27. callApi: jest.fn().mockImplementation(async () => ({
  28. output: 'agent response',
  29. tokenUsage: { numRequests: 1 },
  30. })),
  31. };
  32. simulatedUser = new SimulatedUser({
  33. id: 'test-agent',
  34. config: {
  35. instructions: 'test instructions',
  36. maxTurns: 2,
  37. },
  38. });
  39. jest.clearAllMocks();
  40. });
  41. describe('id()', () => {
  42. it('should return the identifier', () => {
  43. expect(simulatedUser.id()).toBe('test-agent');
  44. });
  45. it('should use label as fallback identifier', () => {
  46. const userWithLabel = new SimulatedUser({
  47. label: 'label-agent',
  48. config: {},
  49. });
  50. expect(userWithLabel.id()).toBe('label-agent');
  51. });
  52. it('should use default identifier if no id or label provided', () => {
  53. const userWithoutId = new SimulatedUser({ config: {} });
  54. expect(userWithoutId.id()).toBe('agent-provider');
  55. });
  56. });
  57. describe('callApi()', () => {
  58. it('should simulate conversation between user and agent', async () => {
  59. const result = await simulatedUser.callApi('test prompt', {
  60. originalProvider,
  61. vars: { instructions: 'test instructions' },
  62. prompt: { raw: 'test', display: 'test', label: 'test' },
  63. });
  64. expect(result.output).toBeDefined();
  65. expect(result.output).toContain('User:');
  66. expect(result.output).toContain('Assistant:');
  67. expect(result.tokenUsage?.numRequests).toBe(2);
  68. expect(originalProvider.callApi).toHaveBeenCalledTimes(2);
  69. expect(timeUtils.sleep).not.toHaveBeenCalled();
  70. });
  71. it('should respect maxTurns configuration', async () => {
  72. const userWithMaxTurns = new SimulatedUser({
  73. config: {
  74. instructions: 'test instructions',
  75. maxTurns: 1,
  76. },
  77. });
  78. const result = await userWithMaxTurns.callApi('test prompt', {
  79. originalProvider,
  80. vars: { instructions: 'test instructions' },
  81. prompt: { raw: 'test', display: 'test', label: 'test' },
  82. });
  83. const messageCount = result.output?.split('---').length;
  84. expect(messageCount).toBe(2);
  85. expect(originalProvider.callApi).toHaveBeenCalledTimes(1);
  86. expect(timeUtils.sleep).not.toHaveBeenCalled();
  87. });
  88. it('should stop conversation when ###STOP### is received', async () => {
  89. // Set up an initial message exchange to have some conversation history
  90. // First call is regular exchange
  91. const mockedCallApi = jest.mocked(originalProvider.callApi);
  92. mockedCallApi.mockImplementationOnce(async () => ({
  93. output: 'initial agent response',
  94. tokenUsage: { numRequests: 1 },
  95. }));
  96. // Second call returns stop command
  97. mockUserProviderCallApi
  98. .mockResolvedValueOnce({ output: 'initial user response' }) // First user response
  99. .mockResolvedValueOnce({ output: 'stopping now ###STOP###' }); // Second user response with STOP
  100. const result = await simulatedUser.callApi('test prompt', {
  101. originalProvider,
  102. vars: { instructions: 'test instructions' },
  103. prompt: { raw: 'test', display: 'test', label: 'test' },
  104. });
  105. expect(result.output).not.toContain('stopping now ###STOP###');
  106. // The original provider should be called once for the first exchange
  107. expect(originalProvider.callApi).toHaveBeenCalledTimes(1);
  108. expect(timeUtils.sleep).not.toHaveBeenCalled();
  109. });
  110. it('should throw error if originalProvider is not provided', async () => {
  111. await expect(
  112. simulatedUser.callApi('test', {
  113. vars: {},
  114. prompt: { raw: 'test', display: 'test', label: 'test' },
  115. }),
  116. ).rejects.toThrow('Expected originalProvider to be set');
  117. });
  118. it('should pass context with vars to the target provider', async () => {
  119. const testContext = {
  120. originalProvider,
  121. vars: {
  122. workflow_id: '123-workflow',
  123. session_id: '123-session',
  124. instructions: 'test instructions',
  125. },
  126. prompt: { raw: 'test', display: 'test', label: 'test' },
  127. };
  128. const result = await simulatedUser.callApi('test prompt', testContext);
  129. expect(result.output).toBeDefined();
  130. expect(originalProvider.callApi).toHaveBeenCalledWith(
  131. expect.stringContaining('[{"role":"user","content":"user response"}'),
  132. expect.objectContaining({
  133. vars: expect.objectContaining({
  134. workflow_id: '123-workflow',
  135. session_id: '123-session',
  136. instructions: 'test instructions',
  137. }),
  138. }),
  139. );
  140. });
  141. it('should handle provider delay', async () => {
  142. const providerWithDelay = {
  143. ...originalProvider,
  144. delay: 100,
  145. };
  146. const result = await simulatedUser.callApi(
  147. 'test prompt',
  148. {
  149. originalProvider: providerWithDelay,
  150. vars: { instructions: 'test instructions' },
  151. prompt: { raw: 'test', display: 'test', label: 'test' },
  152. },
  153. { includeLogProbs: false },
  154. );
  155. expect(result.output).toBeDefined();
  156. expect(providerWithDelay.callApi).toHaveBeenCalledTimes(2);
  157. expect(timeUtils.sleep).toHaveBeenCalledWith(100);
  158. });
  159. });
  160. describe('toString()', () => {
  161. it('should return correct string representation', () => {
  162. expect(simulatedUser.toString()).toBe('AgentProvider');
  163. });
  164. });
  165. });
Tip!

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

Comments

Loading...