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

promptfooModel.test.ts 4.2 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
  1. import { cloudConfig } from '../../src/globalConfig/cloud';
  2. import logger from '../../src/logger';
  3. import { PromptfooModelProvider } from '../../src/providers/promptfooModel';
  4. describe('PromptfooModelProvider', () => {
  5. let mockFetch: jest.Mock;
  6. let mockCloudConfig: jest.SpyInstance;
  7. const mockLogger = jest.spyOn(logger, 'debug').mockImplementation();
  8. beforeEach(() => {
  9. mockFetch = jest.fn();
  10. global.fetch = mockFetch;
  11. mockCloudConfig = jest.spyOn(cloudConfig, 'getApiKey').mockReturnValue('test-token');
  12. mockLogger.mockClear();
  13. });
  14. afterEach(() => {
  15. jest.resetAllMocks();
  16. });
  17. it('should initialize with model name', () => {
  18. const provider = new PromptfooModelProvider('test-model');
  19. expect(provider.id()).toBe('promptfoo:model:test-model');
  20. });
  21. it('should throw error if model name is not provided', () => {
  22. expect(() => new PromptfooModelProvider('')).toThrow('Model name is required');
  23. });
  24. it('should call API with string prompt', async () => {
  25. const provider = new PromptfooModelProvider('test-model');
  26. const mockResponse = {
  27. ok: true,
  28. json: () =>
  29. Promise.resolve({
  30. result: {
  31. choices: [{ message: { content: 'test response' } }],
  32. usage: {
  33. total_tokens: 10,
  34. prompt_tokens: 5,
  35. completion_tokens: 5,
  36. },
  37. },
  38. }),
  39. };
  40. mockFetch.mockResolvedValue(mockResponse);
  41. const result = await provider.callApi('test prompt');
  42. expect(result).toEqual({
  43. output: 'test response',
  44. tokenUsage: {
  45. total: 10,
  46. prompt: 5,
  47. completion: 5,
  48. },
  49. });
  50. });
  51. it('should handle JSON array messages', async () => {
  52. const provider = new PromptfooModelProvider('test-model');
  53. const messages = JSON.stringify([
  54. { role: 'user', content: 'Hello' },
  55. { role: 'assistant', content: 'Hi' },
  56. ]);
  57. const mockResponse = {
  58. ok: true,
  59. json: () =>
  60. Promise.resolve({
  61. result: {
  62. choices: [{ message: { content: 'test response' } }],
  63. usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
  64. },
  65. }),
  66. };
  67. mockFetch.mockResolvedValue(mockResponse);
  68. await provider.callApi(messages);
  69. expect(mockFetch).toHaveBeenCalledWith(
  70. expect.any(String),
  71. expect.objectContaining({
  72. body: expect.stringContaining(
  73. '"messages":[{"role":"user","content":"Hello"},{"role":"assistant","content":"Hi"}]',
  74. ),
  75. }),
  76. );
  77. });
  78. it('should throw error if no auth token', async () => {
  79. mockCloudConfig.mockReturnValue(undefined);
  80. const provider = new PromptfooModelProvider('test-model');
  81. await expect(provider.callApi('test')).rejects.toThrow('No Promptfoo auth token available');
  82. });
  83. it('should handle API errors', async () => {
  84. const provider = new PromptfooModelProvider('test-model');
  85. mockFetch.mockResolvedValue({
  86. ok: false,
  87. status: 500,
  88. text: () => Promise.resolve('Internal server error'),
  89. });
  90. await expect(provider.callApi('test')).rejects.toThrow('PromptfooModel task API error: 500');
  91. });
  92. it('should handle invalid API responses', async () => {
  93. const provider = new PromptfooModelProvider('test-model');
  94. mockFetch.mockResolvedValue({
  95. ok: true,
  96. json: () => Promise.resolve({}),
  97. });
  98. await expect(provider.callApi('test')).rejects.toThrow(
  99. 'Invalid response from PromptfooModel task API',
  100. );
  101. });
  102. it('should use config from options', async () => {
  103. const config = { temperature: 0.7 };
  104. const provider = new PromptfooModelProvider('test-model', { model: 'test-model', config });
  105. const mockResponse = {
  106. ok: true,
  107. json: () =>
  108. Promise.resolve({
  109. result: {
  110. choices: [{ message: { content: 'test' } }],
  111. usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
  112. },
  113. }),
  114. };
  115. mockFetch.mockResolvedValue(mockResponse);
  116. await provider.callApi('test');
  117. expect(mockFetch).toHaveBeenCalledWith(
  118. expect.any(String),
  119. expect.objectContaining({
  120. body: expect.stringContaining('"config":{"temperature":0.7}'),
  121. }),
  122. );
  123. });
  124. });
Tip!

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

Comments

Loading...