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
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("")
The text was updated successfully, but these errors were encountered:
github-actionsbot
changed the title
Extract Plugin Name and Arguments with agent.invoke
Python: Extract Plugin Name and Arguments with agent.invoke
Mar 3, 2025
github-actionsbot
changed the title
Extract Plugin Name and Arguments with agent.invoke
.Net: Extract Plugin Name and Arguments with agent.invoke
Mar 3, 2025
Describe the bug
A clear and concise description of what the bug is.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
A clear and concise description of what you expected to happen.
Screenshots
If applicable, add screenshots to help explain your problem.
Platform
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("")
The text was updated successfully, but these errors were encountered: