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..197e2cd3bf17 100644 --- a/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py +++ b/python/samples/concepts/agents/mixed_chat/mixed_chat_agents.py @@ -2,18 +2,18 @@ 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 # -# 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): @@ -43,53 +43,44 @@ 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, + ): + # 2. Create agents + 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, + ) + + # 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}") - finally: - await client.beta.assistants.delete(agent_writer.id) + + await client.agents.delete_agent(agent_writer.definition.id) if __name__ == "__main__": 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}'")