From bb372209c716112e2f2e0ef18699807168bc32af Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Thu, 17 Apr 2025 15:57:24 +0200 Subject: [PATCH 1/3] updated mixed chat sample --- .../agents/mixed_chat/mixed_chat_agents.py | 78 +++++++++---------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py b/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py index c734681b9533..74762b3b41b7 100644 --- a/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py +++ b/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py @@ -2,11 +2,17 @@ import asyncio -from semantic_kernel.agents import AgentGroupChat, AzureAssistantAgent, ChatCompletionAgent -from semantic_kernel.agents.strategies.termination.termination_strategy import TerminationStrategy +from azure.identity.aio import DefaultAzureCredential + +from semantic_kernel.agents import ( + AgentGroupChat, + AzureAIAgent, + AzureAIAgentSettings, + ChatCompletionAgent, +) +from semantic_kernel.agents.strategies import TerminationStrategy from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion from semantic_kernel.contents import AuthorRole -from semantic_kernel.kernel import Kernel ##################################################################### # The following sample demonstrates how to create an OpenAI # @@ -43,44 +49,34 @@ async def should_agent_terminate(self, agent, history): """ -def _create_kernel_with_chat_completion(service_id: str) -> Kernel: - kernel = Kernel() - kernel.add_service(AzureChatCompletion(service_id=service_id)) - return kernel - - async def main(): - agent_reviewer = ChatCompletionAgent( - kernel=_create_kernel_with_chat_completion("artdirector"), - name=REVIEWER_NAME, - instructions=REVIEWER_INSTRUCTIONS, - ) - - # To create an AzureAssistantAgent for Azure OpenAI, use the following: - client, model = AzureAssistantAgent.setup_resources() - - # Create the assistant definition - definition = await client.beta.assistants.create( - model=model, - name=COPYWRITER_NAME, - instructions=COPYWRITER_INSTRUCTIONS, - ) - - # Create the AzureAssistantAgent instance using the client and the assistant definition - agent_writer = AzureAssistantAgent( - client=client, - definition=definition, - ) - - # Create the AgentGroupChat object and specify the list of agents along with the termination strategy - chat = AgentGroupChat( - agents=[agent_writer, agent_reviewer], - termination_strategy=ApprovalTerminationStrategy(agents=[agent_reviewer], maximum_iterations=10), - ) - - input = "a slogan for a new line of electric cars." - - try: + async with ( + # 1. Login to Azure and create a Azure AI Project Client + DefaultAzureCredential() as creds, + AzureAIAgent.create_client(credential=creds) as client, + ): + agent_writer = AzureAIAgent( + client=client, + definition=await client.agents.create_agent( + model=AzureAIAgentSettings().model_deployment_name, + name=COPYWRITER_NAME, + instructions=COPYWRITER_INSTRUCTIONS, + ), + ) + agent_reviewer = ChatCompletionAgent( + service=AzureChatCompletion(service_id="artdirector"), + name=REVIEWER_NAME, + instructions=REVIEWER_INSTRUCTIONS, + ) + + # Create the AgentGroupChat object and specify the list of agents along with the termination strategy + chat = AgentGroupChat( + agents=[agent_writer, agent_reviewer], + termination_strategy=ApprovalTerminationStrategy(agents=[agent_reviewer], maximum_iterations=10), + ) + + input = "a slogan for a new line of electric cars." + await chat.add_chat_message(input) print(f"# {AuthorRole.USER}: '{input}'") @@ -88,8 +84,6 @@ async def main(): print(f"# {content.role} - {content.name or '*'}: '{content.content}'") print(f"# IS COMPLETE: {chat.is_complete}") - finally: - await client.beta.assistants.delete(agent_writer.id) if __name__ == "__main__": From f95ab0343723a2456d1884958c937de50a32ad25 Mon Sep 17 00:00:00 2001 From: Eduard van Valkenburg Date: Mon, 21 Apr 2025 13:02:53 +0200 Subject: [PATCH 2/3] delete agent and improved comments --- .../agents/mixed_chat/mixed_chat_agents.py | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py b/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py index 74762b3b41b7..e9ae2a6b3ec0 100644 --- a/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py +++ b/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py @@ -4,22 +4,16 @@ from azure.identity.aio import DefaultAzureCredential -from semantic_kernel.agents import ( - AgentGroupChat, - AzureAIAgent, - AzureAIAgentSettings, - ChatCompletionAgent, -) +from semantic_kernel.agents import AgentGroupChat, AzureAIAgent, AzureAIAgentSettings, ChatCompletionAgent from semantic_kernel.agents.strategies import TerminationStrategy from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion from semantic_kernel.contents import AuthorRole -##################################################################### -# The following sample demonstrates how to create an OpenAI # -# assistant using either Azure OpenAI or OpenAI, a chat completion # -# agent and have them participate in a group chat to work towards # -# the user's requirement. # -##################################################################### +""" +The following sample demonstrates how to create a Azure AI Foundry Agent, +a chat completion agent and have them participate in a group chat to work towards +the user's requirement. +""" class ApprovalTerminationStrategy(TerminationStrategy): @@ -55,6 +49,7 @@ async def main(): DefaultAzureCredential() as creds, AzureAIAgent.create_client(credential=creds) as client, ): + # 2. Create agents agent_writer = AzureAIAgent( client=client, definition=await client.agents.create_agent( @@ -69,21 +64,22 @@ async def main(): instructions=REVIEWER_INSTRUCTIONS, ) - # Create the AgentGroupChat object and specify the list of agents along with the termination strategy + # 3. Create the AgentGroupChat object and specify the list of agents along with the termination strategy chat = AgentGroupChat( agents=[agent_writer, agent_reviewer], termination_strategy=ApprovalTerminationStrategy(agents=[agent_reviewer], maximum_iterations=10), ) + # 4. Provide the task an start running input = "a slogan for a new line of electric cars." - await chat.add_chat_message(input) print(f"# {AuthorRole.USER}: '{input}'") - async for content in chat.invoke(): print(f"# {content.role} - {content.name or '*'}: '{content.content}'") + # 5. Done and remove the Auzre AI Foundry Agent. print(f"# IS COMPLETE: {chat.is_complete}") + await client.agents.delete_agent(agent_writer.definition.id) if __name__ == "__main__": From f6b7cbcc9cb34cf043787759d6d206634c2967bf Mon Sep 17 00:00:00 2001 From: Eduard van Valkenburg Date: Mon, 21 Apr 2025 13:14:42 +0200 Subject: [PATCH 3/3] small fix --- .../samples/concepts/agents/mixed_chat/mixed_chat_agents.py | 1 + .../azure_ai_agent/step4_azure_ai_agent_code_interpreter.py | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py b/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py index e9ae2a6b3ec0..197e2cd3bf17 100644 --- a/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py +++ b/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py @@ -79,6 +79,7 @@ async def main(): # 5. Done and remove the Auzre AI Foundry Agent. print(f"# IS COMPLETE: {chat.is_complete}") + await client.agents.delete_agent(agent_writer.definition.id) diff --git a/python/samples/getting_started_with_agents/azure_ai_agent/step4_azure_ai_agent_code_interpreter.py b/python/samples/getting_started_with_agents/azure_ai_agent/step4_azure_ai_agent_code_interpreter.py index f096e220494f..88740623a991 100644 --- a/python/samples/getting_started_with_agents/azure_ai_agent/step4_azure_ai_agent_code_interpreter.py +++ b/python/samples/getting_started_with_agents/azure_ai_agent/step4_azure_ai_agent_code_interpreter.py @@ -17,8 +17,6 @@ async def main() -> None: - ai_agent_settings = AzureAIAgentSettings() - async with ( DefaultAzureCredential() as creds, AzureAIAgent.create_client(credential=creds) as client, @@ -26,7 +24,7 @@ async def main() -> None: # 1. Create an agent with a code interpreter on the Azure AI agent service code_interpreter = CodeInterpreterTool() agent_definition = await client.agents.create_agent( - model=ai_agent_settings.model_deployment_name, + model=AzureAIAgentSettings().model_deployment_name, tools=code_interpreter.definitions, tool_resources=code_interpreter.resources, ) @@ -40,7 +38,7 @@ async def main() -> None: # 3. 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 + thread: AzureAIAgentThread | None = None try: print(f"# User: '{TASK}'")