semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Application Insight meter for TotalTokens is not working as expected with Agents

Open zimeo99 opened this issue 2 years ago • 3 comments

Hi, I am adding a meter for Semantic Kernel in Application Insight. Actually my project uses an agent from the Agents package (using the integration with OpenAI Assistant API).

I am looking at the code in the following guide and successfully modified my Startup.cs code. I can correctly monitor and see the meter for the invoke function as explained in the guide.

However, when i try to add the meter to monitor the TotalToken it does not work. From the guide the AddMeter parameter to use should be "Microsoft.SemanticKernel.Connectors.OpenAI" but it does not work. I've installed both nuget packages that contains the functions to calculate the metrics.

Probably the string to use is different, from the one provided. Does anyone has some suggestions? It's just the parameter that is different?

Thanks in advance. image image

zimeo99 avatar Apr 11 '24 16:04 zimeo99

Probably the problem is related to the logger factory, but actually there is no method available to customize the kernel istance under the agent object. For example, it could be useful to use the same method .WithLoggerFactory(loggerFactory) when using the Agent Builder in order to control the kernel istance under the agent.

zimeo99 avatar Apr 12 '24 07:04 zimeo99

@zimeo99 - Thanks for reaching out on this. I believe there are a couple aspects in play.

First and foremost, this version of agents is entirely based on the OpenAI Assistant API and is directly targeting the REST endpoint, as it was developed prior to the Azure Open Assistant SDK becoming available: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/openai/Azure.AI.OpenAI.Assistants

The chat-completion connector is only used by the agent when it invokes a KernelFunction based on prompt (KernelFunctionFactory.CreateFromPrompt()). I suspect if your agent were to call such a KernelFunction, you'd see the expected telemetry (Microsoft.SemanticKernel.Connectors.*).

I'd also expect you to be seeing any regular function-calling telemetry the agent performs (Microsoft.SemanticKernel.*). Your confimation in the description aligns with this.

I may be doing a pass on this package soon, and may have a chance to add logging and telemetry, however the agent telemetry may take its own shape (tbd):

https://github.com/microsoft/semantic-kernel/issues/4109

crickman avatar Apr 18 '24 22:04 crickman

Hi @crickman, thanks for the clear explaination and for you support.

zimeo99 avatar Apr 19 '24 15:04 zimeo99