Skip to content

Commit

Permalink
.Net: Fix: telemetry sample app repeated calls to the same registered…
Browse files Browse the repository at this point in the history
… function (#6544)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
Previously inside the telemetry sample app, the code registers a
prompt-based plugin to the kernel and then invokes the plugin directly
via `InvokeAsync()`. This is actually against the pattern we recommend.
The direct result of this is the models with function calling feature
could potentially invoke the same function repeatedly.

![image](https://github.com/microsoft/semantic-kernel/assets/12570346/d046e6b7-7220-4af9-aaa0-a198512e2864)

This is related to:
#6154,
#6281
### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Instead of registering the plugin to the kernel for later use, we create
the plugin without registering it to the kernel, so that the model
doesn't have the same function at its disposal when completing a
response.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
TaoChenOSU committed Jun 4, 2024
1 parent f42dfb1 commit 9af6cd6
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions dotnet/samples/Demos/TelemetryWithAppInsights/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,23 @@ private static async Task RunMistralAIChatAsync(Kernel kernel)

private static async Task RunChatAsync(Kernel kernel)
{
// Create the plugin from the sample plugins folder without registering it to the kernel.
// We do not advise registering plugins to the kernel and then invoking them directly,
// especially when the service supports function calling. Doing so will cause unexpected behavior,
// such as repeated calls to the same function.
var folder = RepoFiles.SamplePluginsPath();
var plugin = kernel.CreatePluginFromPromptDirectory(Path.Combine(folder, "WriterPlugin"));

// Using non-streaming to get the poem.
var poem = await kernel.InvokeAsync<string>(
"WriterPlugin",
"ShortPoem",
plugin["ShortPoem"],
new KernelArguments { ["input"] = "Write a poem about John Doe." });
Console.WriteLine($"Poem:\n{poem}\n");

// Use streaming to translate the poem.
Console.WriteLine("Translated Poem:");
await foreach (var update in kernel.InvokeStreamingAsync<string>(
"WriterPlugin",
"Translate",
plugin["Translate"],
new KernelArguments
{
["input"] = poem,
Expand Down Expand Up @@ -273,7 +278,6 @@ private static Kernel GetKernel(ILoggerFactory loggerFactory)
);

builder.Services.AddSingleton<IAIServiceSelector>(new AIServiceSelector());
builder.Plugins.AddFromPromptDirectory(Path.Combine(folder, "WriterPlugin"));
builder.Plugins.AddFromType<WeatherPlugin>();
builder.Plugins.AddFromType<LocationPlugin>();

Expand Down

0 comments on commit 9af6cd6

Please sign in to comment.