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

azure.test.ts 8.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  1. import { AzureCompletionProvider } from '../../src/providers/azure';
  2. import { AzureGenericProvider } from '../../src/providers/azure';
  3. import { AzureChatCompletionProvider } from '../../src/providers/azure';
  4. import { maybeEmitAzureOpenAiWarning } from '../../src/providers/azureUtil';
  5. import { HuggingfaceTextGenerationProvider } from '../../src/providers/huggingface';
  6. import { OpenAiCompletionProvider } from '../../src/providers/openai';
  7. import type { TestSuite, TestCase, CallApiContextParams } from '../../src/types';
  8. jest.mock('../../src/logger');
  9. describe('maybeEmitAzureOpenAiWarning', () => {
  10. it('should not emit warning when no Azure providers are used', () => {
  11. const testSuite: TestSuite = {
  12. providers: [new OpenAiCompletionProvider('foo')],
  13. defaultTest: {},
  14. prompts: [],
  15. };
  16. const tests: TestCase[] = [
  17. {
  18. assert: [{ type: 'llm-rubric', value: 'foo bar' }],
  19. },
  20. ];
  21. const result = maybeEmitAzureOpenAiWarning(testSuite, tests);
  22. expect(result).toBe(false);
  23. });
  24. it('should not emit warning when Azure provider is used alone, but no model graded eval', () => {
  25. const testSuite: TestSuite = {
  26. providers: [new AzureCompletionProvider('foo')],
  27. defaultTest: {},
  28. prompts: [],
  29. };
  30. const tests: TestCase[] = [
  31. {
  32. assert: [{ type: 'equals' }],
  33. },
  34. ];
  35. const result = maybeEmitAzureOpenAiWarning(testSuite, tests);
  36. expect(result).toBe(false);
  37. });
  38. it('should emit warning when Azure provider is used alone, but with model graded eval', () => {
  39. const testSuite: TestSuite = {
  40. providers: [new AzureCompletionProvider('foo')],
  41. defaultTest: {},
  42. prompts: [],
  43. };
  44. const tests: TestCase[] = [
  45. {
  46. assert: [{ type: 'llm-rubric', value: 'foo bar' }],
  47. },
  48. ];
  49. const result = maybeEmitAzureOpenAiWarning(testSuite, tests);
  50. expect(result).toBe(true);
  51. });
  52. it('should emit warning when Azure provider used with non-OpenAI provider', () => {
  53. const testSuite: TestSuite = {
  54. providers: [new AzureCompletionProvider('foo'), new HuggingfaceTextGenerationProvider('bar')],
  55. defaultTest: {},
  56. prompts: [],
  57. };
  58. const tests: TestCase[] = [
  59. {
  60. assert: [{ type: 'llm-rubric', value: 'foo bar' }],
  61. },
  62. ];
  63. const result = maybeEmitAzureOpenAiWarning(testSuite, tests);
  64. expect(result).toBe(true);
  65. });
  66. it('should not emit warning when Azure providers are used with a default provider set', () => {
  67. const testSuite: TestSuite = {
  68. providers: [new AzureCompletionProvider('foo')],
  69. defaultTest: { options: { provider: 'azureopenai:....' } },
  70. prompts: [],
  71. };
  72. const tests: TestCase[] = [
  73. {
  74. assert: [{ type: 'llm-rubric', value: 'foo bar' }],
  75. },
  76. ];
  77. const result = maybeEmitAzureOpenAiWarning(testSuite, tests);
  78. expect(result).toBe(false);
  79. });
  80. it('should not emit warning when both Azure and OpenAI providers are used', () => {
  81. const testSuite: TestSuite = {
  82. providers: [new AzureCompletionProvider('foo'), new OpenAiCompletionProvider('bar')],
  83. defaultTest: {},
  84. prompts: [],
  85. };
  86. const tests: TestCase[] = [
  87. {
  88. assert: [{ type: 'llm-rubric', value: 'foo bar' }],
  89. },
  90. ];
  91. const result = maybeEmitAzureOpenAiWarning(testSuite, tests);
  92. expect(result).toBe(false);
  93. });
  94. });
  95. describe('AzureOpenAiGenericProvider', () => {
  96. describe('getApiBaseUrl', () => {
  97. beforeEach(() => {
  98. delete process.env.AZURE_OPENAI_API_HOST;
  99. });
  100. it('should return apiBaseUrl if set', () => {
  101. const provider = new AzureGenericProvider('test-deployment', {
  102. config: { apiBaseUrl: 'https://custom.azure.com' },
  103. });
  104. expect(provider.getApiBaseUrl()).toBe('https://custom.azure.com');
  105. });
  106. it('should return apiBaseUrl without trailing slash if set', () => {
  107. const provider = new AzureGenericProvider('test-deployment', {
  108. config: { apiBaseUrl: 'https://custom.azure.com/' },
  109. });
  110. expect(provider.getApiBaseUrl()).toBe('https://custom.azure.com');
  111. });
  112. it('should construct URL from apiHost without protocol', () => {
  113. const provider = new AzureGenericProvider('test-deployment', {
  114. config: { apiHost: 'api.azure.com' },
  115. });
  116. expect(provider.getApiBaseUrl()).toBe('https://api.azure.com');
  117. });
  118. it('should remove protocol from apiHost if present', () => {
  119. const provider = new AzureGenericProvider('test-deployment', {
  120. config: { apiHost: 'https://api.azure.com' },
  121. });
  122. expect(provider.getApiBaseUrl()).toBe('https://api.azure.com');
  123. });
  124. it('should remove trailing slash from apiHost if present', () => {
  125. const provider = new AzureGenericProvider('test-deployment', {
  126. config: { apiHost: 'api.azure.com/' },
  127. });
  128. expect(provider.getApiBaseUrl()).toBe('https://api.azure.com');
  129. });
  130. it('should return undefined if neither apiBaseUrl nor apiHost is set', () => {
  131. const provider = new AzureGenericProvider('test-deployment', {});
  132. expect(provider.getApiBaseUrl()).toBeUndefined();
  133. });
  134. });
  135. });
  136. describe('AzureOpenAiChatCompletionProvider', () => {
  137. describe('config merging', () => {
  138. let provider: AzureChatCompletionProvider;
  139. beforeEach(() => {
  140. provider = new AzureChatCompletionProvider('test-deployment', {
  141. config: {
  142. apiHost: 'test.azure.com',
  143. apiKey: 'test-key',
  144. functions: [{ name: 'provider_func', parameters: {} }],
  145. max_tokens: 100,
  146. temperature: 0.5,
  147. },
  148. });
  149. });
  150. it('should use provider config when no prompt config exists', async () => {
  151. const context: CallApiContextParams = {
  152. prompt: {
  153. label: 'test prompt',
  154. raw: 'test prompt',
  155. },
  156. vars: {},
  157. };
  158. expect((provider as any).getOpenAiBody('test prompt', context)).toMatchObject({
  159. functions: [{ name: 'provider_func', parameters: {} }],
  160. max_tokens: 100,
  161. temperature: 0.5,
  162. });
  163. });
  164. it('should merge prompt config with provider config', async () => {
  165. const context: CallApiContextParams = {
  166. prompt: {
  167. config: {
  168. functions: [{ name: 'prompt_func', parameters: {} }],
  169. temperature: 0.7,
  170. },
  171. label: 'test prompt',
  172. raw: 'test prompt',
  173. },
  174. vars: {},
  175. };
  176. expect((provider as any).getOpenAiBody('test prompt', context)).toMatchObject({
  177. functions: [{ name: 'prompt_func', parameters: {} }],
  178. max_tokens: 100,
  179. temperature: 0.7,
  180. });
  181. });
  182. it('should handle undefined prompt config', async () => {
  183. const context: CallApiContextParams = {
  184. prompt: {
  185. label: 'test prompt',
  186. raw: 'test prompt',
  187. },
  188. vars: {},
  189. };
  190. expect((provider as any).getOpenAiBody('test prompt', context)).toMatchObject({
  191. functions: [{ name: 'provider_func', parameters: {} }],
  192. max_tokens: 100,
  193. temperature: 0.5,
  194. });
  195. });
  196. it('should handle empty prompt config', async () => {
  197. const context: CallApiContextParams = {
  198. prompt: {
  199. config: {},
  200. label: 'test prompt',
  201. raw: 'test prompt',
  202. },
  203. vars: {},
  204. };
  205. expect((provider as any).getOpenAiBody('test prompt', context)).toMatchObject({
  206. functions: [{ name: 'provider_func', parameters: {} }],
  207. max_tokens: 100,
  208. temperature: 0.5,
  209. });
  210. });
  211. it('should handle complex nested config merging', async () => {
  212. const context: CallApiContextParams = {
  213. prompt: {
  214. config: {
  215. response_format: { type: 'json_object' },
  216. tool_choice: { function: { name: 'test' }, type: 'function' },
  217. },
  218. label: 'test prompt',
  219. raw: 'test prompt',
  220. },
  221. vars: {},
  222. };
  223. expect((provider as any).getOpenAiBody('test prompt', context)).toMatchObject({
  224. functions: [{ name: 'provider_func', parameters: {} }],
  225. max_tokens: 100,
  226. response_format: { type: 'json_object' },
  227. temperature: 0.5,
  228. tool_choice: { function: { name: 'test' }, type: 'function' },
  229. });
  230. });
  231. });
  232. });
Tip!

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

Comments

Loading...