2
2
3
3
using System . Collections . Generic ;
4
4
using System . Linq ;
5
- using Azure . AI . OpenAI ;
6
5
using Microsoft . SemanticKernel ;
7
6
using Microsoft . SemanticKernel . Connectors . AzureOpenAI ;
8
- using static Microsoft . SemanticKernel . Connectors . AzureOpenAI . AzureToolCallBehavior ;
7
+ using OpenAI . Chat ;
8
+ using static Microsoft . SemanticKernel . Connectors . AzureOpenAI . AzureOpenAIToolCallBehavior ;
9
9
10
10
namespace SemanticKernel . Connectors . AzureOpenAI . UnitTests ;
11
11
12
12
/// <summary>
13
- /// Unit tests for <see cref="AzureToolCallBehavior "/>
13
+ /// Unit tests for <see cref="AzureOpenAIToolCallBehavior "/>
14
14
/// </summary>
15
- public sealed class AzureToolCallBehaviorTests
15
+ public sealed class AzureOpenAIToolCallBehaviorTests
16
16
{
17
17
[ Fact ]
18
18
public void EnableKernelFunctionsReturnsCorrectKernelFunctionsInstance ( )
19
19
{
20
20
// Arrange & Act
21
- var behavior = AzureToolCallBehavior . EnableKernelFunctions ;
21
+ var behavior = AzureOpenAIToolCallBehavior . EnableKernelFunctions ;
22
22
23
23
// Assert
24
24
Assert . IsType < KernelFunctions > ( behavior ) ;
@@ -30,7 +30,7 @@ public void AutoInvokeKernelFunctionsReturnsCorrectKernelFunctionsInstance()
30
30
{
31
31
// Arrange & Act
32
32
const int DefaultMaximumAutoInvokeAttempts = 128 ;
33
- var behavior = AzureToolCallBehavior . AutoInvokeKernelFunctions ;
33
+ var behavior = AzureOpenAIToolCallBehavior . AutoInvokeKernelFunctions ;
34
34
35
35
// Assert
36
36
Assert . IsType < KernelFunctions > ( behavior ) ;
@@ -42,7 +42,7 @@ public void EnableFunctionsReturnsEnabledFunctionsInstance()
42
42
{
43
43
// Arrange & Act
44
44
List < AzureOpenAIFunction > functions = [ new ( "Plugin" , "Function" , "description" , [ ] , null ) ] ;
45
- var behavior = AzureToolCallBehavior . EnableFunctions ( functions ) ;
45
+ var behavior = AzureOpenAIToolCallBehavior . EnableFunctions ( functions ) ;
46
46
47
47
// Assert
48
48
Assert . IsType < EnabledFunctions > ( behavior ) ;
@@ -52,7 +52,7 @@ public void EnableFunctionsReturnsEnabledFunctionsInstance()
52
52
public void RequireFunctionReturnsRequiredFunctionInstance ( )
53
53
{
54
54
// Arrange & Act
55
- var behavior = AzureToolCallBehavior . RequireFunction ( new ( "Plugin" , "Function" , "description" , [ ] , null ) ) ;
55
+ var behavior = AzureOpenAIToolCallBehavior . RequireFunction ( new ( "Plugin" , "Function" , "description" , [ ] , null ) ) ;
56
56
57
57
// Assert
58
58
Assert . IsType < RequiredFunction > ( behavior ) ;
@@ -63,65 +63,62 @@ public void KernelFunctionsConfigureOptionsWithNullKernelDoesNotAddTools()
63
63
{
64
64
// Arrange
65
65
var kernelFunctions = new KernelFunctions ( autoInvoke : false ) ;
66
- var chatCompletionsOptions = new ChatCompletionsOptions ( ) ;
67
66
68
67
// Act
69
- kernelFunctions . ConfigureOptions ( null , chatCompletionsOptions ) ;
68
+ var options = kernelFunctions . ConfigureOptions ( null ) ;
70
69
71
70
// Assert
72
- Assert . Empty ( chatCompletionsOptions . Tools ) ;
71
+ Assert . Null ( options . Choice ) ;
72
+ Assert . Null ( options . Tools ) ;
73
73
}
74
74
75
75
[ Fact ]
76
76
public void KernelFunctionsConfigureOptionsWithoutFunctionsDoesNotAddTools ( )
77
77
{
78
78
// Arrange
79
79
var kernelFunctions = new KernelFunctions ( autoInvoke : false ) ;
80
- var chatCompletionsOptions = new ChatCompletionsOptions ( ) ;
81
80
var kernel = Kernel . CreateBuilder ( ) . Build ( ) ;
82
81
83
82
// Act
84
- kernelFunctions . ConfigureOptions ( kernel , chatCompletionsOptions ) ;
83
+ var options = kernelFunctions . ConfigureOptions ( kernel ) ;
85
84
86
85
// Assert
87
- Assert . Null ( chatCompletionsOptions . ToolChoice ) ;
88
- Assert . Empty ( chatCompletionsOptions . Tools ) ;
86
+ Assert . Null ( options . Choice ) ;
87
+ Assert . Null ( options . Tools ) ;
89
88
}
90
89
91
90
[ Fact ]
92
91
public void KernelFunctionsConfigureOptionsWithFunctionsAddsTools ( )
93
92
{
94
93
// Arrange
95
94
var kernelFunctions = new KernelFunctions ( autoInvoke : false ) ;
96
- var chatCompletionsOptions = new ChatCompletionsOptions ( ) ;
97
95
var kernel = Kernel . CreateBuilder ( ) . Build ( ) ;
98
96
99
97
var plugin = this . GetTestPlugin ( ) ;
100
98
101
99
kernel . Plugins . Add ( plugin ) ;
102
100
103
101
// Act
104
- kernelFunctions . ConfigureOptions ( kernel , chatCompletionsOptions ) ;
102
+ var options = kernelFunctions . ConfigureOptions ( kernel ) ;
105
103
106
104
// Assert
107
- Assert . Equal ( ChatCompletionsToolChoice . Auto , chatCompletionsOptions . ToolChoice ) ;
105
+ Assert . Equal ( ChatToolChoice . Auto , options . Choice ) ;
108
106
109
- this . AssertTools ( chatCompletionsOptions ) ;
107
+ this . AssertTools ( options . Tools ) ;
110
108
}
111
109
112
110
[ Fact ]
113
111
public void EnabledFunctionsConfigureOptionsWithoutFunctionsDoesNotAddTools ( )
114
112
{
115
113
// Arrange
116
114
var enabledFunctions = new EnabledFunctions ( [ ] , autoInvoke : false ) ;
117
- var chatCompletionsOptions = new ChatCompletionsOptions ( ) ;
118
115
119
116
// Act
120
- enabledFunctions . ConfigureOptions ( null , chatCompletionsOptions ) ;
117
+ var options = enabledFunctions . ConfigureOptions ( null ) ;
121
118
122
119
// Assert
123
- Assert . Null ( chatCompletionsOptions . ToolChoice ) ;
124
- Assert . Empty ( chatCompletionsOptions . Tools ) ;
120
+ Assert . Null ( options . Choice ) ;
121
+ Assert . Null ( options . Tools ) ;
125
122
}
126
123
127
124
[ Fact ]
@@ -130,10 +127,9 @@ public void EnabledFunctionsConfigureOptionsWithAutoInvokeAndNullKernelThrowsExc
130
127
// Arrange
131
128
var functions = this . GetTestPlugin ( ) . GetFunctionsMetadata ( ) . Select ( function => function . ToAzureOpenAIFunction ( ) ) ;
132
129
var enabledFunctions = new EnabledFunctions ( functions , autoInvoke : true ) ;
133
- var chatCompletionsOptions = new ChatCompletionsOptions ( ) ;
134
130
135
131
// Act & Assert
136
- var exception = Assert . Throws < KernelException > ( ( ) => enabledFunctions . ConfigureOptions ( null , chatCompletionsOptions ) ) ;
132
+ var exception = Assert . Throws < KernelException > ( ( ) => enabledFunctions . ConfigureOptions ( null ) ) ;
137
133
Assert . Equal ( $ "Auto-invocation with { nameof ( EnabledFunctions ) } is not supported when no kernel is provided.", exception . Message ) ;
138
134
}
139
135
@@ -143,11 +139,10 @@ public void EnabledFunctionsConfigureOptionsWithAutoInvokeAndEmptyKernelThrowsEx
143
139
// Arrange
144
140
var functions = this . GetTestPlugin ( ) . GetFunctionsMetadata ( ) . Select ( function => function . ToAzureOpenAIFunction ( ) ) ;
145
141
var enabledFunctions = new EnabledFunctions ( functions , autoInvoke : true ) ;
146
- var chatCompletionsOptions = new ChatCompletionsOptions ( ) ;
147
142
var kernel = Kernel . CreateBuilder ( ) . Build ( ) ;
148
143
149
144
// Act & Assert
150
- var exception = Assert . Throws < KernelException > ( ( ) => enabledFunctions . ConfigureOptions ( kernel , chatCompletionsOptions ) ) ;
145
+ var exception = Assert . Throws < KernelException > ( ( ) => enabledFunctions . ConfigureOptions ( kernel ) ) ;
151
146
Assert . Equal ( $ "The specified { nameof ( EnabledFunctions ) } function MyPlugin-MyFunction is not available in the kernel.", exception . Message ) ;
152
147
}
153
148
@@ -160,18 +155,17 @@ public void EnabledFunctionsConfigureOptionsWithKernelAndPluginsAddsTools(bool a
160
155
var plugin = this . GetTestPlugin ( ) ;
161
156
var functions = plugin . GetFunctionsMetadata ( ) . Select ( function => function . ToAzureOpenAIFunction ( ) ) ;
162
157
var enabledFunctions = new EnabledFunctions ( functions , autoInvoke ) ;
163
- var chatCompletionsOptions = new ChatCompletionsOptions ( ) ;
164
158
var kernel = Kernel . CreateBuilder ( ) . Build ( ) ;
165
159
166
160
kernel . Plugins . Add ( plugin ) ;
167
161
168
162
// Act
169
- enabledFunctions . ConfigureOptions ( kernel , chatCompletionsOptions ) ;
163
+ var options = enabledFunctions . ConfigureOptions ( kernel ) ;
170
164
171
165
// Assert
172
- Assert . Equal ( ChatCompletionsToolChoice . Auto , chatCompletionsOptions . ToolChoice ) ;
166
+ Assert . Equal ( ChatToolChoice . Auto , options . Choice ) ;
173
167
174
- this . AssertTools ( chatCompletionsOptions ) ;
168
+ this . AssertTools ( options . Tools ) ;
175
169
}
176
170
177
171
[ Fact ]
@@ -180,10 +174,9 @@ public void RequiredFunctionsConfigureOptionsWithAutoInvokeAndNullKernelThrowsEx
180
174
// Arrange
181
175
var function = this . GetTestPlugin ( ) . GetFunctionsMetadata ( ) . Select ( function => function . ToAzureOpenAIFunction ( ) ) . First ( ) ;
182
176
var requiredFunction = new RequiredFunction ( function , autoInvoke : true ) ;
183
- var chatCompletionsOptions = new ChatCompletionsOptions ( ) ;
184
177
185
178
// Act & Assert
186
- var exception = Assert . Throws < KernelException > ( ( ) => requiredFunction . ConfigureOptions ( null , chatCompletionsOptions ) ) ;
179
+ var exception = Assert . Throws < KernelException > ( ( ) => requiredFunction . ConfigureOptions ( null ) ) ;
187
180
Assert . Equal ( $ "Auto-invocation with { nameof ( RequiredFunction ) } is not supported when no kernel is provided.", exception . Message ) ;
188
181
}
189
182
@@ -193,11 +186,10 @@ public void RequiredFunctionsConfigureOptionsWithAutoInvokeAndEmptyKernelThrowsE
193
186
// Arrange
194
187
var function = this . GetTestPlugin ( ) . GetFunctionsMetadata ( ) . Select ( function => function . ToAzureOpenAIFunction ( ) ) . First ( ) ;
195
188
var requiredFunction = new RequiredFunction ( function , autoInvoke : true ) ;
196
- var chatCompletionsOptions = new ChatCompletionsOptions ( ) ;
197
189
var kernel = Kernel . CreateBuilder ( ) . Build ( ) ;
198
190
199
191
// Act & Assert
200
- var exception = Assert . Throws < KernelException > ( ( ) => requiredFunction . ConfigureOptions ( kernel , chatCompletionsOptions ) ) ;
192
+ var exception = Assert . Throws < KernelException > ( ( ) => requiredFunction . ConfigureOptions ( kernel ) ) ;
201
193
Assert . Equal ( $ "The specified { nameof ( RequiredFunction ) } function MyPlugin-MyFunction is not available in the kernel.", exception . Message ) ;
202
194
}
203
195
@@ -207,18 +199,17 @@ public void RequiredFunctionConfigureOptionsAddsTools()
207
199
// Arrange
208
200
var plugin = this . GetTestPlugin ( ) ;
209
201
var function = plugin . GetFunctionsMetadata ( ) [ 0 ] . ToAzureOpenAIFunction ( ) ;
210
- var chatCompletionsOptions = new ChatCompletionsOptions ( ) ;
211
202
var requiredFunction = new RequiredFunction ( function , autoInvoke : true ) ;
212
203
var kernel = new Kernel ( ) ;
213
204
kernel . Plugins . Add ( plugin ) ;
214
205
215
206
// Act
216
- requiredFunction . ConfigureOptions ( kernel , chatCompletionsOptions ) ;
207
+ var options = requiredFunction . ConfigureOptions ( kernel ) ;
217
208
218
209
// Assert
219
- Assert . NotNull ( chatCompletionsOptions . ToolChoice ) ;
210
+ Assert . NotNull ( options . Choice ) ;
220
211
221
- this . AssertTools ( chatCompletionsOptions ) ;
212
+ this . AssertTools ( options . Tools ) ;
222
213
}
223
214
224
215
private KernelPlugin GetTestPlugin ( )
@@ -233,16 +224,15 @@ private KernelPlugin GetTestPlugin()
233
224
return KernelPluginFactory . CreateFromFunctions ( "MyPlugin" , [ function ] ) ;
234
225
}
235
226
236
- private void AssertTools ( ChatCompletionsOptions chatCompletionsOptions )
227
+ private void AssertTools ( IList < ChatTool > ? tools )
237
228
{
238
- Assert . Single ( chatCompletionsOptions . Tools ) ;
239
-
240
- var tool = chatCompletionsOptions . Tools [ 0 ] as ChatCompletionsFunctionToolDefinition ;
229
+ Assert . NotNull ( tools ) ;
230
+ var tool = Assert . Single ( tools ) ;
241
231
242
232
Assert . NotNull ( tool ) ;
243
233
244
- Assert . Equal ( "MyPlugin-MyFunction" , tool . Name ) ;
245
- Assert . Equal ( "Test Function" , tool . Description ) ;
246
- Assert . Equal ( "{\" type\" :\" object\" ,\" required\" :[],\" properties\" :{\" parameter1\" :{\" type\" :\" string\" },\" parameter2\" :{\" type\" :\" string\" }}}" , tool . Parameters . ToString ( ) ) ;
234
+ Assert . Equal ( "MyPlugin-MyFunction" , tool . FunctionName ) ;
235
+ Assert . Equal ( "Test Function" , tool . FunctionDescription ) ;
236
+ Assert . Equal ( "{\" type\" :\" object\" ,\" required\" :[],\" properties\" :{\" parameter1\" :{\" type\" :\" string\" },\" parameter2\" :{\" type\" :\" string\" }}}" , tool . FunctionParameters . ToString ( ) ) ;
247
237
}
248
238
}
0 commit comments