|
| 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.contents import FunctionCallContent, FunctionResultContent |
| 10 | +from semantic_kernel.contents.chat_message_content import ChatMessageContent |
| 11 | +from semantic_kernel.functions import kernel_function |
| 12 | + |
| 13 | +""" |
| 14 | +The following sample demonstrates how to create an Azure AI Agent |
| 15 | +and use it with functions. In order to answer user questions, the |
| 16 | +agent internally uses the functions. These internal steps are returned |
| 17 | +to the user as part of the agent's response. Thus, the invoke method |
| 18 | +configures a message callback to receive the agent's internal messages. |
| 19 | +
|
| 20 | +The agent is configured to use a plugin that provides a list of |
| 21 | +specials from the menu and the price of the requested menu item. |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +# Define a sample plugin for the sample |
| 26 | +class MenuPlugin: |
| 27 | + """A sample Menu Plugin used for the concept sample.""" |
| 28 | + |
| 29 | + @kernel_function(description="Provides a list of specials from the menu.") |
| 30 | + def get_specials(self) -> Annotated[str, "Returns the specials from the menu."]: |
| 31 | + return """ |
| 32 | + Special Soup: Clam Chowder |
| 33 | + Special Salad: Cobb Salad |
| 34 | + Special Drink: Chai Tea |
| 35 | + """ |
| 36 | + |
| 37 | + @kernel_function(description="Provides the price of the requested menu item.") |
| 38 | + def get_item_price( |
| 39 | + self, menu_item: Annotated[str, "The name of the menu item."] |
| 40 | + ) -> Annotated[str, "Returns the price of the menu item."]: |
| 41 | + return "$9.99" |
| 42 | + |
| 43 | + |
| 44 | +intermediate_steps: list[ChatMessageContent] = [] |
| 45 | + |
| 46 | + |
| 47 | +async def handle_intermediate_steps(message: ChatMessageContent) -> None: |
| 48 | + intermediate_steps.append(message) |
| 49 | + |
| 50 | + |
| 51 | +async def main() -> None: |
| 52 | + ai_agent_settings = AzureAIAgentSettings.create() |
| 53 | + |
| 54 | + async with ( |
| 55 | + DefaultAzureCredential() as creds, |
| 56 | + AzureAIAgent.create_client( |
| 57 | + credential=creds, |
| 58 | + conn_str=ai_agent_settings.project_connection_string.get_secret_value(), |
| 59 | + ) as client, |
| 60 | + ): |
| 61 | + AGENT_NAME = "Host" |
| 62 | + AGENT_INSTRUCTIONS = "Answer questions about the menu." |
| 63 | + |
| 64 | + # Create agent definition |
| 65 | + agent_definition = await client.agents.create_agent( |
| 66 | + model=ai_agent_settings.model_deployment_name, |
| 67 | + name=AGENT_NAME, |
| 68 | + instructions=AGENT_INSTRUCTIONS, |
| 69 | + ) |
| 70 | + |
| 71 | + # Create the AzureAI Agent |
| 72 | + agent = AzureAIAgent( |
| 73 | + client=client, |
| 74 | + definition=agent_definition, |
| 75 | + plugins=[MenuPlugin()], # add the sample plugin to the agent |
| 76 | + ) |
| 77 | + |
| 78 | + # Create a thread for the agent |
| 79 | + # If no thread is provided, a new thread will be |
| 80 | + # created and returned with the initial response |
| 81 | + thread: AzureAIAgentThread = None |
| 82 | + |
| 83 | + user_inputs = [ |
| 84 | + "Hello", |
| 85 | + "What is the special soup?", |
| 86 | + "How much does that cost?", |
| 87 | + "Thank you", |
| 88 | + ] |
| 89 | + |
| 90 | + try: |
| 91 | + for user_input in user_inputs: |
| 92 | + print(f"# User: '{user_input}'") |
| 93 | + async for response in agent.invoke( |
| 94 | + messages=user_input, |
| 95 | + thread=thread, |
| 96 | + on_intermediate_message=handle_intermediate_steps, |
| 97 | + ): |
| 98 | + print(f"# Agent: {response}") |
| 99 | + thread = response.thread |
| 100 | + finally: |
| 101 | + # Cleanup: Delete the thread and agent |
| 102 | + await thread.delete() if thread else None |
| 103 | + await client.agents.delete_agent(agent.id) |
| 104 | + |
| 105 | + # Print the intermediate steps |
| 106 | + print("\nIntermediate Steps:") |
| 107 | + for msg in intermediate_steps: |
| 108 | + if any(isinstance(item, FunctionResultContent) for item in msg.items): |
| 109 | + for fr in msg.items: |
| 110 | + if isinstance(fr, FunctionResultContent): |
| 111 | + print(f"Function Result:> {fr.result} for function: {fr.name}") |
| 112 | + elif any(isinstance(item, FunctionCallContent) for item in msg.items): |
| 113 | + for fcc in msg.items: |
| 114 | + if isinstance(fcc, FunctionCallContent): |
| 115 | + print(f"Function Call:> {fcc.name} with arguments: {fcc.arguments}") |
| 116 | + else: |
| 117 | + print(f"{msg.role}: {msg.content}") |
| 118 | + |
| 119 | + # Sample output: |
| 120 | + # User: 'Hello' |
| 121 | + # Agent: Hello! How can I assist you today? |
| 122 | + # User: 'What is the special soup?' |
| 123 | + # Agent: The special soup is Clam Chowder. Would you like to know more about the menu or anything else? |
| 124 | + # User: 'How much does that cost?' |
| 125 | + # Agent: The Clam Chowder costs $9.99. If you have any more questions or need further assistance, feel free to ask! |
| 126 | + # User: 'Thank you' |
| 127 | + # Agent: You're welcome! If you have any more questions in the future, don't hesitate to ask. Have a great day! |
| 128 | + # |
| 129 | + # Intermediate Steps: |
| 130 | + # AuthorRole.ASSISTANT: Hello! How can I assist you today? |
| 131 | + # Function Call:> MenuPlugin-get_specials with arguments: {} |
| 132 | + # Function Result:> |
| 133 | + # Special Soup: Clam Chowder |
| 134 | + # Special Salad: Cobb Salad |
| 135 | + # Special Drink: Chai Tea |
| 136 | + # for function: MenuPlugin-get_specials |
| 137 | + # AuthorRole.ASSISTANT: The special soup is Clam Chowder. Would you like to know more about the menu or anything |
| 138 | + # else? |
| 139 | + # Function Call:> MenuPlugin-get_item_price with arguments: {"menu_item":"Clam Chowder"} |
| 140 | + # Function Result:> $9.99 for function: MenuPlugin-get_item_price |
| 141 | + # AuthorRole.ASSISTANT: The Clam Chowder costs $9.99. If you have any more questions or need further assistance, |
| 142 | + # feel free to ask! |
| 143 | + # AuthorRole.ASSISTANT: You're welcome! If you have any more questions in the future, don't hesitate to ask. |
| 144 | + # Have a great day! |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + asyncio.run(main()) |
0 commit comments