Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: plugin functions not loaded into ChatCompletionAgent when using kernel.CreatePluginFromObject() #10621

Open
jjgriff93 opened this issue Feb 20, 2025 · 2 comments
Assignees
Labels
agents bug Something isn't working documentation

Comments

@jjgriff93
Copy link

Describe the bug
I have some code that creates a number of agents dynamically, each with a stateful plugin created from a new object instance (following the example in the docs):

public static ChatCompletionAgent CreatePlayerAgent(Kernel kernel, int playerId, string initialRole)
{
    // Clone kernel instance to allow for agent specific plug-in definition
    Kernel agentKernel = kernel.Clone();

    // Initialize plug-in from object
    agentKernel.CreatePluginFromObject(new WerewolfPlayerPlugin(playerId, initialRole));

    // Create the agent
    return
      new ChatCompletionAgent()
      {
        Name = playerId.ToString(),
        Kernel = agentKernel,
        Instructions = Prompts.PlayerInstructions,
        Arguments = new KernelArguments(
          new OpenAIPromptExecutionSettings()
          {
            FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
          }
        )
        {
          { "playerId", playerId },
          ...
        }
      };
}

The plugin itself only has one KernelFunction:

public class WerewolfPlayerPlugin
{
  public int playerId;
  public string initialRole;

  public WerewolfPlayerPlugin(int playerId, string initialRole)
  {
    this.playerId = playerId;
    this.initialRole = initialRole;
  }

  [KernelFunction("vote")]
  [Description("Vote for another player to be eliminated")]
  public async Task Vote(
    [Description("The number of the player you want to vote for")]
    int id
  )
  {
    Console.WriteLine($"Player {playerId} voted for player {id}");
    Werewolf.CurrentVotes[playerId] = id;
  }
}

When initiating a group chat, I can see from the trace output that the function isn't being loaded:

Choice:auto, AutoInvoke:True, AllowConcurrentInvocation:False, AllowParallelCalls:(null) Functions:None (Function calling is disabled)

However, when I replace the CreatePluginFromObject with:

agentKernel.Plugins.AddFromObject(new WerewolfPlayerPlugin(playerId, initialRole));

It loads just fine:

Choice:auto, AutoInvoke:True, AllowConcurrentInvocation:False, AllowParallelCalls:(null) Functions:WerewolfPlayerPlugin-vote

Looking at the code I believe the example in the docs is incorrect as the plugin isn't added to the plugins collection after being created within the CreatePluginFromObject function:

return KernelPluginFactory.CreateFromObject(target, pluginName, kernel.LoggerFactory);

Whereas in Plugins.AddFromObject it is:

Platform

  • Language: C#
  • Source: Microsoft.SemanticKernel.Agents.Core Version=1.37.0-alpha
  • AI model: GPT-4o
  • IDE: VS Code
  • OS: Mac
@jjgriff93
Copy link
Author

Raised an issue on docs: MicrosoftDocs/semantic-kernel-docs#211
And PR to fix: MicrosoftDocs/semantic-kernel-docs#212

If this is indeed the expected functionality (which I believe it is) then this issue can be closed in favour of the docs issue above

@crickman
Copy link
Contributor

Yes, the docs are incorrect.

@crickman crickman moved this to Sprint: In Review in Semantic Kernel Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agents bug Something isn't working documentation
Projects
Status: Sprint: In Review
Development

No branches or pull requests

3 participants