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: agent-framework/tutorials/agents/enable-observability.md
+7-4Lines changed: 7 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,13 +16,16 @@ ms.service: semantic-kernel
16
16
This tutorial shows how to enable OpenTelemetry on an agent so that interactions with the agent are automatically logged and exported.
17
17
In this tutorial, output is written to the console using the OpenTelemetry console exporter.
18
18
19
+
> [!NOTE]
20
+
> See [Semantic Conventions for GenAI agent and framework spans](https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-agent-spans/) from Open Telemetry for more information about the standards followed by the Microsoft Agent Framework.
21
+
19
22
## Prerequisites
20
23
21
24
For prerequisites, see the [Create and run a simple agent](./run-agent.md) step in this tutorial.
22
25
23
26
## Installing Nuget packages
24
27
25
-
To use the AgentFramework with Azure OpenAI, you need to install the following NuGet packages:
28
+
To use the Agent Framework with Azure OpenAI, you need to install the following NuGet packages:
To add this middleware function to the `baseAgent` we created in step 1,
76
+
we should use the builder pattern.
77
+
This creates a new agent that has the middleware applied.
78
+
The original `baseAgent` is not modified.
79
+
80
+
```csharp
81
+
varmiddlewareEnabledAgent=baseAgent
82
+
.AsBuilder()
83
+
.Use(CustomAgentRunMiddleware)
84
+
.Build();
85
+
```
86
+
87
+
## Step 4: Create Function calling Middleware
88
+
89
+
> [!NOTE]
90
+
> Function calling middleware is currently only supported with an `AIAgent` that uses `Microsoft.Extensions.AI.FunctionInvokingChatClient`, e.g. `ChatClientAgent`.
91
+
92
+
We can also create middleware that gets called for each function tool that is invoked.
93
+
Here is an example of function calling middleware, that can inspect and/or modify the function being called, and the result from the function call.
94
+
95
+
Unless the intention is to use the middleware to not execute the function tool, the middleware
## Step 5: Add Function calling Middleware to Your Agent
114
+
115
+
Same as with adding agent run middleware, we can add function calling middleware as follows:
116
+
117
+
```csharp
118
+
varmiddlewareEnabledAgent=baseAgent
119
+
.AsBuilder()
120
+
.Use(CustomFunctionCallingMiddleware)
121
+
.Build();
122
+
```
123
+
124
+
Now, when executing the agent with a query that invokes a function, the middleware should get invoked,
125
+
outputting the function name and call result.
126
+
127
+
```csharp
128
+
awaitmiddlewareEnabledAgent.RunAsync("What's the current time?");
129
+
```
130
+
131
+
## Step 6: Create Chat Client Middleware
132
+
133
+
For agents that are built using `IChatClient` developers may want to intercept calls going from the agent to the `IChatClient`.
134
+
In this case it is possible to use middleware for the `IChatClient`.
135
+
136
+
Here is an example of chat client middleware, that can inspect and/or modify the input and output for the request to the inference service that the chat client provides.
> For more information about `IChatClient` middleware, see [Custom IChatClient middleware](/dotnet/ai/microsoft-extensions-ai#custom-ichatclient-middleware)
155
+
> in the Microsoft.Extensions.AI documentation.
156
+
157
+
## Step 7: Add Chat client Middleware to an `IChatClient`
158
+
159
+
To add middleware to your `IChatClient`, you can use the builder pattern.
160
+
After adding the middleware, you can use the `IChatClient` with your agent as usual.
@@ -113,7 +115,11 @@ If there were more than one function available for invocation during this iterat
113
115
Here is an example of chat client middleware, that can inspect and/or modify the input and output for the request to the inference service that the chat client provides.
0 commit comments