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

prompts.processors.python.utils.test.ts 2.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
  1. import { PythonShell } from 'python-shell';
  2. import logger from '../src/logger';
  3. import { pythonPromptFunction, pythonPromptFunctionLegacy } from '../src/prompts/processors/python';
  4. import { runPython } from '../src/python/pythonUtils';
  5. import type { ApiProvider } from '../src/types';
  6. jest.mock('fs');
  7. jest.mock('python-shell');
  8. jest.mock('../src/logger');
  9. jest.mock('../src/python/pythonUtils');
  10. describe('pythonPromptFunction', () => {
  11. interface PythonContext {
  12. vars: Record<string, string | object>;
  13. provider: ApiProvider;
  14. }
  15. it('should call python wrapper function with correct arguments', async () => {
  16. const filePath = 'path/to/script.py';
  17. const functionName = 'testFunction';
  18. const context = {
  19. vars: { key: 'value' },
  20. provider: {
  21. id: () => 'providerId',
  22. label: 'providerLabel',
  23. callApi: jest.fn(),
  24. } as ApiProvider,
  25. } as PythonContext;
  26. const mockRunPython = jest.mocked(runPython);
  27. mockRunPython.mockResolvedValue('mocked result');
  28. await expect(pythonPromptFunction(filePath, functionName, context)).resolves.toBe(
  29. 'mocked result',
  30. );
  31. expect(mockRunPython).toHaveBeenCalledWith(filePath, functionName, [
  32. {
  33. ...context,
  34. provider: {
  35. id: 'providerId',
  36. label: 'providerLabel',
  37. },
  38. },
  39. ]);
  40. });
  41. it('should call legacy function with correct arguments', async () => {
  42. const filePath = 'path/to/script.py';
  43. const context = {
  44. vars: { key: 'value' },
  45. provider: { id: () => 'providerId', label: 'providerLabel' } as ApiProvider,
  46. } as PythonContext;
  47. const mockPythonShellRun = jest.mocked(PythonShell.run);
  48. const mockLoggerDebug = jest.mocked(logger.debug);
  49. mockPythonShellRun.mockImplementation(() => {
  50. return Promise.resolve(['mocked result']);
  51. });
  52. await expect(pythonPromptFunctionLegacy(filePath, context)).resolves.toBe('mocked result');
  53. expect(mockPythonShellRun).toHaveBeenCalledWith(filePath, {
  54. mode: 'text',
  55. pythonPath: process.env.PROMPTFOO_PYTHON || 'python',
  56. args: [
  57. JSON.stringify({
  58. vars: context.vars,
  59. provider: {
  60. id: context.provider.id(),
  61. label: context.provider.label,
  62. },
  63. }),
  64. ],
  65. });
  66. expect(mockLoggerDebug).toHaveBeenCalledWith(`Executing python prompt script ${filePath}`);
  67. expect(mockLoggerDebug).toHaveBeenCalledWith(
  68. `Python prompt script ${filePath} returned: mocked result`,
  69. );
  70. });
  71. });
Tip!

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

Comments

Loading...