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

.Net: Extract Plugin Name and Arguments with agent.invoke #10769

Open
sophialagerkranspandey opened this issue Mar 3, 2025 · 0 comments
Open
Assignees
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code python Pull requests for the Python Semantic Kernel

Comments

@sophialagerkranspandey
Copy link
Contributor

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Platform

  • Language: [e.g. C#, Python]
  • Source: [e.g. NuGet package version 0.1.0, pip package version 0.1.0, main branch of repository]
  • AI model: [e.g. OpenAI:GPT-4o-mini(2024-07-18)]
  • IDE: [e.g. Visual Studio, VS Code]
  • OS: [e.g. Windows, Mac]

Additional context
Add any other context about the problem here.

from: https://teams.microsoft.com/l/message/19:W5nYJLrRvQatcJ04yDmCkzm-t7B6c7VTxS57yNtvSzU1@thread.tacv2/1741016042436?tenantId=72f988bf-86f1-41af-91ab-2d7cd011db47&groupId=a87ae912-78ce-4eb9-862a-e7edf1b2aaa6&parentMessageId=1741016042436&teamName=Semantic%20Kernel%20-%20MSFT%20Support&channelName=General&createdTime=1741016042436

Extract Plugin Name and Arguments with agent.invoke
Is there a way to extract the plugin name and arguments with agent.invoke ?

I need this for evaluation purposes while using ChatCompletionAgent with FunctionChoiceBehavior.Auto().

Using ChatCompletionAgent with agent.invoke and FunctionChoiceBehavior.Auto() (sample code attached).

With agent.invoke(), content.items only contains TextContents.
But with agent.invoke_stream(), content.items includes both FunctionResultContents and TextContents.

How can I retrieveFunctionResultContents (with function name, plugin name, and arguments) when using agent.invoke()?

Copyright (c) Microsoft. All rights reserved.

import asyncio
from typing import Annotated
from semantic_kernel import Kernel
from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.contents import ChatHistory, FunctionCallContent, FunctionResultContent
from semantic_kernel.functions import KernelArguments, kernel_function

Define a sample plugin for the sample

class MenuPlugin:
"""A sample Menu Plugin used for the concept sample."""
@kernel_function(description="Provides a list of specials from the menu.")
def get_specials(self) -> Annotated[str, "Returns the specials from the menu."]:
return """
Special Soup: Clam Chowder
Special Salad: Cobb Salad
Special Drink: Chai Tea
"""
@kernel_function(description="Provides the price of the requested menu item.")
def get_item_price(
self, menu_item: Annotated[str, "The name of the menu item."]
) -> Annotated[str, "Returns the price of the menu item."]:
return "$9.99"

Simulate a conversation with the agent

USER_INPUTS = [
"Hello",
"What is the special soup?",
"What does that cost?",
"Thank you",
]

async def main():
# 1. Create the instance of the Kernel to register the plugin and service
service_id = "agent"
kernel = Kernel()
kernel.add_plugin(MenuPlugin(), plugin_name="menu")
kernel.add_service(AzureChatCompletion(service_id=service_id))
# 2. Configure the function choice behavior to auto invoke kernel functions
# so that the agent can automatically execute the menu plugin functions when needed
settings = kernel.get_prompt_execution_settings_from_service_id(service_id=service_id)
settings.function_choice_behavior = FunctionChoiceBehavior.Auto()
# 3. Create the agent
agent = ChatCompletionAgent(
kernel=kernel,
name="Host",
instructions="Answer questions about the menu.",
arguments=KernelArguments(settings=settings),
)
# 4. Create a chat history to hold the conversation
chat_history = ChatHistory()
for user_input in USER_INPUTS:
# 5. Add the user input to the chat history
chat_history.add_user_message(user_input)
print(f"# User: {user_input}")
# 6. Invoke the agent for a response
async for content in agent.invoke(chat_history):
print(f"# {content.name}: ", end="")
if (
not any(isinstance(item, (FunctionCallContent, FunctionResultContent)) for item in content.items)
and content.content.strip()
):
# We only want to print the content if it's not a function call or result
print(f"{content.content}", end="", flush=True)
print("")

@sophialagerkranspandey sophialagerkranspandey added the bug Something isn't working label Mar 3, 2025
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code python Pull requests for the Python Semantic Kernel triage labels Mar 3, 2025
@github-actions github-actions bot changed the title Extract Plugin Name and Arguments with agent.invoke Python: Extract Plugin Name and Arguments with agent.invoke Mar 3, 2025
@github-actions github-actions bot changed the title Extract Plugin Name and Arguments with agent.invoke .Net: Extract Plugin Name and Arguments with agent.invoke Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code python Pull requests for the Python Semantic Kernel
Projects
None yet
Development

No branches or pull requests

3 participants