|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from typing import Annotated |
| 5 | + |
| 6 | +from azure.identity.aio import DefaultAzureCredential |
| 7 | + |
| 8 | +from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread |
| 9 | +from semantic_kernel.functions import kernel_function |
| 10 | + |
| 11 | +""" |
| 12 | +The following sample demonstrates how to create an Azure AI agent that answers |
| 13 | +questions about a sample menu using a Semantic Kernel Plugin. After all questions |
| 14 | +are answered, it retrieves and prints the messages from the thread. |
| 15 | +""" |
| 16 | + |
| 17 | + |
| 18 | +# Define a sample plugin for the sample |
| 19 | +class MenuPlugin: |
| 20 | + """A sample Menu Plugin used for the concept sample.""" |
| 21 | + |
| 22 | + @kernel_function(description="Provides a list of specials from the menu.") |
| 23 | + def get_specials(self) -> Annotated[str, "Returns the specials from the menu."]: |
| 24 | + return """ |
| 25 | + Special Soup: Clam Chowder |
| 26 | + Special Salad: Cobb Salad |
| 27 | + Special Drink: Chai Tea |
| 28 | + """ |
| 29 | + |
| 30 | + @kernel_function(description="Provides the price of the requested menu item.") |
| 31 | + def get_item_price( |
| 32 | + self, menu_item: Annotated[str, "The name of the menu item."] |
| 33 | + ) -> Annotated[str, "Returns the price of the menu item."]: |
| 34 | + return "$9.99" |
| 35 | + |
| 36 | + |
| 37 | +# Simulate a conversation with the agent |
| 38 | +USER_INPUTS = [ |
| 39 | + "Hello", |
| 40 | + "What is the special soup?", |
| 41 | + "How much does that cost?", |
| 42 | + "Thank you", |
| 43 | +] |
| 44 | + |
| 45 | + |
| 46 | +async def main() -> None: |
| 47 | + async with ( |
| 48 | + DefaultAzureCredential() as creds, |
| 49 | + AzureAIAgent.create_client(credential=creds) as client, |
| 50 | + ): |
| 51 | + # 1. Create an agent on the Azure AI agent service |
| 52 | + agent_definition = await client.agents.create_agent( |
| 53 | + model=AzureAIAgentSettings().model_deployment_name, |
| 54 | + name="Host", |
| 55 | + instructions="Answer questions about the menu.", |
| 56 | + ) |
| 57 | + |
| 58 | + # 2. Create a Semantic Kernel agent for the Azure AI agent |
| 59 | + agent = AzureAIAgent( |
| 60 | + client=client, |
| 61 | + definition=agent_definition, |
| 62 | + plugins=[MenuPlugin()], # Add the plugin to the agent |
| 63 | + ) |
| 64 | + |
| 65 | + # 3. Create a thread for the agent |
| 66 | + # If no thread is provided, a new thread will be |
| 67 | + # created and returned with the initial response |
| 68 | + thread: AzureAIAgentThread | None = None |
| 69 | + |
| 70 | + try: |
| 71 | + for user_input in USER_INPUTS: |
| 72 | + print(f"# User: {user_input}") |
| 73 | + # 4. Invoke the agent for the specified thread for response |
| 74 | + async for response in agent.invoke( |
| 75 | + messages=user_input, |
| 76 | + thread=thread, |
| 77 | + ): |
| 78 | + print(f"# {response.name}: {response}") |
| 79 | + thread = response.thread |
| 80 | + finally: |
| 81 | + # 5. Cleanup: Delete the thread and agent |
| 82 | + # await thread.delete() if thread else None |
| 83 | + await client.agents.delete_agent(agent.id) |
| 84 | + |
| 85 | + print("*" * 50) |
| 86 | + print("# Messages in the thread (asc order):\n") |
| 87 | + async for msg in thread.get_messages(sort_order="asc"): |
| 88 | + print(f"# {msg.role} for name={msg.name}: {msg.content}") |
| 89 | + print("*" * 50) |
| 90 | + |
| 91 | + await thread.delete() if thread else None |
| 92 | + |
| 93 | + """ |
| 94 | + # User: Hello |
| 95 | + # Host: Hello! How can I assist you with the menu today? |
| 96 | + # User: What is the special soup? |
| 97 | + # Host: The special soup today is Clam Chowder. Would you like to know more about it or anything else |
| 98 | + on the menu? |
| 99 | + # User: How much does that cost? |
| 100 | + # Host: The Clam Chowder costs $9.99. Would you like to order it or need information on other items? |
| 101 | + # User: Thank you |
| 102 | + # Host: You're welcome! If you have any more questions or need assistance with the menu, feel free to ask. |
| 103 | + Enjoy your meal! |
| 104 | + ************************************************** |
| 105 | + # Messages in the thread (asc order): |
| 106 | +
|
| 107 | + # AuthorRole.USER for name=asst_mXwZOwyJLxXGtaYKHizRH6Ip: Hello |
| 108 | + # AuthorRole.ASSISTANT for name=asst_mXwZOwyJLxXGtaYKHizRH6Ip: Hello! How can I assist you with the menu today? |
| 109 | + # AuthorRole.USER for name=asst_mXwZOwyJLxXGtaYKHizRH6Ip: What is the special soup? |
| 110 | + # AuthorRole.ASSISTANT for name=asst_mXwZOwyJLxXGtaYKHizRH6Ip: The special soup today is Clam Chowder. Would |
| 111 | + you like to know more about it or anything else on the menu? |
| 112 | + # AuthorRole.USER for name=asst_mXwZOwyJLxXGtaYKHizRH6Ip: How much does that cost? |
| 113 | + # AuthorRole.ASSISTANT for name=asst_mXwZOwyJLxXGtaYKHizRH6Ip: The Clam Chowder costs $9.99. Would you like to |
| 114 | + order it or need information on other items? |
| 115 | + # AuthorRole.USER for name=asst_mXwZOwyJLxXGtaYKHizRH6Ip: Thank you |
| 116 | + # AuthorRole.ASSISTANT for name=asst_mXwZOwyJLxXGtaYKHizRH6Ip: You're welcome! If you have any more questions |
| 117 | + or need assistance with the menu, feel free to ask. Enjoy your meal! |
| 118 | + """ |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + asyncio.run(main()) |
0 commit comments