You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the AI model receives a prompt containing a list of functions, it may choose one or more of them for invocation to complete the prompt. When a function is chosen by the model, it needs be **invoked** by Semantic Kernel.
13
13
14
-
The function calling subsystem in Semantic Kernel has two modes of function invocation: **auto** and **manual**.
14
+
The function calling subsystem in Semantic Kernel has two modes of function invocation: **auto** and **manual**.
15
15
16
16
Depending on the invocation mode, Semantic Kernel either does end-to-end function invocation or gives the caller control over the function invocation process.
17
17
18
18
## Auto Function Invocation
19
-
Auto function invocation is the default mode of the Semantic Kernel function-calling subsystem. When the AI model chooses one or more functions, Semantic Kernel automatically invokes the chosen functions.
20
-
The results of these function invocations are added to the chat history and sent to the model automatically in subsequent requests.
21
-
The model then reasons about the chat history, chooses additional functions if needed, or generates the final response.
19
+
20
+
Auto function invocation is the default mode of the Semantic Kernel function-calling subsystem. When the AI model chooses one or more functions, Semantic Kernel automatically invokes the chosen functions.
21
+
The results of these function invocations are added to the chat history and sent to the model automatically in subsequent requests.
22
+
The model then reasons about the chat history, chooses additional functions if needed, or generates the final response.
22
23
This approach is fully automated and requires no manual intervention from the caller.
23
24
25
+
> [!TIP]
26
+
> Auto function invocation is different from the [auto function choice behavior](./function-choice-behaviors.md#using-auto-function-choice-behavior). The former dictates if functions should be invoked automatically by Semantic Kernel, while the latter determines if functions should be chosen automatically by the AI model.
27
+
24
28
This example demonstrates how to use the auto function invocation in Semantic Kernel. AI model decides which functions to call to complete the prompt and Semantic Kernel does the rest and invokes them automatically.
Some AI models support parallel function calling, where the model chooses multiple functions for invocation. This can be useful in cases when invoking chosen functions takes a long time. For example, the AI may choose to retrieve the latest news and the current time simultaneously, rather than making a round trip per function.
44
93
45
94
Semantic Kernel can invoke these functions in two different ways:
95
+
46
96
-**Sequentially**: The functions are invoked one after another. This is the default behavior.
47
97
-**Concurrently**: The functions are invoked at the same time. This can be enabled by setting the `FunctionChoiceBehaviorOptions.AllowConcurrentInvocation` property to `true`, as shown in the example below.
awaitkernel.InvokePromptAsync("Good morning! What is the current time and latest news headlines?", new(settings));
64
115
```
65
116
117
+
::: zone-end
118
+
119
+
::: zone pivot="programming-language-python"
120
+
121
+
Sometimes a model may choose multiple functions for invocation. This is often referred to as **parallel** function calling. When multiple functions are chosen by the AI model, Semantic Kernel will invoke them concurrently.
122
+
123
+
> [!TIP]
124
+
> With the OpenAI or Azure OpenAI connector, you can disable parallel function calling by doing the following:
In cases when the caller wants to have more control over the function invocation process, manual function invocation can be used.
68
139
69
-
When manual function invocation is enabled, Semantic Kernel does not automatically invoke the functions chosen by the AI model.
70
-
Instead, it returns a list of chosen functions to the caller, who can then decide which functions to invoke, invoke them sequentially or in parallel, handle exceptions, and so on.
71
-
The function invocation results need to be added to the chat history and returned to the model, which reasons about them and decides whether to choose additional functions or generate the final response.
140
+
In cases when the caller wants to have more control over the function invocation process, manual function invocation can be used.
141
+
142
+
When manual function invocation is enabled, Semantic Kernel does not automatically invoke the functions chosen by the AI model.
143
+
Instead, it returns a list of chosen functions to the caller, who can then decide which functions to invoke, invoke them sequentially orin parallel, handle exceptions, and so on.
144
+
The function invocation results need to be added to the chat history and returned to the model, which will reason about them and decide whether to choose additional functions or generate a final response.
72
145
73
146
The example below demonstrates how to use manual function invocation.
147
+
148
+
::: zone pivot="programming-language-csharp"
149
+
74
150
```csharp
75
151
using Microsoft.SemanticKernel;
76
152
using Microsoft.SemanticKernel.ChatCompletion;
@@ -135,11 +211,12 @@ while (true)
135
211
}
136
212
137
213
```
214
+
138
215
> [!NOTE]
139
-
> The FunctionCallContent and FunctionResultContent classes are used to represent AI model function calls and Semantic Kernel function invocation results, respectively.
216
+
> The FunctionCallContent and FunctionResultContent classes are used to represent AI model function calls and Semantic Kernel function invocation results, respectively.
140
217
> They contain information about chosen function, such as the function ID, name, and arguments, and function invocation results, such as function call IDand result.
141
218
142
-
The following example demonstrates how to use manual function invocation with the streaming chat completion API. Note the usage of the `FunctionCallContentBuilder` class to build function calls from the streaming content.
219
+
The following example demonstrates how to use manual function invocation with the streaming chat completion API. Note the usage of the `FunctionCallContentBuilder`class to build function calls from the streaming content.
143
220
Due to the streaming nature of the API, function calls are also streamed. This means that the caller must build the function calls from the streaming content before invoking them.
144
221
145
222
```csharp
@@ -210,11 +287,62 @@ while (true)
210
287
```
211
288
212
289
::: zone-end
290
+
213
291
::: zone pivot="programming-language-python"
214
-
## Coming soon
215
-
More info coming soon.
292
+
293
+
```python
294
+
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
295
+
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
296
+
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
297
+
from semantic_kernel.contents.chat_history import ChatHistory
298
+
from semantic_kernel.contents.function_call_content import FunctionCallContent
299
+
from semantic_kernel.contents.function_result_content import FunctionResultContent
300
+
from semantic_kernel.kernel import Kernel
301
+
302
+
kernel = Kernel()
303
+
chat_completion_service = OpenAIChatCompletion()
304
+
305
+
# Assuming that WeatherPlugin is already implemented
# The weather in Seattle on September 10th, 2024, is expected to be [weather condition].
336
+
```
337
+
338
+
> [!NOTE]
339
+
> The FunctionCallContent and FunctionResultContent classes are used to represent AI model function calls and Semantic Kernel function invocation results, respectively. They contain information about chosen function, such as the function ID, name, and arguments, and function invocation results, such as function call IDand result.
0 commit comments