Loading a Foundry agent in code and adding a plugin to it before executing it #13016
-
Hello My problem occurs when I create an agent in the Azure AI Foundry Project (in the portal) and write code to retrieve the endpoint and retrieve the agent (all works so far). However, I am unable to add a plugin to an existing agent from Foundry in code. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi Lino, The are two steps to perform when creating an AzureAIAgent. You first create the "agent definition" which is the server-side record of the agent (or in your case, you created that in the Portal, and you pull it down via the agent id). Secondly, you create the SK AzureAIAgent instance, passing in the Azure client, and the agent's definition (that you either created or pulled down from the server). When you create the AzureAIAgent instance, you can provide it a list of plugins. Here's an example in Python, where I create a first agent instance, and then use the agent.id to get the agent definition and then use that definition to create the second SK agent instance, while adding plugins. # Copyright (c) Microsoft. All rights reserved.
import asyncio
from typing import Annotated
from azure.identity.aio import AzureCliCredential
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentThread
from semantic_kernel.functions import kernel_function
# Simulate a conversation with the agent
USER_INPUTS = [
"Hello, I am John Doe.",
]
# A plugin we'll use for the agent we create after pulling down the server's agent definition
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"
async def main() -> None:
async with (
AzureCliCredential() as creds,
AzureAIAgent.create_client(credential=creds) as client,
):
# Create an agent on the Azure AI agent service
agent_definition = await client.agents.create_agent(
model="gpt-4o",
name="Assistant",
instructions="Answer the user's questions.",
)
# Create a Semantic Kernel agent for the Azure AI agent
agent = AzureAIAgent(
client=client,
definition=agent_definition,
)
# Create a thread for the agent
# If no thread is provided, a new thread will be
# created and returned with the initial response
thread: AzureAIAgentThread = None
try:
for user_input in USER_INPUTS:
print(f"# User: {user_input}")
# Invoke the agent with the specified message for response
response = await agent.get_response(messages=user_input, thread=thread)
print(f"# {response.name}: {response}")
thread = response.thread
# Create the second agent def by getting the def from the server for that agent id
second_agent_definition = await client.agents.get_agent(agent.id)
# Now create the SK Agent instance using the def, client, and plugins
second_azure_ai_agent = AzureAIAgent(
client=client, definition=second_agent_definition, plugins=[MenuPlugin()] # now we add plugins to this agent instance
)
response = await second_azure_ai_agent.get_response("What is the special food item?")
print(f"# {response.name}: {response}")
finally:
# Cleanup: Delete the thread and agent
await thread.delete() if thread else None
await client.agents.delete_agent(agent.id)
if __name__ == "__main__":
asyncio.run(main()) The output looks like:
Note that the plugins need to exist on the SK AzureAIAgent instance - even if your server-side agent definition contains function tools as part of the definition, they also need to be added to the agent - the server only has a JSON schema representation of the tools, not the actual native code tools required to be invoked during operation/function calling. |
Beta Was this translation helpful? Give feedback.
Hi Lino,
The are two steps to perform when creating an AzureAIAgent. You first create the "agent definition" which is the server-side record of the agent (or in your case, you created that in the Portal, and you pull it down via the agent id). Secondly, you create the SK AzureAIAgent instance, passing in the Azure client, and the agent's definition (that you either created or pulled down from the server). When you create the AzureAIAgent instance, you can provide it a list of plugins. Here's an example in Python, where I create a first agent instance, and then use the agent.id to get the agent definition and then use that definition to create the second SK agent instance, while adding plu…