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
Copy file name to clipboardExpand all lines: semantic-kernel/Frameworks/agent/agent-api.md
+42-50Lines changed: 42 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,10 +9,9 @@ ms.date: 04/03/2025
9
9
ms.service: semantic-kernel
10
10
---
11
11
12
-
# The Semantic Kernel Common Agent API surface
12
+
# The Semantic Kernel Common Agent API Surface
13
13
14
-
Semantic Kernel agents share a common interface for invoking agents.
15
-
This allows for common code to be written, that works against any agent type and allows for easily switching agents as required, without needing to change the bulk of your code.
14
+
Semantic Kernel agents implement a unified interface for invocation, enabling shared code that operates seamlessly across different agent types. This design allows you to switch agents as needed without modifying the majority of your application logic.
16
15
17
16
## Invoking an agent
18
17
@@ -22,9 +21,7 @@ The Agent API surface supports both streaming and non-streaming invocation.
22
21
23
22
::: zone pivot="programming-language-csharp"
24
23
25
-
Semantic Kernel supports four non-streaming agent invocation overloads that allows for passing messages in different ways.
26
-
One of these also allows invoking the agent with no messages.
27
-
This is valuable for scenarios where the agent instructions already have all the required context to provide a useful response.
24
+
Semantic Kernel supports four non-streaming agent invocation overloads that allows for passing messages in different ways. One of these also allows invoking the agent with no messages. This is valuable for scenarios where the agent instructions already have all the required context to provide a useful response.
Semantic Kernel supports two non-streaming agent invocation methods that allows for passing messages in different ways.
55
-
It is also possible to invoke the agent with no messages.
56
-
This is valuable for scenarios where the agent instructions already have all the required context to provide a useful response.
51
+
Semantic Kernel supports two non-streaming agent invocation methods that allows for passing messages in different ways. It is also possible to invoke the agent with no messages. This is valuable for scenarios where the agent instructions already have all the required context to provide a useful response.
57
52
58
53
> [!TIP]
59
-
> All arguments passed to Agent invocation methods require the caller to pass them as keyword arguments.
54
+
> All arguments passed to Agent invocation methods require the caller to pass them as keyword arguments, except for the first positional argument, `messages`. You may invoke with either a positional or a keyword argument for `messages`. For example, both `await agent.get_response("What is the capital of France?")` and `await agent.get_response(messages="What is the capital of France?")` are supported. All other parameters must be passed as keyword arguments.
60
55
61
56
#### Using the `get_response()` method
62
57
63
58
```python
64
59
# Invoke without any messages.
65
-
await agent.get_response(messages=None)
60
+
await agent.get_response()
66
61
67
62
# Invoke with a string that will be used as a User message.
68
63
await agent.get_response(messages="What is the capital of France?")
@@ -83,15 +78,15 @@ await agent.get_response(
83
78
84
79
```python
85
80
# Invoke without any messages.
86
-
asyncfor response in agent.invoke(messages=None):
81
+
asyncfor response in agent.invoke():
87
82
# handle response
88
83
89
84
# Invoke with a string that will be used as a User message.
90
-
asyncfor response in agent.invoke(messages="What is the capital of France?"):
85
+
asyncfor response in agent.invoke("What is the capital of France?"):
91
86
# handle response
92
87
93
88
# Invoke with a ChatMessageContent object.
94
-
asyncfor response in agent.invoke(messages=ChatMessageContent(AuthorRole.USER, "What is the capital of France?")):
89
+
asyncfor response in agent.invoke(ChatMessageContent(AuthorRole.USER, "What is the capital of France?")):
95
90
# handle response
96
91
97
92
# Invoke with multiple ChatMessageContent objects.
@@ -111,9 +106,7 @@ async for response in agent.invoke(
111
106
112
107
::: zone pivot="programming-language-java"
113
108
114
-
Semantic Kernel supports three non-streaming agent invocation overloads that allows for passing messages in different ways.
115
-
One of these also allows invoking the agent with no messages.
116
-
This is valuable for scenarios where the agent instructions already have all the required context to provide a useful response.
109
+
Semantic Kernel supports three non-streaming agent invocation overloads that allows for passing messages in different ways. One of these also allows invoking the agent with no messages. This is valuable for scenarios where the agent instructions already have all the required context to provide a useful response.
117
110
118
111
```java
119
112
// Invoke without any parameters.
@@ -141,9 +134,7 @@ agent.invokeAsync(List.of(
141
134
142
135
::: zone pivot="programming-language-csharp"
143
136
144
-
Semantic Kernel supports four streaming agent invocation overloads that allows for passing messages in different ways.
145
-
One of these also allows invoking the agent with no messages.
146
-
This is valuable for scenarios where the agent instructions already have all the required context to provide a useful response.
137
+
Semantic Kernel supports four streaming agent invocation overloads that allows for passing messages in different ways. One of these also allows invoking the agent with no messages. This is valuable for scenarios where the agent instructions already have all the required context to provide a useful response.
Semantic Kernel supports one streaming agent invocation method that allows for passing messages in different ways.
174
-
It is also possible to invoke the agent stream with no messages.
175
-
This is valuable for scenarios where the agent instructions already have all the required context to provide a useful response.
164
+
Semantic Kernel supports one streaming agent invocation method that allows for passing messages in different ways. It is also possible to invoke the agent stream with no messages. This is valuable for scenarios where the agent instructions already have all the required context to provide a useful response.
176
165
177
166
```python
178
167
# Invoke without any messages.
179
-
asyncfor response in agent.invoke_stream(messages=None):
168
+
asyncfor response in agent.invoke_stream():
180
169
# handle response
181
170
182
171
# Invoke with a string that will be used as a User message.
183
-
asyncfor response in agent.invoke_stream(messages="What is the capital of France?"):
172
+
asyncfor response in agent.invoke_stream("What is the capital of France?"):
184
173
# handle response
185
174
186
175
# Invoke with a ChatMessageContent object.
@@ -200,7 +189,6 @@ async for response in agent.invoke_stream(
200
189
> [!IMPORTANT]
201
190
> Invoking an agent without passing an `AgentThread` to the `invoke_stream()` method will create a new thread and return the new thread in the response.
202
191
203
-
204
192
::: zone-end
205
193
206
194
::: zone pivot="programming-language-java"
@@ -213,8 +201,7 @@ async for response in agent.invoke_stream(
213
201
214
202
::: zone pivot="programming-language-csharp"
215
203
216
-
All invocation method overloads allow passing an `AgentThread` parameter.
217
-
This is useful for scenarios where you have an existing conversation with the agent that you want to continue.
204
+
All invocation method overloads allow passing an `AgentThread` parameter. This is useful for scenarios where you have an existing conversation with the agent that you want to continue.
218
205
219
206
```csharp
220
207
// Invoke with an existing AgentThread.
@@ -241,12 +228,11 @@ var resultMessage = result.Message;
241
228
242
229
::: zone pivot="programming-language-python"
243
230
244
-
All invocation method keyword arguments allow passing an `AgentThread` parameter.
245
-
This is useful for scenarios where you have an existing conversation with the agent that you want to continue.
231
+
All invocation method keyword arguments allow passing an `AgentThread` parameter. This is useful for scenarios where you have an existing conversation with the agent that you want to continue.
246
232
247
233
```python
248
234
# Invoke with an existing AgentThread.
249
-
agent.get_response(messages="What is the capital of France?", existing_agent_thread)
235
+
agent.get_response("What is the capital of France?", thread=existing_agent_thread)
250
236
```
251
237
252
238
All invocation methods also return the active `AgentThread` as part of the invoke response.
@@ -257,7 +243,7 @@ All invocation methods also return the active `AgentThread` as part of the invok
257
243
The returned `AgentThread` is available on the individual response items of the invoke methods together with the response message.
258
244
259
245
```python
260
-
response =await agent.get_response(messages="What is the capital of France?")
246
+
response =await agent.get_response("What is the capital of France?")
Two invocation method overloads allow passing an `AgentThread` parameter.
273
-
This is useful for scenarios where you have an existing conversation with the agent that you want to continue.
258
+
Two invocation method overloads allow passing an `AgentThread` parameter. This is useful for scenarios where you have an existing conversation with the agent that you want to continue.
274
259
275
260
```java
276
261
// Invoke with an existing AgentThread.
@@ -295,10 +280,10 @@ var resultMessage = result.getMessage();
295
280
296
281
::: zone-end
297
282
298
-
### Invoking with Options
299
-
300
283
::: zone pivot="programming-language-csharp"
301
284
285
+
### Invoking with Options
286
+
302
287
All invocation method overloads allow passing an `AgentInvokeOptions` parameter.
303
288
This options class allows providing any optional settings.
304
289
@@ -312,12 +297,12 @@ agent.InvokeAsync("What is the capital of France?", options: new()
312
297
313
298
Here is the list of the supported options.
314
299
315
-
|Option Property|Description|
316
-
|-|-|
317
-
|Kernel|Override the default kernel used by the agent for this invocation.|
318
-
|KernelArguments|Override the default kernel arguments used by the agent for this invocation.|
319
-
|AdditionalInstructions|Provide any instructions in addition to the original agent instruction set, that only apply for this invocation.|
320
-
|OnIntermediateMessage|A callback that can receive all fully formed messages produced internally to the Agent, including function call and function invocation messages. This can also be used to receive full messages during a streaming invocation.|
|Kernel|Override the default kernel used by the agent for this invocation.|
303
+
|KernelArguments|Override the default kernel arguments used by the agent for this invocation.|
304
+
|AdditionalInstructions|Provide any instructions in addition to the original agent instruction set, that only apply for this invocation.|
305
+
|OnIntermediateMessage|A callback that can receive all fully formed messages produced internally to the Agent, including function call and function invocation messages. This can also be used to receive full messages during a streaming invocation.|
321
306
322
307
::: zone-end
323
308
@@ -327,12 +312,14 @@ Here is the list of the supported options.
327
312
328
313
::: zone pivot="programming-language-java"
329
314
315
+
### Invoking with Options
316
+
330
317
One invocation method overloads allow passing an `AgentInvokeOptions` parameter.
331
318
This options class allows providing any optional settings.
332
319
333
320
```java
334
321
// Invoke with additional instructions via options.
335
-
agent.invokeAsync("What is the capital of France?",
322
+
agent.invokeAsync("What is the capital of France?",
336
323
null, // null AgentThread
337
324
AgentInvokeOptions.builder()
338
325
.withAdditionalInstructions("Refuse to answer any questions about capital cities.")
@@ -342,12 +329,12 @@ agent.invokeAsync("What is the capital of France?",
342
329
343
330
Here is the list of the supported options.
344
331
345
-
|Option Property|Description|
346
-
|-|-|
347
-
|Kernel|Override the default kernel used by the agent for this invocation.|
348
-
|KernelArguments|Override the default kernel arguments used by the agent for this invocation.|
349
-
|AdditionalInstructions|Provide any instructions in addition to the original agent instruction set, that only apply for this invocation.|
350
-
|InvocationContext|Override the default invocation context the agent uses for this invocation.|
0 commit comments